Return one item only, OO PHP

class Score
{
    var $score;
    var $name;
    var $dept;
    var $date;

    function Score($score, $name, $dept, $date)
    {
        $this->scores = ($score);
        $this->name = ($name);
        $this->dept = ($dept);
        $this->date = ($date);
    }

    function return_score(){
        return $this->scores;
        return $this->name;
        return $this->dept;
        return $this->date;
    }
}

$newscore = new Score("131313","James", "Marketing", "19/05/2008");
echo $newscore->return_score();

      

The above code only echoes 131313. I am just starting to learn OO PHP, so please go through! Completely lost, so any help would be much appreciated.

+1


source to share


3 answers


You cannot return more than once to a function. You can return the concatenated string:

return $this->scores.' '.this->name.' '.$this->dept.' '.$this->date;
//added spaces for readability, but this is a silly thing to do anyway...

      

I would not recommend it as you would mix object presentation with its functionality - don't do that.

I would suggest creating a template of some kind (I assume you can use this data in tables?). Each line will look something like this:



<tr>
  <td><?php echo $score->name; ?></td>
  <td><?php echo $score->scores; ?></td>
  <!-- more cells for more properies? -->
</tr>

      

and assign it an object or objects in an array (do you know about foreach {}?). I know it looks longer, but separating these concerns will be better for you in the long run.

Assigning with :: you don't need parentheses around the thing being assigned (usually).

And are you using PHP4? Your constructor function indicates that you are. I would recommend upgrading to 5.21 or higher if at all possible, as classes and objects are much better. You can also use the rather useful __construct method (as opposed to using a class named method - in your case: Score ()). This makes inheritance easier and more extensible because your classes no longer need to remember in two places which class they extend from.

+2


source


You can only return one value per function or method.

In your situation, you should have a method for each of the class members:

public function getScore() {
   return $this->score;
}

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

public function getDept() {
   return $this->dept;
}


public function getDate() {
   return $this->date;
}

      



Edit after comments:

You may also want a method that returns all members as one string:

public function getAll() {
   return $this->getScore(). " " .$this->getName() . " " .$this->getDept(). " " .$this->getDate();
}

      

+3


source


First of all, you should use public, protected or private instead of var

var $score;
var $name;
var $dept;
var $date;

      

such as

protected $score;

      

or with standard underscore encoding of protected / private variables and methods

protected $_score;

      

This method can also be called __ construct

function Score($score, $name, $dept, $date)
{

      

var is declared as a score, but you are assigning a variable to score. I also don't understand why you have parentheses around the variable.

        $this->scores = ($score);
        $this->name = ($name);
        $this->dept = ($dept);
        $this->date = ($date);

      

Replace

    $this->score = $score;
    $this->name = $name;
    $this->dept = $dept;
    $this->date = $date;

}

      

The first return encountered will return this value from the function / method. I suggest you recode to add get / set for each variable, i.e. getScore () or use PHP5's __set, __get and __call overloading method.

public function getScore() {
        return $this->score;
}

      

}

You can also see the automatic methods for setting and getting Overloading variables

+1


source







All Articles