简体   繁体   中英

Trying to access an object in a class extended from another class

I'm Trying to access an object in a class extended from another class. But I'm getting the error "Trying to get property of non-object".

This is the code: http://3v4l.org/ZLTWf#v500

why can i not access the $doo->var_2->var ? i works fine with $foo->var_2->var (as it should) but why do i get an error when i extend the class? Is it allowed in PHP? Is there some extra code that will allow me to do that?

I'm even getting an "Undefined variable" error on $doo->$var_3

Problem is here

class TheClass extends SecondClass{
    public $var_3;
    function __construct(){
        $this->var_3 = "Some Text...";
    }
}

You have overwritten the parent constructor so when you instantiate the class the constructor of its parent will not be called since its overwritten.

You can fix it as

class TheClass extends SecondClass{
    public $var_3;
    function __construct(){
        parent::__construct();
        $this->var_3 = "Some Text...";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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