<?php
require("Score.php");
require("MathScore.php");
require("EnglishScore.php");
require("User.php");
$user1 = new User("Taro", new MathScore(70));
$user2 = new User("Jiro", new EnglishScore(90));
echo $user1->getInfo() . PHP_EOL;
echo $user2->getInfo() . PHP_EOL;
<?php
class User
{
private $name;
private $score;
public function __construct($name, $score)
{
$this->name = $name;
$this->score = $score;
}
public function getInfo()
{
return "{$this->name}, {$this->score->getInfo()}";
}
}
<?php
class EnglishScore extends Score
{
public function __construct($points)
{
parent::__construct("English", $points);
}
protected function getResult()
{
return $this->points >= 95 ? "Pass" : "Fail";
}
}
<?php
class MathScore extends Score
{
public function __construct($points)
{
parent::__construct("Math", $points);
}
protected function getResult()
{
return $this->points >= 50 ? "Pass" : "Fail";
}
}
<?php
abstract class Score
{
private $subject;
protected $points;
public function __construct($subject, $points)
{
$this->subject = $subject;
$this->points = $points;
}
abstract protected function getResult();
public function getInfo()
{
return "{$this->subject}, {$this->points}, {$this->getResult()}";
}
}