note 46554 rejected from ref.objaggregation by curt
| From: | curt@php.net | Date: | Sat, 16 Oct 2004 04:54:06 +0000 |
| Subject: | note 46554 rejected from ref.objaggregation by curt | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78642@lists.php.net to get a copy of this message | ||
Note Submitter: six at t0x dot net
----
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