Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"public static" or "static public"?

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

like image 434
dirtside Avatar asked Apr 16 '09 18:04

dirtside


People also ask

Is it public static or static public?

There's no difference. You're free to specify them in either order. However, I find that most developers tend to use public static and not static public.

Can we write static public instead of public static?

If you write static public void instead of public static void then it is perfectly OK. Your Java program will compile and run successfully. It doesn't really make any difference as long as method name comes last and return type of method comes second last.

Can we use static public?

Use public static methods if the method can be considered a unit, and can be effectively tested on its own. It's plain hard to implement dependency injection or mocks on types that use static method. I use static methods for utility methods with few/no dependencies, and well defined input/output.

What is difference between public method and public static method?

public methods and properties are accessible only after instantiating class and is called via "->" sign. public static methods and properties can be accessed without need of instantiating class and can be called via "::".


3 Answers

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.

like image 162
PressingOnAlways Avatar answered Sep 20 '22 07:09

PressingOnAlways


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.

like image 23
Andrew Hare Avatar answered Sep 22 '22 07:09

Andrew Hare


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.

like image 26
JW. Avatar answered Sep 19 '22 07:09

JW.