search
Homephp教程php手册PHP中private和public还有protected的区别

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元

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.