简体   繁体   English

PHP OOP - 常量与静态变量?

[英]PHP OOP - constant vs static variables?

In PHP, What is is the difference between: 在PHP中,有什么区别:

  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. 我知道如何使用它们,但我无法清楚地区分它们。

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

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. 我希望它对你有所帮助。

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. 每次调用函数时,该值都会改变,例如,如果你执行$i++ ,其中$istatic变量。

As for extending a class and creating its object , this is known as inheritance, check out this post to know more about it: 至于extending a class and creating its object ,这被称为继承,请查看这篇文章以了解更多信息:

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. 名称static是在内存分配的性质之后。 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...'

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) 阅读有关面向对象编程的更多信息(不仅仅是PHP)

您想要使用静态成员变量/函数的原因是因为您可以提取有关该类的信息而无需创建它的实例(这会降低您的CPU开销)。

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. 在Class Level创建的静态变量。 Constant variable created at instance level. 在实例级别创建的常量变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM