不覆盖属性子构造函数

时间:2018-04-10 01:43:14

标签: php

如何使用构造函数[php]覆盖子类中父类中声明的属性。

前:

class A {
    public $user;
    public function __construct {
        $this->user = 5;
    }
}

class B extends A {
    public function __construct {
        //How do I make sure that the variables from the parent object are inherited?
    }
}

1 个答案:

答案 0 :(得分:0)

覆盖父母的构造函数

parent::__construct(); //add this in the child's construct() function 

父类和子类的__construct()语法都需要括号

public function __construct() {


}