引用子类的静态成员 [英] Referring to a static member of a subclass

查看:58
本文介绍了引用子类的静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个sub关键字,使下面的代码在执行时显示value.虽然没有,但我想知道-是否存在引用子类成员的现有方法?

I wish there was a sub keyword that would make the code below print value on execution. There isn't though and I wonder - is there an existing way to refer to a member of a subclass?

class Main
{
    static function foo()
    {
        echo sub::$variable;
    }
}

class Sub extends Main
{
    static $variable = "value";
}

Sub::foo();

推荐答案

我认为您正在寻找static::$variable.这称为后期静态绑定,并且自PHP起可用5.3.

I think you are looking for static::$variable. That's called Late Static Binding and is available as of PHP 5.3.

这篇关于引用子类的静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆