49

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

0

7 Answers 7

61

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.

3
  • A more complete quote: "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." source Commented Nov 6, 2014 at 13:48
  • 3
    i mean don't they also advocate putting { on new lines all unto themselves? gross..
    – Eva
    Commented Dec 11, 2014 at 11:21
  • 3
    @Eva Only for method and class declarations. For loops and conditionals, etc, they recommend placing them at the end of the line after a space: php-fig.org/psr/psr-2 I personally find this much easier to read than placing all braces at the end of the line. Commented Sep 29, 2015 at 15:00
47

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.

4
  • 2
    Neither Java nor C# require this. I believe they recommend it, but "static public" is valid in both languages. (I've just compiled a test program to check.)
    – Jon Skeet
    Commented Apr 16, 2009 at 18:41
  • 2
    (Undone my downvote though, as the rest is fair enough - and I certainly agree that "public static" looks more reasonable.)
    – Jon Skeet
    Commented Apr 16, 2009 at 18:43
  • i think whatever you use, just stay consistent. then no one will care what order you use. Commented Jun 15, 2013 at 11:18
  • C++ does require the access modifier first because it has a slightly different syntax: public: static. Java and C# are both modelled on C++, even though they changed the syntax a little in this case to the remove the colon. I think that's why it's the norm to write public static in these languages. Commented Jun 27, 2023 at 6:41
15

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 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.

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)

Try this test.

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

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

and how many static methods are there in Class B?

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

I think class B is much easier to understand quickly.

1
  • @aW-k9IdI'-I0llwlg'I - I don't understand what you mean. You cannot invert the argument for access modifiers - "All methods ought to have their visibility specified. Only some methods have an indication of static status".
    – JW.
    Commented Jan 21, 2013 at 21:17
9

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.

6

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

1
  • I was going to write this as an answer. I totally agree.
    – JW.
    Commented Apr 1, 2012 at 16:42
5

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

2

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.

1
  • 3
    I've considered omitting 'public' since it's technically redundant, but I find it's better to be explicit. That way when I come back to my code six months later, I don't have to wonder why there's no access keyword on method foo(); did I just forget to put one there, or did I mean for it to be public?
    – dirtside
    Commented May 7, 2009 at 17:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.