我在GitHub上有一个类(链接)*,如果指定的类不存在,我想输出一条自定义错误消息。这可能吗?例如:
用户试图调用函数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);
}
}