abstract class Car{
abstract function getMaximumSpeed();
}
由于这个类是抽象的,不能实例化,它本身起不到什么作用。要让这个类起作用并且获得一个实例,首先必须扩展它。
class FastCar extends Car{
function getMaximumSpeed(){
return 150;
}
}
现在有了一个可以实例化的类FastCar。
class Street{
protected $speedLimit;
protected $cars;
public function __construct($speedLimit = 200){
$this->cars = array();
$this->speedLimit = $speedLimit;
}
protected function isStreetLegal($car){
if($car->getMaximumSpeed()speedLimit){
return ture;
} else {
return false;
}
}
public function addCar($car){
if($this-isStreetLegal($car)){
echo 'The Car was allowed on the road.';
$this->cars[] = $car;
} else {
echo 'The Car is too fast and was not allowed on the road.';
}
}
}
$street = new Street();
$street->addCar(new FastCar());