php “公共静态”还是“静态公共”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/757424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"public static" or "static public"?
提问by dirtside
A minor point about function declaration keywords in PHP: If you've got a class method that's static, should the statickeyword come before or after the visibility keyword (public, protected, private)? Assuming all your methods, static or otherwise, have a visibility keyword, then you'd want the visibility keyword to remain in the same place relative to the functionkeyword:
关于 PHP 中函数声明关键字的一个小问题:如果您有一个静态的类方法,那么static关键字应该放在可见性关键字 ( public, protected, private)之前还是之后?假设您的所有方法,无论是静态的还是其他方式,都有一个可见性关键字,那么您希望可见性关键字相对于function关键字保持在相同的位置:
public function foo() {}
public function bar() {}
protected function baz() {}
private function quux() {}
Now pretend a couple are static:
现在假装一对是静态的:
public function foo() {}
static public function bar() {}
protected function baz() {}
static private function quux() {}
Also, if a method is static, you want that to be the firstthing seen, because that has more of an impact on what kind of method it is than even the visibility keyword does.
此外,如果一个方法是静态的,您希望它是第一个看到的东西,因为这对它是哪种方法的影响甚至比可见性关键字的影响更大。
This is strictly a readability issue, as it obviously has no functional or design consequences. (That I can think of.)
这严格来说是一个可读性问题,因为它显然没有功能或设计后果。(我能想到的。)
回答by PressingOnAlways
From PSR-2:
从 PSR-2:
Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility. [reference]
必须在所有属性和方法上声明可见性;abstract 和 final 必须在可见性之前声明;static 必须在可见性之后声明。[参考]
...if you are one to care about the PHP Framework Interop Group standard and conventions.
...如果您关心 PHP 框架互操作组标准和约定。
So public staticnot static publicaccording to them.
所以public static不是static public根据他们。
回答by Andrew Hare
Languages like Java and C# require that the access modifier come first soEdit:The previous struck line is completely false. Neither language has this requirement.
Java 和 C# 等语言要求访问修饰符排在第一位,因此编辑:之前的划线是完全错误的。两种语言都没有这个要求。
public static
looks correct to me. Arguments can be made for both approaches and mine is this: Since "static" qualifies the function rather than the access modifier it makes more sense to say
对我来说看起来是正确的。可以为这两种方法提出论点,我的是这样的:由于“静态”限定了函数而不是访问修饰符,所以说更有意义
<access_modifier> static
If you use it the other way around the meaning of "static" is less clear.
如果您以相反的方式使用它,“静态”的含义就不太清楚了。
回答by JW.
Further to Alexei Tenitski's answer.
进一步了解阿列克谢·特尼茨基 (Alexei Tenitski) 的回答。
I prefer static public since this way
it is easier to spot [usually rare] static methods in classes.
All methods ought to have their visibility specified. So, we know that every method is going to have that mentioned somehere in the definition, the only question is "Which setting is it?".
所有方法都应该指定其可见性。所以,我们知道每个方法都会在定义中的某个地方提到,唯一的问题是“它是哪个设置?”。
Only some are static - so, for each one we have to ask "Is there a mention of the static keyword somewhere in the definition?". So, put static first to make the answer to that question more obvious.
只有一些是静态的——所以,对于每一个,我们都必须问“在定义的某处是否提到了 static 关键字?”。因此,将静态放在首位以使该问题的答案更加明显。
Or, as a wider rule , ......... I tend to put 'the most extraordinary aspect first' so that I don't don't subsconsciously skip over things when reading them. ;o)
或者,作为更广泛的规则,…………我倾向于将“最非凡的方面”放在首位,以免在阅读时下意识地跳过某些内容。;o)
Try this test.
试试这个测试。
Very quickly...How many static methods are there in Class A?
很快……A类有多少静态方法?
class A {
public static methodA() {
}
protected static methodB() {
}
private staticlymethodC() {
}
}
and how many static methods are there in Class B?
B类中有多少静态方法?
class B {
public methodA() {
}
static protected methodB() {
}
static private methodC() {
}
}
I think class B is much easier to understand quickly.
我认为 B 类更容易快速理解。
回答by Greg D
I don't think that this is a strictly PHP question, and for what little it's worth, I've always preferred the consistency of placing the visibility modifier first. I find it easier to scan.
我不认为这是一个严格的 PHP 问题,而且对于它的价值,我一直更喜欢将可见性修饰符放在首位的一致性。我觉得扫描更容易。
回答by chaos
I put visibility first in every language I use that has type modifiers.
我将可见性放在我使用的每种具有类型修饰符的语言中。
回答by Alexei Tenitski
I prefer static publicsince this way it is easier to spot [usually rare] static methods in classes.
我更喜欢static public这样,因为这样更容易在类中发现 [通常很少见] 静态方法。
回答by thomasrutter
You are correct in that it has no effect on the code. Therefore it is up to your own style requirements, or those of your team, as to what you do. Consult with them and agree on a style.
您是正确的,因为它对代码没有影响。因此,这取决于你自己的风格要求,或者你的团队的要求,关于你做什么。与他们协商并就一种风格达成一致。
If you are coding for yourself only, then you should choose for yourself. The choice is not important, but consistency is.
如果您只是为自己编码,那么您应该为自己选择。选择并不重要,但一致性很重要。
Another question you may ask is: should you use 'public' or not? For backwards compatibility (PHP4 had no information hiding) anything without a visibility modifier is public by default. Should you bother writing public if it's public? Again personal choice: make a strong argument either way and you'll convince me your choice is best.
您可能会问的另一个问题是:您是否应该使用“public”?为了向后兼容(PHP4 没有信息隐藏),没有可见性修饰符的任何东西默认都是公开的。如果它是公开的,你应该打扰公开吗?再次个人选择:无论哪种方式都提出强有力的论点,你会说服我你的选择是最好的。
Personally, when I go through and clean up my own code, I like to put the visibility modifier first, and specify it even if it's public.
就我个人而言,当我检查并清理自己的代码时,我喜欢将可见性修饰符放在首位,即使它是公开的也要指定它。

