2014-01-16 110 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也许问题是他已经在喝啤酒了? (开玩笑) –

相关问题