tags:

views:

121

answers:

3
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 is only echoing 131313. I am just beginning to learn OO PHP so please go easy! Totally lost, so any help would be much appreciated.

+3  A: 

You can only return one value in each 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 the comments:

You could also need a method that returns all the members as a single string:

public function getAll() {
   return $this->getScore(). " " .$this->getName() . " " .$this->getDept(). " " .$this->getDate();
}
Davide Gualano
+2  A: 

You can't return more than once in a function. You could return a 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 wouldn't recommend it though as you'd be mixing you presentation of the object with its functionality - don't.

I'd suggest making a template of some sort (I'm imagining you might want to tabulate this data?). Each row would look something like:

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

and giving it your object or objects in array (you know about foreach{} ?). I know it looks more long-winded, but separating these concerns is going to be better for you in the long run.

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

Also: Are you running PHP4? Your constructor function indicates you are. I'd recommend moving 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 the class named method - in your case: Score()). This makes inheritance and extending easier because your classes are no longer having to remember in two places what class they are extending from.

philistyne
+1  A: 

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 a coding standard prefix protected/private variables and methods with underscore like such

protected $_score;

This method can also be called __construct

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

The var is declared as score but you assign a variable to scores. 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 with

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

}

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

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

}

You can also look at the automatic methods to set and get variables Overloading

OIS