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.
You can’t return more than once in a function. You could return a concatenated string:
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:
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.