Your going along the right lines. First up, you should do initialisation in the constructor. Not a rule, but a principle. Secondly, $names and $grades should be declared before use. With regard to having multiple records, perhaps the variables could be arrays, then you could store all the results in there. Not very OOP though. So perhaps you should make a class ReportCard and a class ReportCards.
class ReportCard
(
var $name;
var $grade;
function reportCard($name, $grade)
{
$this->name = $name;
$this->grade = $grade;
}
}
class ReportCards
{
var $reportcards = array();
function addCard($reportcard)
{
$this->reportcards[] = $reportcard
}
}
$r = new ReportCards;
$r->addCard(new ReportCard("Travis", "99%"));
So everything is an object, and so can be treated as such in the greater scheme of things. So now we need some display methods
class ReportCard
{
var $name;
var $grade;
function reportCard($name, $grade)
{
$this->name = $name;
$this->grade = $grade;
}
function display()
{
echo $this->name . ": " . $this->grade . "<br>";
}
}
class ReportCards
{
var $reportcards = array();
function addCard($reportcard)
{
$this->reportcards[] = $reportcard;
}
function displayReportCards()
{
foreach($this->reportcards as $reportcard)
{
$reportcard->display();
}
}
}
$r = new ReportCards;
$r->addCard(new ReportCard("Travis", "99%"));
$r->addCard(new ReportCard("Foobar", "0%"));
$r->displayReportCards();
In PHP 5 you can tighten things up alot, by making use of private variables, and type hinting. But that's another story 😉