类的变量

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();
  1. 子类的属性要更加开放,父类中最好别定义public

  2. 子类中要使用父类中__construct()变更,__construct()不能为空

Last updated