Difference Analysis of Class Static Call and Scope Resolution Operators in PHP
- 2021-09-04 23:45:48
- OfStack
The specific code is as follows:
<?php
// Within a subclass or class, use the :: "When you call this class or the parent class, you do not call the method statically, but the scope resolution operator.
class ParentClass {
public static $my_static = 'parent var ';
function test() {
self::who(); // Output 'parent' Is scope resolution, not static call
$this->who(); // Output 'child'
static::who(); // Deferred static binding Is scope resolution, not static call
}
function who() {
echo 'parent<br>';
}
}
class ChildClass extends ParentClass {
public static $my_static = 'child var ';
function who() {
echo 'child<br>';
}
}
$obj = new ChildClass();
$obj->test();
echo ChildClass::$my_static;// Static invocation
Above output
parent
child
child
child var
Summarize