Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
299 views
Welcome To Ask or Share your Answers For Others

1 Answer

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. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...