note 46554 added to ref.objaggregation
| From: | sixatt0xdotnet at osu1 dot php dot net | Date: | Fri, 15 Oct 2004 10:46:01 +0000 |
| Subject: | note 46554 added to ref.objaggregation | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78577@lists.php.net to get a copy of this message | ||
Here is a way how to do aggregate / multiple inheritance with PHP5 :
<?
class Aggregate {
private $properties = array();
private $methods = array();
private $objects = array();
function __construct() {
foreach (func_get_args() as $class_name) {
$o = new $class_name();
foreach (get_class_methods($class_name) as $method_name)
$this->methods[$method_name] = array($o, $method_name);
foreach (array_keys(get_class_vars($class_name)) as $prop_name)
$this->properties[$prop_name] =& $o->$prop_name;
$this->objects[] = $o;
}
}
function __get($name) {
return $this->properties[$name];
}
function __set($name, $value) {
$this->properties[$name] = $value;
}
function __call($name, $args) {
return call_user_func_array($this->methods[$name], $args);
}
}
?>
now let's try something simple :
<?
class test_a {
var $var_a = 10;
function a() {
echo $this->var_a."\n";
}
}
class test_b {
var $var_b = 20;
function b() {
echo $this->var_b."\n";
}
}
class test_c {
public $var_c;
function c($val) {
$this->var_c = $val;
}
}
$obj = new Aggregate("test_a", "test_b", "test_c");
$obj->c("test");
$obj->a();
$obj->b();
echo "this is a ".$obj->var_c."\n";
?>
The following outputs :
10
20
this is a test
----
Manual Page -- http://www.php.net/manual/en/ref.objaggregation.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46554
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46554&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46554&report=yes
Search -- http://master.php.net/manage/user-notes.php