0

I get the following error Creating default object from empty value... each time I attempt to add a nested object.

class api {

    private $authentication=null;
    private $status=null;
    private $response=null;

    public function __construct($user, $token) {
        $this->status->version=2.3; 
        $this->authentication->user=$user;
        $this->authentication->token=$token;
    }

}

How can I accomplish this exact thing without getting this error?

I don't want to use arrays for this particular thing.

0

1 Answer 1

0

does this help you or am I misunderstanding...

public function __construct($user, $token) {
    $this->status = new stdClass(); // Here
    $this->status->version=2.3; 
    $this->authentication = new stdClass(); // & Here
    $this->authentication->user=$user;
    $this->authentication->token=$token;
}
Sign up to request clarification or add additional context in comments.

5 Comments

OK, my bad. Updated, but does it work?
Yes it does work. Thanks. But can you explain why I have to do this?
My understanding here is that $this->status and $this->authentication are set to null (not an object) so you have to cast them to an object first before assigning properties to them.
How would I cast them to objects?
Just handle it in the constructor as per my answer for convenience.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.