note 74937 added to language.oop5.magic
| From: | adar at darkpoetry dot de | Date: | Sat, 05 May 2007 01:09:53 +0000 |
| Subject: | note 74937 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126209@lists.php.net to get a copy of this message | ||
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 ;)
----
Server IP: 217.160.72.57
Probable Submitter: 80.137.17.214
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- https://master.php.net/note/edit/74937
Del: integrated -- https://master.php.net/note/delete/74937/integrated
Del: useless -- https://master.php.net/note/delete/74937/useless
Del: bad code -- https://master.php.net/note/delete/74937/bad+code
Del: spam -- https://master.php.net/note/delete/74937/spam
Del: non-english -- https://master.php.net/note/delete/74937/non-english
Del: in docs -- https://master.php.net/note/delete/74937/in+docs
Del: other reasons-- https://master.php.net/note/delete/74937
Reject -- https://master.php.net/note/reject/74937
Search -- https://master.php.net/manage/user-notes.php