duancaiyi7567 2016-11-09 19:13
浏览 112

从php中的匿名函数访问受保护的静态方法?

I have a following problem: in a static method of some class A I define anonymous function within from I want to call another protected static method of the class A. The problem is I need to make it work in php 5.3. Any ideas? Here is a sample code:

class A {
    public static function test() 
    {
        $class = new static;
        $f = function () use ($class) {
            // problem: I need to call static::foo() from here
            return $class::foo();
        };

        print $f();
    }
    protected static function foo() { return 1; }
}

class B extends A {
    protected static function foo() { return 2; }
}

// assuming this will output 2
B::test();

This works in php >= 5.4, but in php 5.3 I get Fatal error: Call to protected method B::foo() from context '' ....

Thanks for any help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度