phpfunctionclassoop

The difference between "public" and "public static"?


What does static mean?

I know public means that it can be accessed from outside the class, and private only from inside the class…


Solution

  • Static means that it can be accessed without instantiating a class. This is good for constants.

    Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.

    Modifiable static variables are risky. They act like a global variable, which can make the application fragile. Tracing where the variable was modified can be difficult.

    Static methods are not risky. They can replace repeated code, increasing the likelihood the code will be well tested and correct.