在PHP中的动态静态方法调用

在PHP中的动态静态方法调用

本文介绍了在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.

推荐答案

使用 function:

Use the call_user_func function:

示例:

call_user_func('myClassName_' . $language . '::myFunctionName');

这篇关于在PHP中的动态静态方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:20