Home > php教程 > php手册 > body text

PHP中private和public还有protected的区别

WBOY
Release: 2016-06-07 11:34:45
Original
784 people have browsed it

public 表示全局,类内部外部子类都可以访问;
private表示私有的,只有本类内部可以使用;
protected表示受保护的,只有本类或子类或父类中可以访问;

         <br>     //父类<br>     class father{<br>      public function a(){<br>       echo "function a";<br>      }<br>      private function b(){<br>       echo "function b";<br>      }<br>      protected function c(){<br>       echo "function c";<br>      }<br>     }<br>     //子类<br>     class child extends father{<br>       function d(){<br>         parent::a();//调用父类的a方法<br>       }<br>       function e(){<br>        parent::c(); //调用父类的c方法<br>       }<br>      function f(){<br>         parent::b(); //调用父类的b方法<br>       }<br>     }<br>     $father=new father();<br>     $father->a();<br>     $father->b(); //显示错误 外部无法调用私有的方法 Call to protected method father::b()<br>     $father->c(); //显示错误 外部无法调用受保护的方法Call to private method father::c()<br>     $chlid=new child();<br>     $chlid->d();<br>     $chlid->e();<br>     $chlid->f();//显示错误 无法调用父类private的方法 Call to private method father::b()<br>     ?>以上是自己对private和public还有protected 三者的个人理解。

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template