2017-05-24 33 views
-2

我也許是PHP使用過的最令人困惑的問題。請看下面的代碼:只有寫入順序中的最後一個變量

public $profile; 

public $account; 

    function __construct(){ 
    if(isset($_SESSION['uid'])){ 
     $this->$profile = $_SESSION['user_profile']; 
     $this->$account = $_SESSION['user_account']; 
     echo "<script> alert('".$this->$profile->forename."'); </script>"; //Shows nothing 
    }else{ 
     unset($_SESSION['user_profile']); 
     unset($_SESSION['user_account']); 
    } 
    } 

出於某種原因,似乎無論是$this->$profile將寫入或$this->$account會寫,但只有當它是最後一個。在上面的情況下,如果我將配置文件行移到帳戶行後面,它將被寫入。但是,在這種情況下並非如此。

兩個$_SESSION變量都是從SQL語句中檢索的對象,並且它們的分配是有效的,因爲直接訪問任一變量(例如$_SESSION['user_profile']->forename)都可以正常工作。

任何想法?謝謝。

+6

嘗試使用'$這個 - > profile'和'$這個 - > account',而不是'$此 - > $ profile', '$ this - > $ account' –

+1

well spotted @RyanHipkiss – Dale

+0

@RyanHipkiss這似乎工作。你知道爲什麼嗎?我認爲我以前沒有遇到過麻煩。感謝您的評論。 – user3642576

回答

2

您正在使用variable variables,但您的代碼看起來像您試圖訪問成員變量。

替換此:

$this->$profile 
$this->$account 

有了這個

$this->profile 
$this->account 
相關問題