tags:

views:

31

answers:

4

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?

A: 

Yeh yeh, I know, RTFM

http://www.php.net/manual/en/function.method-exists.php

Itay Moav
+1  A: 
if (method_exists ($this, $methodName)) {
    ... exists
}
Piotr Pankowski
+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
why `call_user_func` and not `$this->{$methodname}()`?
Itay Moav
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
A: 

I'd say you have not properly designed your application, if you got to check that.

seva.lapsha
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