Why a protected variable of parent class come empty?

后端 未结 2 951
萌比男神i
萌比男神i 2021-01-25 18:02

I got a protected variable in Father class, the content of this variable will change in Father class but I need to use this variable in sub classes, i.

2条回答
  •  不要未来只要你来
    2021-01-25 18:51

    This is because you're overriding the constructor function. You must call the parent's constructor as well. More info

    class Child extends Father {
        function __construct() {
            parent::__construct();
    
            echo $this->body;
        }
    }
    

提交回复
热议问题