Home  >  Article  >  Backend Development  >  Understanding the static keyword in php

Understanding the static keyword in php

angryTom
angryTomforward
2019-11-26 17:35:003193browse

Understanding the static keyword in php

Understanding of the static keyword in php

Understanding of static static variables

The static variable type specifier is static.

Static variables belong to the static storage method, and their storage space is the static data area in the memory (storage units are allocated in the static storage area). The data in this area occupies these storage spaces throughout the running of the program. (It is not released during the entire running of the program), and it can also be considered that its memory address remains unchanged until the end of the entire program (on the contrary, auto automatic variables, that is, dynamic local variables, belong to the dynamic storage category and occupy dynamic storage space. Functions Released after the call is completed). Although static variables always exist throughout the execution of the program, they cannot be used outside its scope.

In addition, quantities belonging to static storage methods are not necessarily static variables. For example: Although external variables belong to the static storage method, they are not necessarily static variables. They must be defined by static before they can become static external variables, or static global variables.

All global variables are static variables, and local variables are local static variables only when they are defined with the type modifier static.

Static variables can be applied anywhere. Once the application is successful, it will no longer accept other similar applications.

Static variables do not mean that they cannot change the value. A quantity that cannot change the value is called a constant. The value it holds is mutable, and it will remain up-to-date. It is said to be static because it does not change when the function is called or exits. That is, if we assign a certain value to a static variable the last time the function is called, the value will remain unchanged the next time the function is called.

Static variables within the function

static usage

1, please see the following example:

function doStuff(&$cache) {
    static $cache = null;
    if ($cache === null) {
        echo $cache = '%heavy database stuff or something%';
    }
}
$cache = 'not null';
doStuff($cache);
// Output
%heavy database stuff or something%

And, in doStuff( ) function, the static variable $cache is not immutable, $cache changed from null to %heavy database stuff or something%; from the above example, we can see that the static keyword affects reference transfer, even if we use & To try to change the value and address of the variable $cache, it still does not affect the if judgment in the doStuff() function;

Static methods and attributes in the class

● We Treat the class as a template for generating objects, use the object as an active component, instantiate a class, get an object, and then access the methods and properties of the object.

For example $foo = new Foo(); $foo is the object after instantiation of class Foo.

● Static methods are functions with class as scope. We can directly access static methods without instantiation.

For example:

class Foo()
{
    public static function a(){}
}
// 访问a();
Foo::a();

● In the current class ( To access static methods or attributes in non-subclasses) use self::method(), note: self can call static methods and attributes of the parent class; ● Static methods cannot access ordinary attributes and methods in this class, because those attributes and methods Belonging to an object, static methods and properties are also called methods of class variables.

Delayed static binding

Let’s look at an example first

header("Content-type: text/html; charset=utf-8");
class A
{
    public static function aa()
    {
        echo "非延迟静态绑定<br>";
    }
     
    public static function bb()
    {
        echo self::aa();  // Output 非延迟静态绑定
        echo static::aa(); // Output 延迟静态绑定
    }
}
class B extends A
{
    public static function aa()
    {
        echo "延迟静态绑定";
    }
     
    public static function cc()
    {
        echo self::bb();
    }
}
B::bb();
// Output
非延迟静态绑定
延迟静态绑定

After php5.3, we can use static to get the aa() of the subclass Method, which refers to the class being called. Using the self keyword refers to the current class (A), so what is obtained is the return value of the aa() method of class A;

Summary:

Lazy binding of the static keyword It has many uses and can generally be summarized while working on a project.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Understanding the static keyword in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete