2014-01-16 109 views
-1

我不明白爲什麼以下不適合我。我複製了我的一些教程中,但在運行程序時,我得到一個錯誤:php:在構造函數中訪問類變量

class tc { 
    private $test = NULL; 
    public function __construct(){ 
     $this->$test = 'sdfsdf'; 
    } 
} 

創建對象:

$test = new tc; 

Undefined variable: test 
Cannot access empty property.. 

我知道這是很基本的,但不明白是什麼我做錯了。

+0

嘗試'$ this-> test'(正如Abra指出的那樣)。我看到它之前評論。 – Dutchie432

+0

另外,也許不要使用這些教程:-) – AbraCadaver

回答

3
$this->test = 'sdfsdf'; 
//not 
$this->$test = 'sdfsdf'; 

注意號碼$

+0

哇。它結束了一天,我很累。這是我的藉口。 – red888

+2

註銷,回家,喝啤酒...... – AbraCadaver

+0

@AbraCadaver也許問題是他已經在喝啤酒了? (開玩笑) –

相關問題