PHP OOP - constant vs static variables?

萝らか妹 提交于 2019-12-09 12:24:51

问题


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.


回答1:


Static is for:

class properties or methods as static makes them accessible without needing an instantiation of the class

So, the value returned by a static member may differ. For example, you can call a static method with different result depending of what parameters you pass to it.

Constants value:

must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

So, it always return the same result when you call it

About create an object and extending a class, when you "create an object" you make an instance of a class. When you extend a class, you create an other class who:

inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

I hope it help you.




回答2:


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.

    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
        }
    }



回答3:


A constant is constant and can NOT change its value once assigned. A static variable, on the other hand, can have varying values. For example, you can create a static variable inside a function to know how many time the function was called. The value will change each time function is called eg if you do $i++ where $i is static variable.

As for extending a class and creating its object, this is known as inheritance, check out this post to know more about it:

  • PHP - Inheritance



回答4:


Constant variable is a variable which can be accessed by class name and can NOT be changed during script execution. Class static variable also can be accessed by class name but can be changed during program execution.

Second question - these are COMPLETELY other things. Read more about object oriented programming (not only in PHP)




回答5:


One important difference is in memory allocation.

When an instance of a class (object) is created, memory is allocated for the newly created object. The name static is after the nature of the memory allocation. That is, memory for static objects are allocated only once, statically. When an object changes it's static property, it reflects on all objects of the same class.

<?php
class Test {
    static $test = "world!";
    static function hello() {
        echo "\nhello " . self::$test . "\n";
    }
}

$t = new Test();
$x = new Test();

$t->hello(); // prints 'hello world!'
$t::$test = "Hmmm...";
$t->hello(); // prints 'hello Hmmm...'
$x->hello(); // prints 'hello Hmmm...'



回答6:


The reason you'd want to use a static member variable/function is because you can extract information about that class without needing to create an instance of it (which costs you less CPU overhead).




回答7:


Constant variable are separate for each and every object. But static variable are common for all object of that class. Each object share a single static variable. Static variable created at Class Level. Constant variable created at instance level.



来源:https://stackoverflow.com/questions/4163975/php-oop-constant-vs-static-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!