Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When is it a good time to create another class in php?

Status
Not open for further replies.

ppat60

Programmer
Oct 20, 2007
13
US
When is it a good time to create another class in php? I have functions that belong to three separate arrays, that are getting very large in class Player.

Which me asks how does one class in php call another class to use it member functions?

Thanks in advance.
 
i don't feel that the size of a class has any relevance to whether a method should be included within it or not. to my mind a class is used to create an object that has a certain number of properties and a certain number of things that can be done to or with its properties or it, itself (methods). the logical "enclosedness" of that structure is, to me, what determines whether a new class should be created.

as to how one object calls the methods of another object, the answer depends on whether you are using php5 or php4 and whether you are using object inheritance. The manual, as ever, is excellent on these subjects.
 
Another consideration to make is:
Are there logical sections (i.e. data and behaviour) of code which you are likely to reuse within other classes/projects etc.? One of the major concepts in object-oriented programming is reusability, which is a worthy consideration in answering your question.

I agree with jpadie that size doesn't really come into it. However, if your class is massive, it's worth looking to see whether there are logical sections which would make useful classes in their own right.

A brief example of using one class in another would be:
Code:
<?php 
require_once 'MatchHistory.php';

class Player {
  
  // declare class member variables
  var $name;
  var $matchHistory;

  // constructor
  function Player($name, $gamesPlayed, $goalsScored) {
    $this->name = $name;
    $this->matchHistory = new MatchHistory($gamesPlayed, $goalsScored);
  }

  function getName() {
    return $this->name;
  }

  function getMatchHistory() {
    return $this->matchHistory;
  }
}
?>
Code:
<?php
class MatchHistory {
  
  // declare class member variables
  var $gamesPlayed;
  var $goalsScored;

  // constructor
  function MatchHistory($gamesPlayed, $goalsScored) {
    $this->gamesPlayed = $gamesPlayed;
    $this->goalsScored = $goalsScored;
  }

  // calculate goals per game ratio
  function GoalsPerGame() {
    return ($this->goalsScored / $this->gamesPlayed);
  }
}
?>
Code:
<?php  
  require_once 'Player.php';

  $myPlayer = new Player('David Beckham', 28, 3);
  $playerMatchHistory = $myPlayer->getMatchHistory();
  echo $myPlayer->getName() . ' scored ' . $playerMatchHistory->GoalsPerGame() . ' goals per game'; 
?>

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top