【发布时间】:2014-12-26 02:25:47
【问题描述】:
我想问这是否可能:
我有一个带有私有构造函数的类,如果我尝试从无效上下文(类外部)实例化该类,它当然会引发致命错误,我可以捕获这个致命错误和类的名称然后抛出像这样的例外:
// catch the private constructor from invalid context fatal error
// and store somehow the name of the class inside $class
// and then:
throw new UninstantiableClassException("The class " . $class . " cannot be instantiated");
这可能吗,或者根据这篇文章我刚刚发现它不是? -> PHP : Custom error handler - handling parse & fatal errors
如果不可能,我是否应该将构造函数公开,然后像这样在其中抛出异常?:
public function __construct() {
throw new UninstantiableClassException("The class " . get_class($this) . " cannot be instantiated");
}
【问题讨论】:
-
其实我不认为这是一个重复的问题,我已经阅读了@user259973 对该帖子的回答。在他的回答中,他说应该使用 register_shutdown_function,但是即使调用
register_shutdown_function(),在调用 register_shutdown_function() 之前,致命错误仍然会出现在屏幕上,所以我的问题是是否有一种方法可以使该致命错误不会在出现问题时出现在屏幕上,而是制作一个自定义的致命错误处理程序......希望你能理解。
标签: php exception private fatal-error