PHP 方法:getMyVariable() 与 myVariable()
我想知道用 PHP 编码时什么更好或更容易被接受。我被告知,在 Java 中,获取和设置变量的类方法应该以“get”和“set”为前缀。不过,我想知道的是我是否应该在常规 PHP 函数上使用这些前缀。
例如,要从会话变量中检索用户名,我要么getUsername()
或username()
有哪些优点和最佳实践。我知道使用“get”更容易助记,但它相当多余(特别是对于我不希望其他人阅读的个人项目),但为了良好的实践,我想把它做好。
当我这样做时,变量的正确命名约定是什么?下划线分隔还是驼峰式大小写?我环顾四周,看到了两者的混合。 Wordpress 倾向于在其函数名称中使用下划线,但许多其他网站表示驼峰式大小写是最好的。
I was wondering what is better or more accepted when coding in PHP. I was taught, in Java, that class methods to get and set variables should be prefixed with "get" and "set". What I want to know, though, is should I use these prefixes on regular PHP functions.
For example, to retrieve a username from a session variable I would either havegetUsername()
orusername()
What are the advantages and best practices. I know that using "get" is more mnemonic but it's rather redundant (especially for a personal project that I don't expect to be having other people read) but for the sake of good practice I would like to get it right.
While I'm at it, what is the proper naming convention for variables? Underscore-separated or camel-case? I've looked around and I've seen a mix of both. Wordpress tends to use underscores in their function names but a lot of other sites say camel-case is best.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我个人尽量远离 getter/setter。我更喜欢使用魔术方法,这样我就可以执行
$foo->myVar
而无需显式调用函数(我认为这使代码更具可读性)。话虽如此,在某些情况下我会使用显式的 getter 和 setter(基本上是在直接使用变量的结果不明确的情况下。就像我将魔术方法映射到内部数组并有其他需要访问的成员变量一样)。在这种情况下,我使用
$foo->getMyVar()
作为函数签名。恕我直言,如果您知道它是一个变量,那么
$foo->myVar()
就有意义。但是如果您看到$foo->show()
会发生什么。这是否意味着执行表演动作?或者这是否意味着获取显示变量的当前设置?我总是尝试用能够识别其功能的名称来命名所有方法。
$foo->var()
没有给出任何指示发生了什么。但$foo->showVar()
确实如此(至少更是如此)。至于命名约定,这完全取决于你读的是谁。我更喜欢驼色箱。但下划线没有问题。选择一个,并坚持下去。一致性比选择本身更重要......
I personally try to stay away from getters/setters. I prefer to use the magic methods so then I can do
$foo->myVar
without needing to explicitly call a function (I think it makes the code more readable).With that said, there are circumstances where I use explicit getters and setters (Basically in situations where the result would be ambiguous using the variables directly. Like if I map the magic methods to an internal array and have other member variables that need accessing). In those circumstances, I use
$foo->getMyVar()
as the function signature.IMHO,
$foo->myVar()
makes sense if you know it's a variable. But what happens if you see$foo->show()
. Does that mean to perform the show action? Or does that mean get the current setting for the show variable?I always try to name all methods with something that identifies what they do.
$foo->var()
doesn't give any indication to what's going on. But$foo->showVar()
does (At least moreso).As for naming convention, it's all about who you read. I prefer camel-case. But there's nothing wrong with underscores. Pick one, and stick with it. Consistency is more important than the choice itself...
一般来说,方法最好包含动词。
查看 PHP 的 PEAR 命名约定: http://pear.php.net /manual/en/standards.naming.php
他们鼓励 getter 方法使用“get”前缀。我将其修改为使用“is”作为布尔类属性的前缀。
PEAR 还鼓励使用驼峰式大小写(或“大写大写”)而不是下划线。
In general, it's good for a method to contain a verb.
Check out the PEAR naming conventions for PHP: http://pear.php.net/manual/en/standards.naming.php
They encourage the "get" prefix for getter methods. I'd amend this to say to use "is" as the prefix for boolean class properties.
PEAR also encourages camel-case (or "studly caps") instead of underscores.
我个人认为“get”前缀很有帮助。尽管这取决于您的 IDE 和偏好。如果您的 IDE 提供注释和工具提示,那么它可以通过简单的鼠标悬停来提醒您 myFunction 执行此或操作。
我还经常使用其他有用的前缀:
get
、set
、validate
(或只是val
)、<代码>解析和显示
。我发现在函数海洋中,小描述符有很大帮助。Personally I find the "get" prefix helpful. Though it depends on your IDE and preference. If your IDE provides comments and tooltips, then it can remind you with a simple mouseover that myFunction does this or that.
There are other useful prefixes that I frequently use:
get
,set
,validate
(or justval
),parse
, andshow
. I find that in a sea of functions, the little descriptors help greatly.首先
$_SESSION
是一个全局变量,你真的不需要它的 getter。根据更好的命名约定,最好使用 getUsername()。休息是个人选择或意见。最好选择适合您的命名约定并坚持下去。
first of all
$_SESSION
is a global variable, you really do not need a getter for it. It is better to use getUsername() as per better naming convention.Rest is personal choice or opinion. It's better to choose a naming convention which suits you and stick to it.
无论你最喜欢什么,只要你坚持下去。下划线风格更老派,更像 C,而驼峰风格显然受到 Java 的启发。不过,用下划线分隔会使你的名字更长。
Whatever you like best as long as your consistent with it. The underscore style is more old-school and C like while the camelcase style is obviously inspired by Java. Seperating with underscore makes your names longer though.
就我个人而言,我认为方法和变量名称越明确且易于解释越好。如果除了您之外的任何人都会查看代码,这一点非常重要。随着项目规模的扩大,它也会有所帮助。
另一个关键是一致性......选择一些东西并坚持下去。
Personally, I think the more explicit and easy to interpret the method and variable names are the better. This is very important if anybody other than you will look at the code. It also helps as the project grows larger.
The other key is consistency...pick something and stick with it.