note 88949 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:29:59 +0000 |
| Subject: | note 88949 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211905@lists.php.net to get a copy of this message | ||
Note Submitter: niehztog
----
If you got a parent class agregating(not inheriting) a number of child classes in an array, you can
use the following to allow calling methods of the parent object on agregated child objects:
<?php
class child {
public $holder = null;
public function __call($name, $arguments) {
if($this->holder instanceof parentClass && method_exists($this->holder,
$name)) {
return call_user_func_array(array($this->holder, $name), $arguments);
}
else {
trigger_error();
}
}
}
class parentClass {
private $children = array();
function __construct() {
$this->children[0] = new child();
$this->children[0]->holder = $this;
}
function getChild($number) {
if(!isset($this->children[$number])) {
return false;
}
return $this->children[$number];
}
function test() {
return 'it works';
}
}
$parent = new parentClass();
$firstChild = $parent->getChild(0);
echo $firstChild->test(); //should output 'it works'
?>