在PHP中无法设置任何类/成员变量?

时间:2012-05-11 17:42:38

标签: php object

class posts
{
    public function __construct($db)
    {
        $this->dbase = $db;
        $test = new post(1, $this->dbase);
    }
}

class post
{
    public function __construct($id, &$j)
    {

        $this->ID = $id;
        $this->dbase = $j;

        var_dump($this); // At this point, $this-ID and $this->dbase are both null.
    }
}

这是我的问题。在“post”类中​​,我可以转储$ j和$ id并看到它们都完美地传递。但是,当我尝试设置$ this-> ID = $ id或$ this-> dbase = $ j时,没有设置任何内容。为什么会这样?

3 个答案:

答案 0 :(得分:2)

尝试:

class post
{
    var ID;
    var dbase;
    public function __construct($id, &$j)
    {

        $this->ID = $id;
        $this->dbase = $j;

        var_dump($this); // At this point, $this-ID and $this->dbase are both null.
    }
}

答案 1 :(得分:0)

因为帖子类没有属性

试试这个

class post
{
    private $ID;
    private $dbase
    public function __construct($id, &$j)
    {

        $this->ID = $i;
        $this->dbase = $j;

        var_dump($this); // At this point, $this-ID and $this->dbase are both null.
    }
}

答案 2 :(得分:0)

请参阅 $test->ID$test->dbase

class posts
{
    public function __construct($db)
    {
        $this->dbase = $db;
        $test = new post(1, $this->dbase);

        echo($test->dbase); // Here you can get all infos
    }
}

class post
{
    public function __construct($id, $j)
    {

        $this->ID = $id;
        $this->dbase = $j;
    }
}

new posts('foo');