note 74937 deleted from language.oop5.magic by cmb

From: Date: Sun, 10 Nov 2019 12:38:28 +0000
Subject: note 74937 deleted from language.oop5.magic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212986@lists.php.net to get a copy of this message
Note Submitter: adar at darkpoetry dot de ---- Maybe not really new and all in all definitely not the best solution,but if you cant extend a class (if your class alreay extends an abstract or other things like that) you can 'fake' a extend. <?php class MyClass extends SomeAbstractUnknownClass { private $classObject; public function __construct ( classObject $classToExtend ) { $this->classObject = $classToExtend; } public function __call($func, $var) { if ( !count($var) ) { return $this->classObject->$func($var); } else { $str = ''; $values = array_values($var); for ( $i=0; $i<count($values); $i++ ) { $str .= "'".$values[$i]."' ,"; } $str = substr($str, 0, -2); return eval('return $this->classObject->'.$func.'('.$str.');'); } } } ?> So if you'll do a $myClass->unknownMethod() and it is found neither in MyClass nor in SomeAbstractUnknownClass, MyClass will try to call this method in $classObject. I use this for 'extending' a UserObject-Class which already extends an other one. Better solutions are always welcome ;)

« previous php.notes (#212986) next »