What is late static binding ? In fact, our previous article PHP Medium static I've already said this in . Today, let's have a deeper understanding of this concept .
First , We introduce the concept of late static binding through a piece of code :
class A
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
public static function test()
{
self::who();
}
}
class B extends A
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
}
B::test(); // A
In this code , We used self keyword , When using B Class call test() Static method ,self Pointing to A Class who() Method , therefore , The output is A. Don't get excited. , This is a normal static binding .self The content of a keyword call depends on the class in which it is defined . That is to say, no matter how you inherit , Which subclass to call test() Method ,self Keyword will call is A Class who() Method .
What about later static binding ? In fact, it's a bit like instantiated class objects , Each instantiated object , It's all about itself , Instead of the property method of the parent class . Normal static calls are not like this , But in reality, we have such a demand , Call static property methods just like instantiating objects , At this time , We can use static Keyword to achieve late static binding .
class C
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
public static function test()
{
static::who();
}
}
class D extends C
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
}
D::test(); // D
When using static The key word , here D Class calls test() Method called internally who() Namely D I like myself .
The definitions in the official document are as follows :
When making a static method call , The name of the class is the one specified ( Usually in :: The left side of the operator ); When making a non static method call , Is the class to which the object belongs .
This function is named from the internal point of view of language “ Late static binding ”.“ Late binding ” Means to say ,static:: No longer resolves to the class that defines the current method , It's actually calculated at run time . It can also be called “ Static binding ”, Because it can be used for ( But not limited to, ) Static method calls .
except self and static Out of keywords , We have one more parent keyword , The meaning of this keyword is obvious , Call the static content of the parent class . We use three keywords to test at the same time :
class E
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
public static function test()
{
self::who();
static::who();
}
}
class F extends E
{
public static function who()
{
echo __CLASS__, PHP_EOL;
}
}
class G extends F
{
public static function who()
{
parent::who();
echo __CLASS__, PHP_EOL;
}
}
G::test();
// E
// F
// G
Last , Let's look at two more PHP Methods , One is get_called_class() Method , Which class is used to get the current call . In the static method, you can determine which class the current class is to perform other business logic operations according to the calling mode . The other is forward_static_call() Method , For static method calls .
class H
{
public static function who()
{
echo __CLASS__ . ':' . join(',', func_get_args()), PHP_EOL;
}
public static function test()
{
echo get_called_class(), PHP_EOL;
forward_static_call('who', 'a', 'b'); // xxx:a,b
forward_static_call(['I', 'who'], 'c', 'd'); // I:c,d
forward_static_call_array(['H', 'who'], ['e', 'f']); // H:e,f
}
}
class I extends H
{
public static function who()
{
echo __CLASS__ . ':' . join(',', func_get_args()), PHP_EOL;
}
}
function who()
{
echo 'xxx:' . join(',', func_get_args()), PHP_EOL;
}
H::test(); // H
// xxx:a,b
// I:c,d
// H:e,f
I::test(); // I
// xxx:a,b
// I:c,d
// H:e,f
Be careful , If forward_static_call() If you don't specify a class name , Global methods will be called .forward_static_call_array() Is to pass the parameters using an array .
Each media platform can search 【 Hard core project manager 】