ds1379551 2009-02-03 23:27
浏览 19
已采纳

在没有实例化的情况下从超类访问子变量

I'm attempting to access member variables in a child class via the parent class without instantiation.

This is one of my attempts but B::getStatic() fails with Access to undeclared static property.

Is there another solution to this, possibly without static?

class A {

    static public function getStatic() {

        return self::$myStatic;

    }

}

class B extends A {

    public static $myStatic = 5;

}

class C extends A {

    public static $myStatic = 6;

}

var_dump(B::$myStatic);
var_dump(B::getStatic());

var_dump(C::$myStatic);
var_dump(C::getStatic());
  • 写回答

8条回答 默认 最新

  • douyan4470 2009-02-04 06:18
    关注

    The concept you're running into is called "Late Static Binding." Until PHP 5.3.0, there was no support for this.

    If you're running 5.3.0 or higher, update the getStatic() method:

    static public function getStatic() {

    return static::$myStatic;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器