在php中对属性或方法的访问控制,是在前面加public private protected等关键字来实现的,public修饰的类成员可以在任何地方访问,private修饰的类成员只能够被自身访问,protected修饰的类成员能被自身或其子类访问。
<code><span><span><?php </span><span><span>class</span><span>test</span>{</span><span>public</span><span>$public</span>=<span>'public'</span>; <span>private</span><span>$private</span>=<span>'private'</span>; <span>protected</span><span>$protected</span>=<span>'protected'</span>; <span>public</span><span><span>function</span><span>show</span><span>()</span>{</span><span>echo</span><span>$this</span>-><span>private</span>; } <span>public</span><span><span>function</span><span>show2</span><span>()</span>{</span><span>echo</span><span>$this</span>-><span>protected</span>; } } <span>$test</span>=<span>new</span> test(); <span>echo</span><span>$test</span>-><span>public</span>; <span>$test</span>->show(); <span>$test</span>->show2(); <span>// exit();</span><span>// echo $test->protected; //会产生一个致命错误</span><span>// echo $test->private; //也会产生一个致命错误 private属性只能在定义的类中访问</span><span>/** * */</span><span><span>class</span><span>test2</span><span>extends</span><span>test</span> {</span><span>//重写父类的show()方法</span><span><span>function</span><span>show</span><span>()</span>{</span><span>//继承自父类的protected</span><span>echo</span><span>$this</span>-><span>protected</span>; } } <span>$test2</span>=<span>new</span> test2(); <span>echo</span><span>"<hr>"</span>; <span>// $test2->protected; //会产生一个致命错误 protected属性不能在外部访问</span><span>$test2</span>->show(); <span>//能够访问 protected只能被自身或其子类访问</span><span>?></span></span></span></code>
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了php访问控制(public protected private),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Anintriguingthird-partycasefortheGalaxyS24Ultra(curr.$1,099.99onAmazon)hasappearedonmarketplaceslikeAliExpress.Astheimagesthroughoutthisarticleshow,thecasehasasimplesiliconeconstruction.However,italsocontainsanEInkd

In2012,theeminentautomotivejournalistJeremyClarksonstatedthattheLamborghiniAventador,whichhewasreviewingatthetime,wouldbeamongthelastcarstofeatureanaturallyaspiratedV12.Morethanadecadelater,V12-poweredsupercarsareal

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

在java中,private的意思为“私有的”,是一种访问控制修饰符,用于修饰类、属性和方法。用private修饰的类成员,只能被该类自身的方法访问和修改,而不能被任何其他类(包括该类的子类)访问和引用;因此,private修饰符具有最高的保护级别。

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具