Reasons to avoid access modifiers in php [closed]

六眼飞鱼酱① 提交于 2019-11-28 08:49:43

The private modifier is - imho - vastly overused. The problem with it is that it makes it impossible to extend classes. But more importantly, it is a concept that leads one to write code which is class-oriented, rather then object-oriented.

I have no beef with protected for properties. In fact, I think it should be the only scope used. protected methods are usually a hassle though, as it makes testing harder.

Gordon

valid reasons NOT to use keywords public, private, protected in php?

In a dynamic language like PHP, it is assumed that the programmer knows how the code works. That means the programmer knows which methods to call and which should not be called directly.

This is similar to untyped variables: in typed languages each variable is explicitly typed, but in PHP it is assumed that the programmer knows the type of each variable.

user187291

mario nailed it (copied from the comments)

Access modifiers make sense in Java/C++ and compiled code in general, where they are enforceable. In uncompiled scripting languages they can easily be ripped out. Hence they should be considered just decorators, and thus pragmatically could just be implemented as coding convention. (See underscoritis in Python, and pretty much any other scripting language. PHP is pretty alone with its purposeless access decorators.)

You won't have much success convincing your teammates about the advantage of useful APIs over encapsulation by restrictiveness. The use of syntax-enforced access decorators is often cargo cult driven.

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