PHP:按字符串值获取静态类的实例(PHP: Get instance of static class by string value)

我正在研究一个php web api,它带有很多需要重构的代码。 编写代码的人希望将静态配置类包含到api资源中,然后获取该类的实例,如下所示:

<?php $obj = "User"; $confObjectSuffix = "_conf"; $confObject = $obj.$confObjectSuffix; if ($confObject::inst()->checkMethod($method)) { .....

这给出了错误“解析错误:语法错误,意外的T_PAAMAYIM_NEKUDOTAYIM in .....”,因为$ confObject是一个字符串而不是一个对象。

我写了一些测试代码:

<?php $class = "User_conf"; echo "<pre>"; print_r($$class::Inst()); echo "</pre>"; class User_conf { private static $INSTANCE = null; public static function Inst() { if(User_conf::$INSTANCE === null) { User_conf::$INSTANCE = new User_conf(); } return User_conf::$INSTANCE; } }

但也无法让它与$$合作,还有其他方法吗? 我不想重写超过必要的。

I'm working on a php web api that was handed to me with a lot of code that needs to be refactored. The ones that wrote the code wanted to include a static configuration class to an api resource and then get an instance of that class something like this:

<?php $obj = "User"; $confObjectSuffix = "_conf"; $confObject = $obj.$confObjectSuffix; if ($confObject::inst()->checkMethod($method)) { .....

This gives the error "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ....." since $confObject is a string and not a object.

I wrote some testcode:

<?php $class = "User_conf"; echo "<pre>"; print_r($$class::Inst()); echo "</pre>"; class User_conf { private static $INSTANCE = null; public static function Inst() { if(User_conf::$INSTANCE === null) { User_conf::$INSTANCE = new User_conf(); } return User_conf::$INSTANCE; } }

But can't get it to work with $$ either, is there some other way around this? I don't want to rewrite more than necessary.

最满意答案

您可以使用call_user_func捕获实例,然后根据需要进行处理:

$instance = call_user_func(array($confObject, 'inst')); if($instance->checkMethod($method)) { ...

You can use call_user_func to capture the instance, then process it as needed:

$instance = call_user_func(array($confObject, 'inst')); if($instance->checkMethod($method)) { ...PHP:按字符串值获取静态类的实例(PHP: Get instance of static class by string value)

我正在研究一个php web api,它带有很多需要重构的代码。 编写代码的人希望将静态配置类包含到api资源中,然后获取该类的实例,如下所示:

<?php $obj = "User"; $confObjectSuffix = "_conf"; $confObject = $obj.$confObjectSuffix; if ($confObject::inst()->checkMethod($method)) { .....

这给出了错误“解析错误:语法错误,意外的T_PAAMAYIM_NEKUDOTAYIM in .....”,因为$ confObject是一个字符串而不是一个对象。

我写了一些测试代码:

<?php $class = "User_conf"; echo "<pre>"; print_r($$class::Inst()); echo "</pre>"; class User_conf { private static $INSTANCE = null; public static function Inst() { if(User_conf::$INSTANCE === null) { User_conf::$INSTANCE = new User_conf(); } return User_conf::$INSTANCE; } }

但也无法让它与$$合作,还有其他方法吗? 我不想重写超过必要的。

I'm working on a php web api that was handed to me with a lot of code that needs to be refactored. The ones that wrote the code wanted to include a static configuration class to an api resource and then get an instance of that class something like this:

<?php $obj = "User"; $confObjectSuffix = "_conf"; $confObject = $obj.$confObjectSuffix; if ($confObject::inst()->checkMethod($method)) { .....

This gives the error "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ....." since $confObject is a string and not a object.

I wrote some testcode:

<?php $class = "User_conf"; echo "<pre>"; print_r($$class::Inst()); echo "</pre>"; class User_conf { private static $INSTANCE = null; public static function Inst() { if(User_conf::$INSTANCE === null) { User_conf::$INSTANCE = new User_conf(); } return User_conf::$INSTANCE; } }

But can't get it to work with $$ either, is there some other way around this? I don't want to rewrite more than necessary.

最满意答案

您可以使用call_user_func捕获实例,然后根据需要进行处理:

$instance = call_user_func(array($confObject, 'inst')); if($instance->checkMethod($method)) { ...

You can use call_user_func to capture the instance, then process it as needed:

$instance = call_user_func(array($confObject, 'inst')); if($instance->checkMethod($method)) { ...