![]() Mailinglisten-Archive |
Steffen Kother wrote: >> aber das wird weiterhin, in PHP5 nicht funktionieren ... man kann $this >> nicht neu zuweisen! > > > Ich werte es trotzdem erstmal als "Beifall/Lob", ganz am Anfang schrieb > ich ja auch PHP 4(.3.) :o) > > Wie w�rde das denn mit PHP5 aussehen? Oder gar nicht? > In PHP 5 ist die magische __call Methode sehr n�tzlich. Du kannst damit auch Mehrfachvererbung Simulieren. Ich habe mal eine Beispiel geschrieben wie man das realisieren kann. Dabei werden mehrere Klassen aggregiert, jede mit den Methoden die Vererbt werden sollen. Au�erdem ist ein Singleton integriert. So w�rde ich zumindest bei PHP5 ansetzen um Objekten w�hrend der Laufzeit funktionalit�t zu geben, aber auch nur wenn es keine einfachere L�sung gibt. :) Gruss, elias <?php class CoalesceProxy { private $methods = array(); private $instances = array(); private $classes = array(); private $id = 0; public function __construct() { $args = func_get_args(); foreach ( $args AS $class ) { $this->addClass($class); } //print_r($this); } public function addClass($class) { $this->classes[] = array_shift($class); foreach ( $class AS $method ) { $this->methods[$method] = $this->id; } $this->id++; } public function __call($meth, $args) { if(!isset($this->methods[$meth])) return; $className = $this->classes[$this->methods[$meth]]; if(!isset($this->instances[$className])) { $this->instances[$className] = new $className; } $obj = $this->instances[$className]; return call_user_func_array(array($obj,$meth), $args); } public function __toString() { return '['.__CLASS__.']'; } } class DummyA { function methodA() { $args = func_get_args(); return __METHOD__ . "(".implode(', ', $args).")\n"; } } class DummyB { function methodB() { $args = func_get_args(); return __METHOD__ . "(".implode(', ', $args).")\n"; } } $o = new CoalesceProxy ( array('DummyA', 'methodA'), array('DummyB', 'methodB') ); echo $o->methodA(1,2,3); echo $o->methodB(1,2,3); ?>
php::bar PHP Wiki - Listenarchive