user745668
user745668

Reputation: 111

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. However, when I try to set $this->ID = $id or $this->dbase = $j, nothing is set. Why would this be?

Upvotes: 0

Views: 845

Answers (3)

Gabriel Santos
Gabriel Santos

Reputation: 4974

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

Upvotes: 0

bitoshi.n
bitoshi.n

Reputation: 2318

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

Upvotes: 0

mgraph
mgraph

Reputation: 15338

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

Upvotes: 2

Related Questions