fork(1) download
  1. <?php
  2.  
  3. class Dog {
  4. function say () { echo "Гав"; }
  5. }
  6. class Cat {
  7. function say () { echo "Мяу"; }
  8. }
  9.  
  10. // А теперь введем аспект, "усиливающий" say()
  11. trait Strong {
  12. function say () { parent::say(); echo "!"; }
  13. }
  14.  
  15. class StrongDog extends Dog { use Strong; }
  16. class StrongCat extends Cat { use Strong; }
  17.  
  18. $animals = [new Dog(), new Cat(), new StrongDog(), new StrongCat()];
  19. foreach ($animals as $a) {
  20. $a->say(); echo "\n";
  21. }
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Гав
Мяу
Гав!
Мяу!