ctor_dtor_inheritance.phpt 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. --TEST--
  2. ZE2 A derived class can use the inherited constructor/destructor
  3. --FILE--
  4. <?php
  5. // This test checks for:
  6. // - inherited constructors/destructors are not called automatically
  7. // - base classes know about derived properties in constructor/destructor
  8. // - base class constructors/destructors know the instanciated class name
  9. class base {
  10. public $name;
  11. function __construct() {
  12. echo __CLASS__ . "::" . __FUNCTION__ . "\n";
  13. $this->name = 'base';
  14. print_r($this);
  15. }
  16. function __destruct() {
  17. echo __CLASS__ . "::" . __FUNCTION__ . "\n";
  18. print_r($this);
  19. }
  20. }
  21. class derived extends base {
  22. public $other;
  23. function __construct() {
  24. $this->name = 'init';
  25. $this->other = 'other';
  26. print_r($this);
  27. parent::__construct();
  28. echo __CLASS__ . "::" . __FUNCTION__ . "\n";
  29. $this->name = 'derived';
  30. print_r($this);
  31. }
  32. function __destruct() {
  33. parent::__destruct();
  34. echo __CLASS__ . "::" . __FUNCTION__ . "\n";
  35. print_r($this);
  36. }
  37. }
  38. echo "Testing class base\n";
  39. $t = new base();
  40. unset($t);
  41. echo "Testing class derived\n";
  42. $t = new derived();
  43. unset($t);
  44. echo "Done\n";
  45. ?>
  46. --EXPECT--
  47. Testing class base
  48. base::__construct
  49. base Object
  50. (
  51. [name] => base
  52. )
  53. base::__destruct
  54. base Object
  55. (
  56. [name] => base
  57. )
  58. Testing class derived
  59. derived Object
  60. (
  61. [other] => other
  62. [name] => init
  63. )
  64. base::__construct
  65. derived Object
  66. (
  67. [other] => other
  68. [name] => base
  69. )
  70. derived::__construct
  71. derived Object
  72. (
  73. [other] => other
  74. [name] => derived
  75. )
  76. base::__destruct
  77. derived Object
  78. (
  79. [other] => other
  80. [name] => derived
  81. )
  82. derived::__destruct
  83. derived Object
  84. (
  85. [other] => other
  86. [name] => derived
  87. )
  88. Done