note 90964 added to language.oop5.reflection

From: Date: Mon, 18 May 2009 21:00:03 +0000
Subject: note 90964 added to language.oop5.reflection
Groups: php.notes 
Request: Send a blank email to php-notes+get-155701@lists.php.net to get a copy of this message
If you want to use method closures and don't have PHP 5.3, perhaps you find useful the following function <?php function get_method_closure($object,$method_name){ if(method_exists(get_class($object),$method_name)){ $func = create_function( '', ' $args = func_get_args(); static $object = NULL; /* * Check if this function is being called to set the static $object, which * containts scope information to invoke the method */ if(is_null($object) && count($args) && get_class($args[0])=="'.get_class($object).'"){ $object = $args[0]; return; } if(!is_null($object)){ return call_user_func_array(array($object,"'.$method_name.'"),$args); }else{ return FALSE; }' ); //Initialize static $object $func($object); //Return closure return $func; }else{ return false; } } ?> Here is how you would use it: <?php class foo{ public function bar($foo){ return strtolower($foo); } } $foo = new foo(); $f = get_method_closure($foo,'bar'); echo $f("BAR");//Prints 'bar' ?> ---- Server IP: 201.216.232.3 Probable Submitter: 190.220.9.210 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.reflection.php Edit -- https://master.php.net/note/edit/90964 Del: integrated -- https://master.php.net/note/delete/90964/integrated Del: useless -- https://master.php.net/note/delete/90964/useless Del: bad code -- https://master.php.net/note/delete/90964/bad+code Del: spam -- https://master.php.net/note/delete/90964/spam Del: non-english -- https://master.php.net/note/delete/90964/non-english Del: in docs -- https://master.php.net/note/delete/90964/in+docs Del: other reasons-- https://master.php.net/note/delete/90964 Reject -- https://master.php.net/note/reject/90964 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#155701) next »