有没有一种方法可以告诉PHP在我试图访问null
对象上的成员或方法时抛出异常?PHP处理异常空处理
例如为:
$x = null;
$x->foo = 5; // Null field access
$x->bar(); // Null method call
现在,我只得到了下面的错误不属于很好的处理:
PHP Notice: Trying to get property of non-object in ...
PHP Warning: Creating default object from empty value in ...
PHP Fatal error: Call to undefined method stdClass::bar() in ...
我想有而不是抛出一个特定的异常。这可能吗?
注册一个[全局错误处理程序](http://php.net/manual/en/function.set-error-handler.php)并抛出你自己的异常。 – Rob 2014-10-05 16:16:21