简体   繁体   English

为什么不在PHP中使用'protected'或'private'?

[英]Why not use 'protected' or 'private' in PHP?

I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore " _ " in front of the method name), but they do not explicitly declare any methods public , private , or protected . 我一直在使用Joomla框架,我注意到他们使用一个约定来指定私有或受保护的方法(它们在方法名称前加上一个下划线“ _ ”),但它们没有明确声明任何方法publicprivateprotected Why is this? 为什么是这样? Does it have to do with portability? 它与便携性有关吗? Are the public , private , or protected keywords not available in older versions of PHP? publicprivateprotected关键字在旧版本的PHP中不可用吗?

public, private and protected are PHP5 keywords. public,private和protected是PHP5关键字。 unfortunately, PHP4 still has a very high install base (especially amongst shared hosting services). 不幸的是,PHP4仍然具有非常高的安装基础(特别是在共享主机服务中)。

here's a pretty pic showing july usage rates (text in french). 这是一张漂亮的照片,显示7月的使用率(法文文本)。 spoiler : php4 still has over a 35% usage rate sadly. 剧透 :php4仍然有超过35%的使用率。

There's some good arguments for not using private methods and properties over here: http://aperiplus.sourceforge.net/visibility.php 在这里不使用private方法和属性有一些很好的论据: http//aperiplus.sourceforge.net/visibility.php

Includes: makes coding and reading code harder. 包括:使编码和阅读代码更难。 Makes re-using classes and debugging harder. 重新使用类和调试更难。

I'm not sold either way, but I would like to see an informed rebuttal of that article, if one exists. 我没有以任何方式出售,但我希望看到该文章的明确反驳,如果有的话。

PHP5 introduced some hefty changes in the object model. PHP5在对象模型中引入了一些重大变化。 Among supporting visibility, there are various other changes. 在支持可见性中,还有其他各种变化。 Be sure to check out: 一定要看看:

PHP 4 classes and objects PHP 4类和对象

PHP 5 classes and objects PHP 5类和对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM