PHP 5.2是否等效于后期静态绑定(新静态)? [英] PHP 5.2 Equivalent to Late Static Binding (new static)?

查看:92
本文介绍了PHP 5.2是否等效于后期静态绑定(新静态)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为php 5.3构建的脚本在php 5.2服务器上工作.该脚本使用了大量后期静态绑定,例如:

I am trying to make a script that is built for php 5.3 work on a php 5.2 server. The script uses a lot of late static binding like:

return new static($options);

php 5.2中的等效项是什么?会以某种方式成为新的自我吗?还是不可能达到同样的效果...

What is the equivalent to this in php 5.2? would it be new self somehow? Or is it not possible to achieve the same effect...

谢谢

这是一个相关的问题新的自我与新的静态

Juts试图把我的头缠在这个后来的静态绑定东西上...

Juts trying to wrap my head around this late static binding stuff...

推荐答案

我认为唯一的方法是通过建立单例的受保护静态方法和定义要使用的类的公共静态方法. 您可以通过在$ this <上使用 get_class 函数来模拟"它./p>

I think the only way is to pass by a protected static method that build your singleton and a public static method that defines the class to use. You can "emulate" it by using the get_class function over $this

class ParentClass{
    protected static function getInstance2($className){
         //some stuffs here
         return new $className();
    }
    public static function getInstance(){
        return self::getInstance2(get_class(self));
    }
}
class ChildClass extends ParentClass{
    public static function getInstance(){
        return self::getInstance2(get_class(self));
    }
}

这篇关于PHP 5.2是否等效于后期静态绑定(新静态)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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