views:

519

answers:

4

In PHP5, what is the difference between using const and static? When is each appropriate? And what role does public, protected and private play - if any.

+8  A: 

In the context of a class, static variables are on the class scope (not the object) scope, but unlike a const, their values can be changed.

class ClassName {
    static my_var = 10;  /* defaults to public unless otherwise specified */
    const MY_CONST = 5;
}
echo ClassName::$my_var;   // returns 10
echo ClassName::MY_CONST;  // returns 5
ClassName::$my_var = 20;   // now equals 20
ClassName::MY_CONST = 20;  // error! won't work.

Public, protected, and private are irrelevant in terms of consts; they are only useful for class variables, including static variable.

  • public static variables can be accessed anywhere via ClassName::$variable.
  • protected static variables can be accessed by the defining class or extending classes via ClassName::$variable.
  • private static variables can be accessed only by the defining class via ClassName::$variable.
Matt Huggins
I prefer to use `self::$variable` for protected static and private static variables since I prefer to keep the class name mentioned only once within itself which is at the very beginning of the class.
Lukman
Yes, good point, I neglected to mention that the self keyword can be used if referencing from within the class itself. The examples I provided above were performed outside the class definition, in which case the class name must be used.
Matt Huggins
Great answer, very close to accepting. Could you please clarify one point: "Public, protected, and private are irrelevant in terms of consts" - Why? Are consts by default all public? all private?
Chris Jacob
+1  A: 

Declaring a class method or property as statis makes them accessible without needing an instantiation of the class.

A class constant is just like a normal constant, it cannot be changed at runtime. This is also the only reason you will ever use const for.

Private, public and protected are access modifiers that describes who can access which parameter/method.

Public means that all other objects gets access. Private means that only the instantiated class gets access. Protected that the instantiated class and derived classes gets access.

alexn
A: 

Constant is just a constant, i.e. you can't change its value after declaring.

Static variable is accessible without making an instance of a class and therefore shared between all the instances of a class.

Also, there can be a static local variable in a function that is declared only once (on the first execution of a function) and can store its value between function calls, example:

function foo()
{
   static numCalls = 0;
   numCalss++;
   print('this function has been executed " . numCalls . " times");
}
Mike Borozdin
A: 

One last point that should be made is that a const is always static and public. This means that you can access the const from within the class like so:

class MyClass
{
     const MYCONST = true;
     public function test()
     {
          echo self::MYCONST;
     }
}

From outside the class you would access it like this:

echo MyClass::MYCONST;
luoshiben