Sé lo que self::staticFunctionName()
y parent::staticFunctionName()
son, y cómo son diferentes entre sí y desde $this->functionName
.static :: staticFunctionName()
Pero, ¿qué es static::staticFunctionName()
?
Sé lo que self::staticFunctionName()
y parent::staticFunctionName()
son, y cómo son diferentes entre sí y desde $this->functionName
.static :: staticFunctionName()
Pero, ¿qué es static::staticFunctionName()
?
Es la palabra clave utilizada en PHP 5.3+ para invocar enlaces estáticos tardíos.
leer todos los detalles en el manual: http://php.net/manual/en/language.oop5.late-static-bindings.php
En resumen, static::foo()
obras como una dinámica 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
resuelve este problema:
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
como palabra clave para este comportamiento es un poco confuso, ya que es todo lo contrario. :)
Usted use static :: functionName() si está en la clase principal, pero desea llamar a la función estática del niño. De esta forma, puede dejar que las subclases anulen el comportamiento estático. –
* (relacionado) * [¿Qué significa ese símbolo en PHP] (http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon