我通过利用__get魔法方法来定义当一个不存在的属性被访问时会发生什么。
所以如果$property->bla不存在我会得到null。
return (isset($this->$name)) ? $this->$name : null;
$property->bla->bla但是当我知道$property->bla不存在时,我想抛出并捕获错误。
我会在return (isset($this->$name)) ? $this->$name : null;下面得到这个错误,
<b>Notice</b>: Trying to get property of non-object in...
所以我throw and catch在课堂上使用错误,
类属性 {
public function __get($name)
{
//return (isset($this->$name)) ? $this->$name : null;
try {
if (!isset($this->$name)) {
throw new Exception("Property $name is not defined");
}
return $this->$name;
}
catch (Exception $e) {
return $e->getMessage();
}
}
}
但结果不是我想要的,因为它throw是错误消息("Property $name is not defined")而不是nullfor $property->bla。
我怎样才能让它只为$property->bla->bla,$property->bla->bla->bla等抛出错误消息?