note 98012 deleted from function.call-user-method-array by danbrown

From: Date: Thu, 20 May 2010 15:41:29 +0000
Subject: note 98012 deleted from function.call-user-method-array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169453@lists.php.net to get a copy of this message
Note Submitter: kai at froghh dot de ---- Small, but necessary change: Remember: eval returns null, unless return is called within eval'd string. So check, that result is not false (bool). Hint: Using classes that gets functionality from external sources, i.e. SoapClient, which gets functions from wdsl, the method_exists -check in the beginning will fail. Maybe you want to add a parameter to the function to supress the check that method exists. <?php /** * @param string $func - method name * @param object $obj - object to call method on * @param boolean|array $params - array of parameters */ function call_object_method_array($func, $obj, $params=false){ if (!method_exists($obj,$func)){ // object doesn't have function, return null return (null); } // no params so just return function if (!$params){ return ($obj->$func()); } // build eval string to execute function with parameters $pstr=''; $p=0; foreach ($params as $param){ $pstr.=$p>0 ? ', ' : ''; $pstr.='$params['.$p.']'; $p++; } $evalstr='$retval=$obj->'.$func.'('.$pstr.');'; $evalok=eval($evalstr); // if eval worked ok, return value returned by function if ($evalok !== false){ return ($retval); } else { return (null); } return (null); } ?>

« previous php.notes (#169453) next »