如果目標類別已實作 __call() 魔術方法,則無論您呼叫哪個方法,is_callable 都會永遠回傳 TRUE。
is_callable 不會評估您在 __call() 實作中的內部邏輯(這是合理的)。
因此,對於此類類別,每個方法名稱都是可呼叫的。
因此,說(如有人所說)是錯誤的
...is_callable 會正確判斷使用 __call 建立的方法是否存在...
範例
<?php
class TestCallable
{
public function testing()
{
return "I am called.";
}
public function __call($name, $args)
{
if($name == 'testingOther')
{
return call_user_func_array(array($this, 'testing'), $args);
}
}
}
$t = new TestCallable();
echo $t->testing(); echo $t->testingOther(); echo $t->working(); echo is_callable(array($t, 'testing')); echo is_callable(array($t, 'testingOther')); echo is_callable(array($t, 'working')); ?>