后期静态绑定
后期静态绑定的功能,用于在继承范围内引用静态调用的类。准确的说,后期静态绑定工作原理是存储了在上一个“非转发调用”的类名。当进行静态方法调用时,该类名即为明确指定的那个(通常在范围解析操作符符 ::
左侧部分)。当进行非静态方法调用时,即为该对象所属的类。所谓的“转发调用”指的是通过以下几种方式进行的静态调用: self::
, parent::
, static::
以及 forward_static_call()
。可以在类中用 get_called_class()
函数来得到被调用的方法所在的类名, static::
则指出了其范围。该功能从语言内部角度考虑被命名为“后期静态绑定”。“后期绑定”的意思是说, static::
不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。使用 self::
或者 __CLASS__
对当前类的静态引用,取决于定义当前方法所在的类。
<?php
class Foo
{
/**
* Return the name of the current class.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
/**
* Call the specified method.
*
* @param void
* @return string
*/
public static function test(): string
{
return self::method();
}
}
class Bar extends Foo
{
/**
* Override the parent method.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
}
var_dump(Bar::test()); // string(3) "Foo"
使用后期静态绑定。
<?php
class Foo
{
/**
* Return the name of the current class.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
/**
* Call the specified method.
*
* @param void
* @return string
*/
public static function test(): string
{
return static::method();
}
}
class Bar extends Foo
{
/**
* Override the parent method.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
}
var_dump(Bar::test()); // string(3) "Bar"
在非静态环境下,所调用的类即为该对象实例所属的类。由于 $this
会在同一作用范围内尝试调用私有方法,而 static::
则可能给出不同结果。另一个区别是 static::
只能用于静态属性。
<?php
class Foo
{
/**
* Return success if the method is successfully called.
*
* @param void
* @return string
*/
private function method(): string
{
return 'success';
}
/**
* Call the specified method.
*
* @param void
* @return void
*/
public function test()
{
var_dump($this->method());
var_dump(static::method());
}
}
class Bar extends Foo
{
//
}
class Baz extends Foo
{
/**
* Override the parent method.
*
* @param void
* @return void
*/
private function method()
{
//
}
}
$bar = new Bar();
$bar->test();
// string(7) "success"
// string(7) "success"
$baz = new Baz();
$baz->test();
// string(7) "success"
// PHP Fatal error: Uncaught Error: Call to private method Baz::method() from context 'Foo'.
后期静态绑定的解析会一直到取得一个完全解析了的静态调用为止。另一方面,如果静态调用使用 parent::
或者 self::
等将会转发调用。
<?php
class Foo
{
/**
* Return the name of the current class.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
/**
* Different calls for late static binding.
*
* @param void
* @return string
*/
public static function call(): string
{
return static::method();
}
}
class Bar extends Foo
{
/**
* Override the parent method.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
/**
* Call the specified method.
*
* @param void
* @return void
*/
public static function test()
{
var_dump(Foo::call());
var_dump(parent::call());
var_dump(self::call());
}
}
class Baz extends Bar
{
/**
* Override the parent method.
*
* @param void
* @return string
*/
public static function method(): string
{
return __CLASS__;
}
}
Baz::test();
// string(3) "Foo"
// string(3) "Baz"
// string(3) "Baz"