<?phpclass _Parent{ //子类中无法访问父类private属性 protected可以 private $data = "parent"; protected $data2 = "protected data"; public function _print() { echo $this->data . "<br>"; } }class childA extends _Parent{}class childB extends _Parent{ protected $data = "childB"; // 覆盖父类的属性 // 覆盖父类的方法 public function _print() { echo $this->data . "<br>"; } }class childC extends _Parent{ public function other(){ // echo $this->data; 错误 不能访问父中 private属性 echo $this->data2; } //错误 无法重载方法 若要重写 必须参数和返回值相同 public function _print($d) { echo $this->data2 . $d; } } $A = new childA(); $B = new childB(); $C = new childC(); $A->_print(); $B->_print(); $C->other(); //错误 PHP不支持方法的重载 $C->_print(",hi"); ?>
The above is PHP Introduction 8 Object Oriented 1 Methods and Properties Override Access to the Contents of the Parent Class, More Related Please pay attention to the PHP Chinese website (m.sbmmt.com) for content!