Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP - constant vs static variables?

In PHP, What is is the difference between:

  1. Constants and static variables?
  2. Extending a class and creating its object?

I know how they can be used, but I can't clearly distinguish between them.

like image 232
pMan Avatar asked Nov 12 '10 11:11

pMan


People also ask

What is the difference between static and constant in PHP?

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.

What is the difference between static variable and constant?

The value of a static variable can be modified. The value of the constant variable cannot be modified.

Does PHP have static variables?

Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::).

What is a static variable PHP?

The final type of variable scoping that I discuss is known as static. In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again.


1 Answers

Comparison

Static

  1. Can has a access modifier.

    class A{
        public static $public_static = "can access from anywhere";
        protected static $protected_static = "can access from inheriting classes";
        private static $private_static = "can access only inside the class";
    }
    
  2. Depending the visibility you can access static variables.

    //inside the class
        self::$variable_name;
        static::$variable_name;
    //outside the class
        class_name::$variable_name;
    
  3. Can change the value after declaration.

        self::$variable_name = "New Value";
        static::$variable_name = "New Value";
    
  4. No need to initialize when declare.

        public static $variable_name; 
    
  5. Normal variable declaration rules applied(ex: begins with $)

  6. Can create inside a function.

        class A{
            function my_function(){
                 static $val = 12;
                 echo ++$val; //13
            }
        }
    

Constant

  1. Always public cannot put access modifiers(>PHP7.1)

    class A{
        const my_constant = "constant value";
        public const wrong_constant="wrong" // produce a parse error
    }
    
  2. Anywhere you can access constant.

    //inside the class
        self::variable_name;
        static::variable_name;
    //outside the class
        class_name::variable_name;
    
  3. Cannot change the value after declaration.

    self::variable_name = "cannot change"; //produce a parse error
    
  4. Must initialize when declare.

    class A{
        const my_constant = "constant value";// Fine
        const wrong_constant;// produce a parse error
    }
    
  5. Must not use $ in the beginning of the variable(Other variable rules applied).

     class A{
        const my_constant = "constant value";// Fine
        const $wrong_constant="wrong";// produce a parse error
    }
    
  6. Cannot declare inside a function.


When Extending

    class A{

        public static $public_static = "can access from anywhere";
        protected static $protected_static = "can access from inheriting classes";
        private static $private_static = "can access only inside the class";
        
        const my_constant = "Constant value";
    }

    class B extends A{

        function output(){

            // you can use self or static

            echo self::$public_static; //can access from anywhere;
            echo self::$protected_static; //can access from inheriting classes;
            self::$protected_static = "Changed value from Class B";
            echo self::$protected_static; //"Changed value from Class B";
            
            echo self::$private_static; //Produce Fatal Error

            echo self::my_constant;//Constant value
        }
    }
like image 179
Ntwobike Avatar answered Oct 17 '22 23:10

Ntwobike