PHP中的动态静态方法调用? [英] Dynamic static method call in PHP?
问题描述
请有 PHP 经验的人帮忙解决以下问题.在我的代码中,我调用了一个非实例化类中的公共静态方法:
Please could someone experienced in PHP help out with the following. Somewhere in my code, I have a call to a public static method inside a non-instantiated class:
$result = myClassName::myFunctionName();
但是,我希望有很多这样的类,并根据用户的语言即时确定正确的类名.换句话说,我有:
However, I would like to have many such classes and determine the correct class name on the fly according to the user's language. In other words, I have:
$language = 'EN';
...我需要做一些类似的事情:
... and I need to do something like:
$result = myClassName_EN::myFunctionName();
我知道我可以将语言作为参数传递给函数并在一个公共类中处理它,但出于各种原因,我更喜欢不同的解决方案.
I know I could pass the language as a parameter to the function and deal with it inside just one common class but for various reasons, I would prefer a different solution.
这有意义吗,有人吗?谢谢.
Does this make any sense, anyone? Thanks.
推荐答案
使用 call_user_func 函数:
http://php.net/manual/en/function.call-user-func.php
示例:
call_user_func('myClassName_' . $language . '::myFunctionName');
这篇关于PHP中的动态静态方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!