0
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 时,没有设置任何内容。为什么会这样?

4

3 回答 3

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.
    }
}
于 2012-05-11T17:45:36.617 回答
0

因为 post 类没有属性

试试这个

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.
    }
}
于 2012-05-11T17:47:30.483 回答
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');
于 2012-05-11T17:48:45.407 回答