简体   繁体   English

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

[英]Can't set any class/member variables in PHP?

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.
    }
}

This is my issue. 这是我的问题。 In the "post" class, I can dump out $j and $id and see that they are both passed perfectly. 在“post”类中​​,我可以转储$ j和$ id并看到它们都完美地传递。 However, when I try to set $this->ID = $id or $this->dbase = $j, nothing is set. 但是,当我尝试设置$ this-> ID = $ id或$ this-> dbase = $ j时,没有设置任何内容。 Why would this be? 为什么会这样?

try: 尝试:

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.
    }
}

Because post class has no property 因为帖子类没有属性

Try this 尝试这个

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.
    }
}

See $test->ID or $test->dbase 请参阅$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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM