我知道什么是self::staticFunctionName()和parent::staticFunctionName(),以及它们之间和$this->functionName之间的不同之处。
但是什么是static::staticFunctionName()呢?
发布于 2010-11-08 09:53:21
它是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作为此行为的关键字有点令人困惑,因为它几乎是。:)
https://stackoverflow.com/questions/4120755
复制相似问题