PHP5 的 __get 在物件內找不到 method 或 member 時便會執行,如果我們直接在內部使用會怎樣?
<?php $t = new Test(); class Test { public function __construct() { echo $this->x."<br>"; echo 'isset:'.isset($this->x)."<br>"; echo 'empty:'.empty($this->x)."<br>"; } private function __get($var){ return $var; } } ?>
class Test {
public function __construct() { echo $this->x."<br>"; echo 'isset:'.isset($this->x)."<br>"; echo 'empty:'.empty($this->x)."<br>"; }
private function __get($var){ return $var; } } ?>
執行結果:
x isset: empty:1
雖然 $this->x 會回傳 __get 執行的結果,但是 $this->x 並不是實際存在的 member variable,所以在內部使用的時候不能因為 $this->x 會回傳資料而使用 empty (註) 來判別回傳的資料是否為空。
註: empty - Determine whether a "variable" is empty isset - Determine whether a "variable" is set