} } %FDMBSBUJPOͱ'JOE6TBHF $male = new Male(); $male->greeting(); $female = new Female(); $female->greeting(); class Male extends Human { public function greeting() { echo '͜Μʹͪ'; } } class Female extends Human { } %FDMBSBUJPO %FDMBSBUJPO 'JOE6TBHF 'JOE6TBHF
{ parent::__construct('Male'); } public function greeting() { echo $this->name; } } abstract class Human { protected $name; public function __construct($name) { $this->name = $name; } } %FDMBSBUJPO 'JOE6TBHF ˞ಉ໊ͷDMBTT͕ͳ͚Ε࿙ΕϊΠζͳ͍ ˞ಉ໊ͷDMBTT͕͋Δͱ͓͔͘͠ͳΔ
speak($male); function speak($male) { $male->greeting(); } class Male { public function greeting() { echo 'Hello'; } } NBMF.BMFDMBTTͷ ΦϒδΣΫτʂ ͜Εͳʹʁʁʁʁʁ
__construct() { $this->human = new Robot(); parent::__construct(); } public function greeting() { $this->human = new Female(); $this->human->greeting(); } } class Hoge { protected $human; public function __construct() { $this->human = new Male(); } } 3PCPU ·ͨ 'FNBMF DMBTTͰ.BMFͰೖ͞Ε Δ͔Βҙຯ͕ແ͍ೖ