Delay binding of [modernPHP topic] class (late binding)

  1. Call with static:: to perform binding operation at the time of running;
  2. There is a method in the parent class that is delayed binding. When the child class:: calls this method, it returns to the calling child class and starts to look up;

example1:

class Human {
    public static function whoami() {
        echo 'From parent whoami In execution';
    }
    public static function say() {
        self::whoami(); // There is no say method in the subclass, found the parent class here
        // self here refers to the parent class
    }
    public static function say2() {
        static::whoami();    //  There is no say2 method in the subclass, and the parent class is found here
        // But the parent class uses static::whoami,
        // Refers to calling your subclass's own whoami method
    }
}

class Stu extends Human{
    public static function whoami () {
        echo 'From subclass whoami In execution';
    }
}

// whoami from the parent class is executing
Stu::say();//Call the say method of Stu class, but Stu class doesn't have the say method, so it looks for its parent class. After finding the parent class, it finds that self::whoami() is called in the say method of the parent class. At this time, there are actually two whoamI methods in self, but because the environment of this call is in the say method of the parent class, it calls the whoamI method of the parent class instead of the whoamI method of the child class;

echo PHP_EOL;

// whoami from subclass is executing
Stu::say2(); //Call the say2 method of Stu class, but Stu class does not have the say2 method, so it looks for its parent class. After finding the parent class say2, it finds that the parent class's say2 method uses static delay to bind the whoami method, while the child class that is called at this time has the whoami method (if not, it looks for the parent class), so it is bound to the whoami method of the child class at this time, so it calls the whoami method of the child class;

example2:

class Animal { 
    const age = 1; 
    public static $leg = 4; 
    public static function cry() { 
        echo 'Purring<br />'; 
    } 
    public static function t1() { 
        self::cry(); 
        echo self::age,'<br />'; 
        echo self::$leg,'<br />'; 
    } 
    public static function t2() { 
        static::cry(); 
        echo static::age,'<br />'; 
        echo static::$leg,'<br />'; 
    } 
} 
class Human extends Animal { 
    public static $leg = 2; 
    public static function cry() { 
        echo 'Wah<br />'; 
    } 
} 
class Stu extends Human { 
    const age = 16; 
    public static function cry() { 
        echo 'Chirp<br />'; 
    } 
} 
Stu::t1(); //Whining, 1,4 
/*
↑↑:Find the Animal class all the time. The t1 method of the Animal class is a normal binding, so it's whine, 1,4
*/

Stu::t2(); // 16,2 
/*
↑↑:Find the Animal class all the time. The t2 method of the Animal class is to delay binding, and then go back to the Stu class to start searching. The Stu class has the cry method, so it has the age attribute, so it's 16. There's no leg attribute. Then look up and find it all the time, so it's 2
*/

new static() and new self()

  1. Self - that's the class. It's the class in the code snippet. new self is to instantiate this class;
  2. static - PHP 5.3 adds only the current class, which is a bit like $this. It is extracted from the heap memory and accesses the currently instantiated class. Then static represents that class.
<?php
class A {
static public function get_self() {
        return new self();
    }

static public function get_static() {
        return new static();
    }
}

class B extends A { }

echo get_class(A::get_self());//A
echo get_class(A::get_static()); // A

echo get_class(B::get_self()); // A: instantiate the class of self () in the B::get_self() object, and return the class.
echo get_class(B::get_static()); // B: it accesses the current class, which is a bit like this, because it is called by the get_static method inherited by B.

Thus, their differences can only be reflected in inheritance. If there is no inheritance, then there is no difference between them. But if it is new in static method, it is better to use new static();

Keywords: PHP Attribute

Added by vladj on Sat, 16 Nov 2019 16:47:11 +0200