php PHP中的动态静态方法调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic static method call in PHP?
提问by Tom
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:
请有 PHP 经验的人帮忙解决以下问题。在我的代码中的某个地方,我调用了一个非实例化类中的公共静态方法:
$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.
这有意义吗,有人吗?谢谢。
回答by Ben Everard
Use the call_user_funcfunction:
使用call_user_func函数:
http://php.net/manual/en/function.call-user-func.php
http://php.net/manual/en/function.call-user-func.php
Example:
例子:
call_user_func('myClassName_' . $language . '::myFunctionName');
回答by Adam Hopkinson
I thinkyou could do:
我认为你可以这样做:
$classname = 'myClassName_' . $language;
$result = $classname::myFunctionName();
This is called Variable Functions
这称为变量函数
回答by Silvio Donnini
I would encapsulate the creation of the class you need in a factory.
我将在工厂中封装您需要的类的创建。
This way you will have a single entry point when you need to change your base name or the rules for mapping the language to the right class.
这样,当您需要更改基本名称或将语言映射到正确类的规则时,您将拥有一个入口点。
class YourClassFactory {
private $_language;
private $_basename = 'yourclass';
public YourClassFactory($language) {
$this->_language = $language;
}
public function getYourClass() {
return $this->_basename . '_' . $this->_language;
}
}
and then, when you have to use it:
然后,当你必须使用它时:
$yourClass = $yourClassFactoryInstance->getYourClass();
$yourClass::myFunctionName();
回答by Dimcho
As temuri said, parse error is produced, when trying '$className::functionName' :
正如 temuri 所说,在尝试 '$className::functionName' 时会产生解析错误:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM ...
解析错误:语法错误,意外的 T_PAAMAYIM_NEKUDOTAYIM ...
In my case (static method with 2 arguments), best solutions is to use call_user_func_arraywith 2 arrays (as suggested by nikc.org):
在我的情况下(带有 2 个参数的静态方法),最好的解决方案是使用带有 2 个数组的call_user_func_array(如 nikc.org 所建议的):
$result = call_user_func_array(array($className, $methodName), array($ard1, $arg2));
BR
BR
回答by marvin
although i think the way you deal is a very bad idea, i think i may have a solution
虽然我认为你的处理方式是一个非常糟糕的主意,但我想我可能有一个解决方案
$className = 'myClassName_'.$language;
$result = $className::myFunctionName();
i think this is what you want
我想这就是你想要的
回答by Sarfraz
As far as i could understand your question, you need to get the class name which can be done using get_classfunction. On the other hand, the Reflection classcan help you here which is great when it comes to methods, arguments, etc in OOP way.
据我了解您的问题,您需要获取可以使用get_class函数完成的类名。另一方面,Reflection 类可以在这里为您提供帮助,这在 OOP 方式的方法、参数等方面非常有用。
回答by KorbenDallas
You can easily do next:
您可以轻松地进行下一步:
<?php
class B {
public static $t = 5;
public static function t($h) {
return "Works!" . $h;
}
}
$g = 't';
$class = 'B';
echo $class::$g('yes'); //Works! Yes
And it will works fine, tested on PHP 5.2 >=
它可以正常工作,在 PHP 5.2 >= 上测试
回答by temuri
Solutions like:
解决方案如:
$yourClass::myFunctionName();
will not work. PHP will produce parse error.
不管用。PHP 会产生解析错误。
Unfortunately, the only way is to use very slowcall_user_func().
不幸的是,唯一的方法是使用非常慢的call_user_func().
回答by Beau
I know it's an old thread, but as of PHP 5.3.0 you should be using forward_static_call
我知道这是一个旧线程,但是从 PHP 5.3.0 开始,您应该使用forward_static_call
$result = forward_static_call(array('myClassName_EN', 'myFunctionName'));
Using your $language variable, it might look like:
使用您的 $language 变量,它可能看起来像:
$result = forward_static_call(array('myClassName_' . $language, 'myFunctionName'));

