Home >Backend Development >PHP Tutorial >[php classes and objects] late static binding

[php classes and objects] late static binding

不言
不言Original
2018-04-17 14:00:461351browse

This article introduces the content of [php classes and objects] late static binding, which has a certain reference value. Now I share it with you. Friends in need can refer to it

Later static binding

PHP 5.3.0

Used for: Reference to statically called classes in the inheritance scope

Working principle:

  • stored The class name of the last "non-forwarding call".

  • When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator);

  • When a non-static method call is made, it is the class to which the object belongs.

Forwarding call: static call through self::, parent::, static::, forward_static_call() (calling a static method).
You can use the get_called_class() function to get the class name of the called method, and static:: points out its scope.

This feature is named "late static binding" from a language internal perspective.
"Late binding" means that static:: is no longer parsed into the class in which the current method is defined, but is calculated at actual runtime.
It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.

<?phpclass A {
    public static function who() {
        echo __CLASS__; //返回该类被定义时的名字
    }    public static function test() {
        self::who();
    }
}class B extends A {
    // public static function who() {
    //     echo __CLASS__;
    // }}

B::test();  //A?>

Usage of late static binding

Late static binding originally wanted to circumvent this by introducing a new keyword to represent the class initially called at runtime. over restrictions.

Simply put, this keyword allows you to refer to class B instead of A when calling test() in the above example. It was finally decided not to introduce new keywords, but to use the already reserved static keyword.

Example #2 static:: 简单用法<?phpclass A {
    public static function who() {
        echo __CLASS__;
    }    public static function test() {
        static::who(); // 注意使用static,后期静态绑定从这里开始
    }
}class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();  //B?>

In a non-static environment, the class called is the class to which the object instance belongs. Since $this-> will try to call the private method in the same scope, static:: may give different results.
Another difference is that static:: can only be used for static properties.

Late static binding

PHP 5.3.0

Used for: referencing statically called classes in the inheritance scope

Working principle:

  • Stores the class name of the previous "non-forwarding call".

  • When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator);

  • When a non-static method call is made, it is the class to which the object belongs.

Forwarding call: static call through self::, parent::, static::, forward_static_call() (calling a static method).
You can use the get_called_class() function to get the class name of the called method, and static:: points out its scope.

This feature is named "late static binding" from a language internal perspective.
"Late binding" means that static:: is no longer parsed into the class in which the current method is defined, but is calculated at actual runtime.
It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.

<?phpclass A {
    public static function who() {
        echo __CLASS__; //返回该类被定义时的名字
    }    public static function test() {
        self::who();
    }
}class B extends A {
    // public static function who() {
    //     echo __CLASS__;
    // }}

B::test();  //A?>

Usage of late static binding

Late static binding originally wanted to circumvent this by introducing a new keyword to represent the class initially called at runtime. over restrictions.

Simply put, this keyword allows you to refer to class B instead of A when calling test() in the above example. It was finally decided not to introduce new keywords, but to use the already reserved static keyword.

Example #2 static:: 简单用法<?phpclass A {
    public static function who() {
        echo __CLASS__;
    }    public static function test() {
        static::who(); // 注意使用static,后期静态绑定从这里开始
    }
}class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();  //B?>

In a non-static environment, the class called is the class to which the object instance belongs. Since $this-> will try to call the private method in the same scope, static:: may give different results.
Another difference is that static:: can only be used for static properties.

Related recommendations:

[php classes and objects]Type constraints

[php classes and objects]Object copy

【php classes and objects】magic method

The above is the detailed content of [php classes and objects] late static binding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn