1

认为我需要使用反射来使这个 5.3 代码在 5.2 上工作但有问题。

我们在一个只有 PHP 5.2 的服务器上,此时每个托管公司都无法升级,但是我们需要的一个类在 5.2 上存在问题,因为该类使用 5.3 语法。

这是我需要帮助的代码:

static public function instance($class) {
    if (!isset($class::$instance)) {
        $class::$instance = new $class();
        $class::$instance->initialize();

        MobileHelper::registerDevice($class::$instance);
    }

    return $class::$instance;
}

我已经看到了许多关于使用反射的问题的答案,但它们都是基本示例,我不太了解将它们转换为这里的解决方案,但我已经尝试过了。这里有任何专家可以提供帮助吗?

4

1 回答 1

0

在这里,同样使用反射。

static public function instance($class) {
    $ref = new ReflectionClass($class);
    if (!$ref->getStaticPropertyValue('instance')){
        $ref->setStaticPropertyValue('instance', new $class());
        $obj = $ref->getStaticPropertyValue('instance');
        $obj->initialize();
        MobileHelper::registerDevice($obj);
    }
    return $ref->getStaticPropertyValue('instance');
}
于 2012-09-04T12:23:54.747 回答