class A { protected $a; public function __construct() { $this->a = 1; } } class B extends A { public $a; public function __construct() { parent::__construct(); } public function getA() { return $this->a; } } $b = new B(); echo $b->getA();
子类的属性要更加开放,父类中最好别定义public
子类中要使用父类中__construct()变更,__construct()不能为空
__construct()
Last updated 3 years ago