本文介绍了static :: staticFunctionName()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道self::staticFunctionName()parent::staticFunctionName()是什么,以及它们彼此之间以及与$this->functionName有何不同.

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

但是什么是static::staticFunctionName()?

推荐答案

这是PHP 5.3+中用于调用后期静态绑定的关键字.
阅读手册中的所有内容: http://php.net /manual/en/language.oop5.late-static-bindings.php

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

总而言之,static::foo()的工作方式类似于动态self::foo().

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解决了这个问题:

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作为此行为的关键字有点令人困惑,因为它只是. :)

static as a keyword for this behavior is kind of confusing, since it's all but. :)

这篇关于static :: staticFunctionName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 08:11