Why not use 'protected' or 'private' in PHP?

折月煮酒 提交于 2019-12-19 17:42:31

问题


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?


回答1:


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.




回答2:


There's some good arguments for not using private methods and properties over here: 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.




回答3:


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



来源:https://stackoverflow.com/questions/196737/why-not-use-protected-or-private-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!