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 に答える