fork(1) download
  1. <?php
  2.  
  3. $pets = array( new Dog("ぽち"), new Cat("たま", 1) );
  4.  
  5. foreach( $pets as $pet ) {
  6. $pet->play();
  7. }
  8.  
  9. class Animal {
  10. var $name;
  11.  
  12. function __construct($name) {
  13. $this->name = $name;
  14. }
  15.  
  16. function play() {
  17. }
  18. }
  19.  
  20. class Dog extends Animal {
  21. function __construct($name) {
  22. parent::__construct($name);
  23. }
  24.  
  25. function play() {
  26. echo "わんわん!" . $this->name . "だよ\n";
  27. }
  28. }
  29.  
  30. class Cat extends Animal {
  31. var $sleep;
  32.  
  33. function __construct($name, $sleep) {
  34. parent::__construct($name);
  35. $this->sleep = $sleep;
  36. }
  37.  
  38. function play() {
  39. echo "にゃん!" . $this->name . "だよ\n";
  40. if($this->sleep == 1) {
  41. echo "...でも寝る\n";
  42. }
  43. }
  44. }
  45. ?>
  46.  
Success #stdin #stdout 0.03s 25752KB
stdin
Standard input is empty
stdout
わんわん!ぽちだよ
にゃん!たまだよ
...でも寝る