purencool Posted February 6, 2012 Share Posted February 6, 2012 Hi PhpFreaks, I have a method that I need to be initialised dynamically and I am not sure if the code below will work I have been able to workout if the method exists but I have not found a way to it get to return the array. Has anyone go any ideas? The error I get is: Catchable fatal error: Object of class Forms could not be converted to string public function setMethodForms($methodName){ echo $methodName." and does it exist ". (int)method_exists($this->formsObj, $methodName); //test to see if the method exists if ((int)method_exists($this->formsObj, $methodName) == 1){ //if it does execute //print_r ($this->formsObj->projects()); //test to see if object works $return call_user_func($this->formsObj."->".$methodName."()"); } return $return; } Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/ Share on other sites More sharing options...
spiderwell Posted February 6, 2012 Share Posted February 6, 2012 I think you need eval to run it on the fly public function setMethodForms($methodName){ echo $methodName." and does it exist ". (int)method_exists($this->formsObj, $methodName); //test to see if the method exists if ((int)method_exists($this->formsObj, $methodName) == 1){ //if it does exist eval ('$arrProjects = $this->formsObj->' . $methodName . '()'); $return $arrProjects; } return false; } i haven't checked the code to test it for typos, but you have to evalulate the code, pass the response to a variable called $arrProjects which is then returned. Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315036 Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 eval is almost always the wrong tool. $return call_user_func_array(array($this->formsObj, $methodName)); Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315045 Share on other sites More sharing options...
spiderwell Posted February 6, 2012 Share Posted February 6, 2012 Can you tell me why Thorpe? being a self taught person, I don't always pick up on whats the best methods for things. Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315053 Share on other sites More sharing options...
trq Posted February 6, 2012 Share Posted February 6, 2012 Most of us are self taught. Anyway, eval can be very dangerous as it can open a hole where a user might be able to execute arbitrary code. Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315055 Share on other sites More sharing options...
silkfire Posted February 6, 2012 Share Posted February 6, 2012 eval() is handy when you want to calculate a string, i.e '(43 - 27) * 5 + 3 / 0.023' instead of having a hoarde of switch / case statements. Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315060 Share on other sites More sharing options...
purencool Posted February 7, 2012 Author Share Posted February 7, 2012 Thanks for all your feedback. I found a way that is very clean. This is the completed code it gets an array of objects finds the method that then executes it. public function getMethodForms($methodName){ foreach ($this->formsObj as $key => $Obj){ //test to see if the method exists if ((int)method_exists($Obj, $methodName) == 1){ //if it does execute $return= $Obj->{$methodName}(); } } //print_r($return); return $return; } Quote Link to comment https://forums.phpfreaks.com/topic/256515-getting-methods-to-work-dynamically/#findComment-1315289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.