note 90964 modified in language.oop5.reflection by danbrown

From: Date: Wed, 16 Sep 2009 16:08:17 +0000
Subject: note 90964 modified in language.oop5.reflection by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160704@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' ?> --was-- 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' ?> http://php.net/manual/en/language.oop5.reflection.php

« previous php.notes (#160704) next »