动态静态方法调用PHP? [英] Dynamic static method call in PHP?
问题描述
请有经验的PHP帮助以下。在我的代码中,我在一个非实例化的类中调用了一个public static方法:
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屋!