关于PHP下划线命名约定(如"_method"或"_property"中的) [英] About PHP underscore naming convention (as in "_method" or "_property")

查看:81
本文介绍了关于PHP下划线命名约定(如"_method"或"_property"中的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直想知道的一种一般性询问.我已经通过其他人的代码注意到了很多,但从来没有真正知道真正的原因,只是跟随趋势,所以去了.

为什么某些方法和属性的前面加一个下划线,而其他方法和属性却没有呢? 例如,什么时候专门使用function _method(),什么时候使用function method(),换句话说,private $_someVariableprivate $someVariable?

解决方案

在大多数情况下,这是PHP4的回溯约定,不支持属性或方法的可见性,并且库开发人员使用_来表示应注意的事项被认为是私有的,而不是直接从课堂之外访问. PHP5确实具有可见性,但是约定仍然经常得到维护.

This is a sort of general inquiry I've been wondering about. I've noticed a lot of this through other people's code, and never really knew the actual reason, just followed the trends, so here goes.

How come some methods and properties are named with an underscore in front, and others aren't? For example, when specifically would one use function _method(), and when would one use function method(), or, in other words, private $_someVariable vs. private $someVariable?

解决方案

Most of the time, it's a throwback convention to PHP4 which didn't support visibility for properties or methods, and library developers used the _ to indicate something that should be considered private, and not to be accessed directly from outside of the class. PHP5 does have visibility, but the convention is still often maintained.

这篇关于关于PHP下划线命名约定(如"_method"或"_property"中的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆