当调用类中不存在的函数时自定义错误消息

问题描述:

我在GitHub上有一个类(link)*,如果指定的类不存在,我想输出自定义错误消息。这可能吗?例如:当调用类中不存在的函数时自定义错误消息

用户试图调用函数123456()和不存在(main::123456()),它不存在,输出错误消息:

对不起,该功能123456不存在或为除去。

这可能吗?

*链接并不再存在

您可以通过重写魔术方法__call()这样做。当你这样做时,你需要必须为提供两种方法(例如,$name$args),否则这将不起作用。

class MyClass { 
    public function __call($name, $arguments) { 
     throw new Exception("failed to call method ".$name); 
    } 
    public function __callStatic($name, $arguments) { 
     throw new Exception("failed to call static method ".$name); 
    } 
}