[php]private public protected 三者之间关系

public 权限是最大的,可以内部调用,实例调用等;

private 私有类型,只有在本类中使用

protected 受保护类型,用于本类和继承类调用

  1. <?  
  2. //父类  
  3. class father{  
  4. public function a(){  
  5. echo "function a";   
  6. }  
  7. private function b(){  
  8. echo "function b";   
  9. }  
  10. protected function c(){  
  11. echo "function c";   
  12. }  
  13. }  
  14. //子类  
  15. class child extends father{  
  16. function d(){   
  17. parent::a();//调用父类的a方法  
  18. }  
  19. function e(){   
  20. parent::c(); //调用父类的c方法  
  21. }   
  22. function f(){   
  23. parent::b(); //调用父类的b方法  
  24. }   
  25.   
  26. }  
  27. $father=new father();  
  28. $father->a();  
  29. $father->b(); //显示错误 外部无法调用私有的方法 Call to protected method father::b()   
  30. $father->c(); //显示错误 外部无法调用受保护的方法Call to private method father::c()   
  31.   
  32. $chlid=new child();  
  33. $chlid->d();  
  34. $chlid->e();//可以调用受保护的方法 
  35. $chlid->f();//显示错误 无法调用父类private的方法 Call to private method father::b()   
  36. ?> 

 

上一篇: 三种编程命名规则(匈牙利法,小驼峰法,大驼峰法)
下一篇: 解决80端口被封LNMP无法安装Let's Encrypt SSL证书

发表评论