class S1Class { public function s1ParentFunc($s1Str){ echo $s1Str . "parent br />"; } } class s1ChildClass extends S1Class { public function s1ChildFunc($s1ChildStr){ echo $s1ChildStr . "child br />"; } } $s1Obj = new S1Class(); $s1ChildObj = new s1ChildClass(); $s1ChildObj02 = new s1ChildClass(); $s1Obj->s1ParentFunc("aaa"); $s1ChildObj->s1ParentFunc("bbb"); $s1ChildObj02->s1ChildFunc("ccc");
class Box { public $myItem; public function __construct($item) { $this->myItem = $item; } public function open() { echo "宝箱を開いた。".$this->myItem."を手に入れた。\n"; } } class MagicBox extends Box { public function look() { echo "宝箱は妖しく輝いている。\n"; } public function open() { echo "宝箱を開いた。".$this->myItem."が襲ってきた!\n"; } } $box = new Box("鋼鉄の剣"); $box->open(); $magicBox = new MagicBox("モノマネモンスター"); $magicBox->look(); $magicBox->open();
class Player { public $myName; public function __construct($name) { $this->myName = $name; } public function attack($enemy) { echo $this->myName."は、".$enemy."を攻撃した!\n"; } } echo "=== パーティーでスライムと戦う ===\n"; $hero = new Player("勇者"); $warrior = new Player("戦士"); $party = [$hero, $warrior]; foreach ($party as $member) { $member->attack("スライム"); } class Player { public $myName; public function __construct($name) { $this->myName = $name; } public function attack($enemy) { echo $this->myName."は、".$enemy."を攻撃した!\n"; } } class Wizard extends Player { public function attack($enemy) { echo "シャラララーン!\n"; echo $this->myName."は、".$enemy."に炎を放った!\n"; } } echo "=== パーティーでスライムと戦う ===\n"; $hero = new Player("勇者"); $warrior = new Player("戦士"); $wizard = new Wizard("魔法使い"); $party = [$hero, $warrior, $wizard]; foreach ($party as $member) { $member->attack("スライム"); }