Inside an object, I get a string and I have to know if there is a method with the same name in the current object I am in.
How do I do that?
views:
31answers:
4
+2
A:
Use method_exists
. Here's an example:
$methodname = 'asdf';
if(method_exists($this, $methodname)) {
// call_user_func(array($this, $methodname)); See comments
$this->{$methodname}();
}
scompt.com
2010-05-28 11:45:47
why `call_user_func` and not `$this->{$methodname}()`?
Itay Moav
2010-05-28 11:58:26
Because I didn't think of your suggestion. That would definitely also work and it looks like it might perform better: http://www.php.net/manual/en/function.call-user-func.php#64415
scompt.com
2010-05-28 12:04:12
A:
I'd say you have not properly designed your application, if you got to check that.
seva.lapsha
2010-07-12 01:55:51
Are you kidding me? The entire ZF is designed on parsing strings into method names. If PHP gives you a powerful tool like that, why wouldn't I use it?
Itay Moav
2010-07-12 13:52:24