Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5: const vs static

People also ask

Should I use const or static?

const is a constant value, and cannot be changed. It is compiled into the assembly. static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly ). So if the values are never changed, use consts.

Is const same as static?

const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that .

What is difference between const and static variable?

The const variable is basically used for declaring a constant value that cannot be modified. A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable.

Is a const always static?

A const object is always static . const makes the variable constant and cannot be changed.


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 (which are always public); 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.

Edit: It is important to note that PHP 7.1.0 introduced support for specifying the visibility of class constants.


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;

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 $numOfCalls = 0;
   $numOfCalls++;
   print("this function has been executed " . $numOfCalls . " times");
}

When talking about class inheritance you can differentiate between the constant or variable in different scopes by using self and static key words. Check this example which illustrates how to access what:

class Person
{
    static $type = 'person';

    const TYPE = 'person';

    static public function getType(){
        var_dump(self::TYPE);
        var_dump(static::TYPE);

        var_dump(self::$type);
        var_dump(static::$type);
    }
}

class Pirate extends Person
{
    static $type = 'pirate';

    const TYPE = 'pirate';
}

And then do:

$pirate = new Pirate();
$pirate::getType();

or:

Pirate::getType();

Output:

string(6) "person" 
string(6) "pirate" 
string(6) "person" 
string(6) "pirate"

In other words self:: refers to the static property and constant from the same scope where it is being called (in this case the Person superclass), while static:: will access the property and constant from the scope in run time (so in this case in the Pirate subclass).

Read more on late static binding here on php.net.
Also check the answer on another question here and here.