“public static” or “static public”?

2019-02-05 10:54发布

A minor point about function declaration keywords in PHP: If you've got a class method that's static, should the static keyword 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 function keyword:

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 first thing 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.)

7条回答
The star\"
2楼-- · 2019-02-05 11:16

Languages like Java and C# require that the access modifier come first so Edit: The previous struck line is completely false. Neither language has this requirement.


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.

查看更多
唯我独甜
3楼-- · 2019-02-05 11:19

I prefer static public since this way it is easier to spot [usually rare] static methods in classes.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-05 11:24

I put visibility first in every language I use that has type modifiers.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-05 11:30

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.

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.

查看更多
贪生不怕死
6楼-- · 2019-02-05 11:38

From 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]

...if you are one to care about the PHP Framework Interop Group standard and conventions.

So public static not static public according to them.

查看更多
成全新的幸福
7楼-- · 2019-02-05 11:38

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.

查看更多
登录 后发表回答