protected与private在构造函数选择执行时的差异[有点乱,请看实例]

<?php
class A{
function __construct(){
   $this->say();
   $this->hello();
  }
protected function say(){
 echo __CLASS__."[A]<br/>";
  }
private function hello(){
  echo "[a]hello<br/>";
  }
}

class B extends A{
function __construct(){
    parent::__construct();
    $this->say();
    $this->hello();
 }
 function say(){
 echo __CLASS__."[B]<br/>";
}
 function hello(){
  echo "[b]hello<br/>";
  }
}
$a=new B();
?>

output:

B[B]
[a]hello
B[B]
[b]hello

 

parent::__construct()执行时 say方法是子类的,而hello类是父类的,差异十分明显
原文地址:https://www.cnblogs.com/HKUI/p/3132123.html