<?php // 继承拥有比组合更少的代码量 class car { public function addoil() { echo "Add oil\r\n"; } } class bmw extends car { } class benz { public $car; public function __construct() { $this->car = new car(); } public function addoil() { $this->car->addoil(); } } $bmw = new bmw(); $bmw->addoil(); echo '<br>'; $benz = new benz(); $benz->addoil();
echo 'VROOOOOOOOMMMMM'; } } class chanceOfRain { public $chance; function __construct() { $this->chance = rand(0, 10); // echo $this->chance; } function checkForRain() { if ($this->chance > 5) { return true; } else { return false; } } } $myCar = new car(); $myCar->accelerate(); $myCar->letsOpenTheTop(); $myNewCar = new bmw(); $myNewCar->accelerate(); // $myNewCar->letsOpenTheTop(); $rain = new chanceOfRain(); if ($rain) { echo "<p>It's raining!</p>"; $myCar->letsCloseTheTop(); }