我試圖使用魔術PHP函數將對象鏈接在一起。__isset不返回正確的結果
我有一個稱爲頁面的抽象類,並且我網站中的每個頁面都擴展了這個類。 在這種抽象類的構造函數我試圖讓用戶對象是這樣的:
public function __construct(){
$user = new user();
$this->user = $user->getIdentity();
$this->getHeader();
$this->switchAction();
$this->getFooter();
}
現在,在我的網頁,我可以使用$這 - >用戶,它工作一切正常。如果用戶登錄給我回的用戶對象 在我的用戶類,我有一個神奇的__get & __isset功能:
public function __get ($name){
switch ($name){
case 'oAlbums':
return $this->oAlbums = album::get_ar_obj($arWhere = array('user_id' => $this->id));
break;
public function __isset ($name){
switch($name){
case 'oAlbums':
echo 'magic isset called, trying to call '. $name . '<br />';
return isset($this->$name);
break;
}
}
因此,當在一個頁面中,我要檢查,如果用戶有任何全部通過致電$this->user->oAlbums
。 如預期的那樣,這將返回一個包含所有專輯對象的數組。但是,當我在我的網頁做
if(empty($this->user->oAlbums))
echo 'still give smepty';
,它仍然Echo的字符串了..
爲什麼不工作的__isset功能?
謝謝!!像魅力一樣工作 – 2013-05-01 21:03:17