简体   繁体   English

尝试访问从另一个类扩展的类中的对象

[英]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 这是代码:http: //3v4l.org/ZLTWf#v500

why can i not access the $doo->var_2->var ? 为什么我不能访问$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? 我可以与$foo->var_2->var (但应该如此),但是为什么在扩展类时出现错误? Is it allowed in PHP? 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 我什至在$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...";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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