Only getting one element returned, 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 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.

Answers


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.


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();
}

Need Your Help

Swift - share sqlite database between app and extension

database sqlite swift ios8-extension

I have followed this tutorial and added my database. I did target both my app and my extension. From my app I can SELECT, INSERT, DELETE and UPDATE to the database. I want both my app and extension...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.