为什么不使用“受保护”? 或“私人” 在 PHP 中?
我一直在使用 Joomla 框架,我注意到他们使用约定来指定私有或受保护的方法(他们在方法名称前面添加下划线“_
”),但他们确实不显式声明任何方法 public
、private
或 protected
。 为什么是这样? 和便携性有关系吗? public
、private
或 protected
关键字在旧版本的 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
. Why is this? Does it have to do with portability? Are the public
, private
, or protected
keywords not available in older versions of PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
public、private 和 protected 是 PHP5 关键字。 不幸的是,PHP4 仍然拥有非常高的安装基础(尤其是在共享托管服务中)。
这是漂亮图片,显示 7 月份的使用率(文本为法语)。 剧透:遗憾的是,php4 的使用率仍然超过 35%。
public, private and protected are PHP5 keywords. unfortunately, PHP4 still has a very high install base (especially amongst shared hosting services).
here's a pretty pic showing july usage rates (text in french). spoiler: php4 still has over a 35% usage rate sadly.
这里有一些不使用
private
方法和属性的好论据:http:// aperiplus.sourceforge.net/visibility.php包括:使编码和阅读代码变得更加困难。 使重用类和调试变得更加困难。
无论如何,我都不会被说服,但我希望看到对该文章的知情反驳(如果存在的话)。
There's some good arguments for not using
private
methods and properties over here: http://aperiplus.sourceforge.net/visibility.phpIncludes: 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 在对象模型中引入了一些重大变化。 除了支持可见性之外,还有其他各种变化。 请务必查看:
PHP 4 类和对象
PHP 5 类和对象
PHP5 introduced some hefty changes in the object model. Among supporting visibility, there are various other changes. Be sure to check out:
PHP 4 classes and objects
PHP 5 classes and objects