note 88949 added to language.oop5.overloading
| From: | niehztog at osu1 dot php dot net | Date: | Sat, 14 Feb 2009 18:59:15 +0000 |
| Subject: | note 88949 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150683@lists.php.net to get a copy of this message | ||
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'
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.79.53.172
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/88949
Del: integrated -- https://master.php.net/note/delete/88949/integrated
Del: useless -- https://master.php.net/note/delete/88949/useless
Del: bad code -- https://master.php.net/note/delete/88949/bad+code
Del: spam -- https://master.php.net/note/delete/88949/spam
Del: non-english -- https://master.php.net/note/delete/88949/non-english
Del: in docs -- https://master.php.net/note/delete/88949/in+docs
Del: other reasons-- https://master.php.net/note/delete/88949
Reject -- https://master.php.net/note/reject/88949
Search -- https://master.php.net/manage/user-notes.php