1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php interface Cryable { public function cry(); } interface Movable { public function move(); } trait MoveGround { public function move() { echo 'トコトコ' . PHP_EOL; return $this; } } trait MoveAir { public function move() { echo 'ヒュー' . PHP_EOL; return $this; } } abstract class Animal implements Cryable, Movable { protected $age; public function __construct($age) { $this->age = $age; } } class Cat extends Animal { use MoveGround; public function cry() { echo "ニャー({$this->age})" . PHP_EOL; return $this; } } class Bird extends Animal { use MoveAir; public function cry() { echo "鳥です({$this->age})" . PHP_EOL; return $this; } } (new Cat(5))->cry()->move(); (new Bird(7))->move()->cry(); |
1 2 3 4 |
ニャー(5) トコトコ ヒュー 鳥です(7) |