Detailed Explanation of PHP Class and Object Static Binding Operation Instance in Later Stage

  • 2021-11-13 00:56:46
  • OfStack

The example of this paper tells the static binding operation between PHP class and object in the later stage. Share it for your reference, as follows:

Static binding is very useful when doing projects. For example, service layer singleton pattern is very easy to implement with late static binding.

Since PHP 5.3. 0, PHP has added a feature called late static binding to reference statically called classes within the scope of inheritance.

To be precise, late static binding works by storing the class name of the last "non-forwarding call" (non-forwarding call). When a static method call is made, 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. The so-called "forward call" (forwarding call) refers to static calls made in the following ways: self:: , parent:: , static:: As well as forward_static_call() . Available get_called_class() Function to get the class name of the called method, and static:: indicates its scope.

This function is named "late static binding" from the perspective of language. "Late binding" means that, static:: Is no longer resolved to define the class in which the current method is located, but is evaluated at actual runtime. It can also be called "static binding" because it can be used for (but not limited to) static method calls.


<?php
class A {
  public static function who() {
    echo __CLASS__;
  }
  public static function test() {
    static::who(); //  Late static binding starts here 
  }
}
class B extends A {
  public static function who() {
    echo __CLASS__;
  }
}
B::test();
?>

The above routine outputs:

B

Note:

In a non-static environment, the class called is the class to which the object instance belongs. Due to $this- > An attempt is made to call a private method within the same scope of 1, whereas static:: might give a different result. Another difference is that static:: can only be used for static attributes.

Example # 3 Use static in a non-static environment:


<?php
class A {
  private function foo() {
    echo "success!\n";
  }
  public function test() {
    $this->foo();
    static::foo();
  }
}
class B extends A {
  /* foo() will be copied to B, hence its scope will still be A and
  * the call be successful */
}
class C extends A {
  private function foo() {
    /* original method is replaced; the scope of the new one is C */
  }
}
$b = new B();
$b->test();
$c = new C();
$c->test();  //fails
?>

The above routine outputs:

success!
success!
success!
Fatal error: Call to private method C:: foo () from context 'A' in/tmp/test. php on line 7

Note:

The resolution of the late static binding will be 1 until a fully resolved static call is obtained. Another aspect, if a static call uses a parent:: Or self:: The call information will be forwarded.

Example # 4 Forward and Non-Forward Calls


<?php
class A {
  public static function foo() {
    static::who();
  }
  public static function who() {
    echo __CLASS__."\n";
  }
}
class B extends A {
  public static function test() {
    A::foo();
    parent::foo();
    self::foo();
  }
  public static function who() {
    echo __CLASS__."\n";
  }
}
class C extends B {
  public static function who() {
    echo __CLASS__."\n";
  }
}
C::test();
?>

The above routine outputs:

A
C
C

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: