I just wrote that this example is based on the extension of user drivers, which may require an additional support package for the aforementioned main driver file.
// File: parent.php class parent_class() { protected $_protected_object; // set in constructor protected $_protected_var = "Something dynamic"; public function __construct() { // keep in mind that if you don't override this constructor, this will execute when extended } public function create_resource() { $this->_protected_object = new SomeObjectInstance(); } public function child_class() { static $child = null; if (is_null($child)) { $file = "child.php"; if (!\file_exists($file)) { throw new Exception("Couldn't load '$file', doesn't exist"); } else { require_once($file); $child = new child_class(); $child->__overload("_protected_object", $this->_protected_object)->__overload("_protected_var", $this->_protected_var); } } return $child; } } // File: child.php class child_class extends parent_class { protected function __overload($index, &$value) { $this->$index =& $value; return $this; } public function __construct() { // REMEMBER: if you don't declare this method, the parent constructor will execute } public function extended_func() { // code } public function etc() { // code } } // Code instantiating objects: $parent = new parent_class(); $parent->create_resource(); var_dump($parent); /** * Would output something like this: * * object(parent_class)
Note that the second var_dump () for $ child displays the same information as $ parent, except that now you can see that the specified properties are preceded by ampersands (&), which means that now there are links. So, if you change anything in an instance of the parent class with respect to these two properties, it will be reflected in the instance of the child class.
source share