从成员函数访问PHP私有数组

Ale*_*x_B 0 php class

当我运行这段代码时,我只会打印出“在构造函数中”。

为什么我看不到阵列被打印出来?

Apache日志未显示任何错误。PHP语法检查器未显示任何错误。

<?php
//---- User Class ----      
class User {
    private $list;

    function __construct() { 
        echo "in constructor";
        $this->$list = array(1, 2, 5);
        }

    function printAll() {
        print_r($this->$list);
    }

}   // end Class  

$foo = new User(); 
$foo->printAll();
?>
Run Code Online (Sandbox Code Playgroud)

sil*_*lly 5

一美元,尝试一下

当我运行这段代码时,我只会打印出“在构造函数中”。

为什么我看不到阵列被打印出来?

Apache日志未显示任何错误。PHP语法检查器未显示任何错误。

class User {
    private $list;

    function __construct() { 
        echo "in constructor";
        $this->list = array(1, 2, 5);
        }

    function printAll() {
        print_r($this->list);
    }
}
Run Code Online (Sandbox Code Playgroud)