静:: staticFunctionName()
我知道self::staticFunctionName()
和parent::staticFunctionName()
是什么,它们是如何从$this->functionName
彼此不同的。
但什么是static::staticFunctionName()
?
这是PHP 5.3+中用于调用晚期静态绑定的关键字。
阅读手册中的所有内容:http://php.net/manual/en/language.oop5.late-static-bindings.php
总之, static::foo()
工作方式类似于动态self::foo()
。
class A {
static function foo() {
// This will be executed.
}
static function bar() {
self::foo();
}
}
class B extends A {
static function foo() {
// This will not be executed.
// The above self::foo() refers to A::foo().
}
}
B::bar();
static
解决了这个问题:
class A {
static function foo() {
// This is overridden in the child class.
}
static function bar() {
static::foo();
}
}
class B extends A {
static function foo() {
// This will be executed.
// static::foo() is bound late.
}
}
B::bar();
作为这种行为的关键字static
是有点令人困惑,因为它只是一个。 :)