首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >static::staticFunctionName()

static::staticFunctionName()
EN

Stack Overflow用户
提问于 2010-11-08 09:50:57
回答 1查看 1.4K关注 0票数 11

我知道什么是self::staticFunctionName()parent::staticFunctionName(),以及它们之间和$this->functionName之间的不同之处。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-08 09:53:21

它是PHP 5.3+中用于调用后期静态绑定的关键字。

请在手册中阅读所有相关内容:http://php.net/manual/en/language.oop5.late-static-bindings.php

总之,static::foo()的工作方式就像一个动态self::foo()

代码语言:javascript
复制
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解决了这个问题:

代码语言:javascript
复制
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作为此行为的关键字有点令人困惑,因为它几乎是。:)

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4120755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档