note 58364 added to language.oop5.overloading
| From: | seufert at gmail dot com | Date: | Wed, 02 Nov 2005 00:25:50 +0000 |
| Subject: | note 58364 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97953@lists.php.net to get a copy of this message | ||
This allows you to seeminly dynamically overload objects using plugins.
<PRE>
<?php
class standardModule{}
class standardPlugModule extends standardModule
{
static $plugptrs;
public $var;
static function plugAdd($name, $mode, $ptr)
{
standardPlugModule::$plugptrs[$name] = $ptr;
}
function __call($fname, $fargs)
{ print "You called __call($fname)\n";
$func = standardPlugModule::$plugptrs[$fname];
$r = call_user_func_array($func, array_merge(array($this),$fargs));
print "Done: __call($fname)\n";
return $r;
}
function dumpplugptrs() {var_dump(standardPlugModule::$plugptrs); }
}
class a extends standardPlugModule
{ function text() { return "Text"; } }
//Class P contained within a seperate file thats included
class p
{ static function plugin1($mthis, $r)
{ print "You called p::plugin1\n";
print_r($mthis);
print_r($r);
}
} a::plugAdd('callme', 0, array('p','plugin1'));
//Class P contained within a seperate file thats included
class p2
{ static function plugin2($mthis, $r)
{ print "You called p2::plugin2\n";
$mthis->callme($r);
}
} a::plugAdd('callme2', 0, array('p2','plugin2'));
$t = new a();
$testr = array(1,4,9,16);
print $t->text()."\n";
$t->callme2($testr);
//$t->dumpplugptrs();
?>
</pre>
Will result in:
----------
Text
You called __call(callme2)
You called p2::plugin2
You called __call(callme)
You called p::plugin1
a Object
(
[var] =>
)
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
)
Done: __call(callme)
Done: __call(callme2)
----------
This also clears up a fact that you can nest __call() functions, you could use this to get around
the limits to __get() not being able to be called recursively.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58364
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58364&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58364&report=yes
Search -- http://master.php.net/manage/user-notes.php