2010-05-28 61 views

回答

2

使用method_exists。下面是一个例子:

$methodname = 'asdf'; 
if(method_exists($this, $methodname)) { 
    // call_user_func(array($this, $methodname)); See comments 
    $this->{$methodname}(); 
} 
+1

为什么'call_user_func'而不是'$ this - > {$ methodname}()'? – 2010-05-28 11:58:26

+0

因为我没有想到你的建议。这肯定也会起作用,看起来它可能表现更好:http://www.php.net/manual/en/function.call-user-func.php#64415 – 2010-05-28 12:04:12

1
if (method_exists ($this, $methodName)) { 
    ... exists 
} 
相关问题