note 58364 modified in language.oop5.overloading by danbrown

From: Date: Fri, 14 Nov 2008 17:19:26 +0000
Subject: note 58364 modified in language.oop5.overloading by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146551@lists.php.net to get a copy of this message
This allows you to seeminly dynamically overload objects using plugins. <?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(); ?> 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. --was-- 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. http://php.net/manual/en/language.oop5.overloading.php

« previous php.notes (#146551) next »