[ACCEPTED]-public static" or "static public"?-public-method

Accepted answer
Score: 57

From PSR-2:

Visibility MUST be declared on 6 all properties and methods; abstract and 5 final MUST be declared before the visibility; static 4 MUST be declared after the visibility. [reference]

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

So 1 public static not static public according to them.

Score: 46

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

public static

looks 6 correct to me. Arguments can be made for 5 both approaches and mine is this: Since 4 "static" qualifies the function rather than 3 the access modifier it makes more sense 2 to say

<access_modifier> static

If you use it the other way around 1 the meaning of "static" is less clear.

Score: 13

Further to Alexei Tenitski's answer.

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

All methods ought to have 17 their visibility specified. So, we know 16 that every method is going to have that 15 mentioned somehere in the definition, the 14 only question is "Which setting is 13 it?".

Only some are static - so, for 12 each one we have to ask "Is there a 11 mention of the static keyword somewhere 10 in the definition?". So, put static 9 first to make the answer to that question 8 more obvious.

Or, as a wider rule , ......... I 7 tend to put 'the most extraordinary aspect 6 first' so that I don't don't subsconsciously 5 skip over things when reading them. ;o)

Try 4 this test.

Very quickly...How many static 3 methods are there in Class A?

class A {
 public static methodA() {
 protected static methodB() {
 private staticlymethodC() {

and how many 2 static methods are there in Class B?

class B {
 public methodA() {
 static protected methodB() {
 static private methodC() {

I think 1 class B is much easier to understand quickly.

Score: 9

I don't think that this is a strictly PHP 4 question, and for what little it's worth, I've 3 always preferred the consistency of placing 2 the visibility modifier first. I find it 1 easier to scan.

Score: 6

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

Score: 5

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

Score: 2

You are correct in that it has no effect 17 on the code. Therefore it is up to your 16 own style requirements, or those of your 15 team, as to what you do. Consult with them 14 and agree on a style.

If you are coding for 13 yourself only, then you should choose for 12 yourself. The choice is not important, but 11 consistency is.

Another question you may 10 ask is: should you use 'public' or not? For 9 backwards compatibility (PHP4 had no information 8 hiding) anything without a visibility modifier 7 is public by default. Should you bother 6 writing public if it's public? Again personal 5 choice: make a strong argument either way 4 and you'll convince me your choice is best.

Personally, when 3 I go through and clean up my own code, I 2 like to put the visibility modifier first, and 1 specify it even if it's public.

More Related questions