diff --git a/language/oop5/decon.xml b/language/oop5/decon.xml index a8bd592f9c..252256cf7a 100644 --- a/language/oop5/decon.xml +++ b/language/oop5/decon.xml @@ -21,7 +21,9 @@ Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct within the child constructor is - required. + required. If the child does not define a constructor then it may be inherited + from the parent class just like a normal class method (if it was not declared + as private). @@ -42,15 +44,27 @@ class SubClass extends BaseClass { } } +class OtherSubClass extends BaseClass { + // inherits BaseClass's constructor +} + +// In BaseClass constructor $obj = new BaseClass(); + +// In BaseClass constructor +// In SubClass constructor $obj = new SubClass(); + +// In BaseClass constructor +$obj = new OtherSubClass(); ?> ]]> For backwards compatibility, if PHP 5 cannot find a - __construct() function for a given class, it will + __construct() function for a given class, and the + class did not inherit one from a parent class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named @@ -122,7 +136,8 @@ $obj = new MyDestructableClass(); Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct in the destructor - body. + body. Also like constructors, a child class may inherit the parent's + destructor if it does not implement one itself. The destructor will be called even if script execution is stopped using