public 表示全局,類內部外部子類都可以訪問;
private表示私有的,只有本類內部可以使用;
protected表示受保護的,只有本類或子類或父類中可以訪問;
//父類
classfather{
publicfunctiona(){
echo"function?a";
}
privatefunctionb(){
echo"function?b";
}
protectedfunctionc(){
echo"function?c";
}
}
//子類
classchildextendsfather{
functiond(){
parent::a();//調用父類的a方法
}
functione(){
parent::c();//調用父類的c方法
}
functionf(){
parent::b();//調用父類的b方法
}
}
$father=newfather();
$father->a();
$father->b();//顯示錯誤?外部無法調用私有的方法?Call?to?protected?method?father::b()
$father->c();//顯示錯誤?外部無法調用受保護的方法Call?to?private?method?father::c()
$chlid=newchild();
$chlid->d();
$chlid->e();
$chlid->f();//顯示錯誤?無法調用父類private的方法?Call?to?private?method?father::b()