__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();
}
}
}
しかし、 forの代わりにthrow
エラーメッセージが表示されるため、結果は私が望むものではありません。("Property $name is not defined")
null
$property->bla
、などに対してのみエラー メッセージをスローするようにするにはどうすればよいですか?$property->bla->bla
$property->bla->bla->bla