11

I know what self::staticFunctionName() and parent::staticFunctionName() are, and how they are different from each other and from $this->functionName.

But what is static::staticFunctionName()?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shealtiel
  • 8,020
  • 18
  • 50
  • 82
  • *(related)* [What does that symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Nov 08 '10 at 08:04

1 Answers1

16

It's the keyword used in PHP 5.3+ to invoke late static bindings.
Read all about it in the manual: http://php.net/manual/en/language.oop5.late-static-bindings.php


In summary, static::foo() works like a dynamic 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 solves this problem:

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 as a keyword for this behavior is kind of confusing, since it's all but. :)

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    You use static::functionName() if you're in the parent class, but you want to call the child's static function. That way you can let subclasses override static behaviour. – Michael Clerx Nov 08 '10 at 01:56