Dynamic static method call in PHP?

和自甴很熟 提交于 2019-12-17 06:33:32

问题


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.


回答1:


Use the call_user_func function:

http://php.net/manual/en/function.call-user-func.php

Example:

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



回答2:


I think you could do:

$classname = 'myClassName_' . $language;
$result = $classname::myFunctionName();

This is called Variable Functions




回答3:


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();



回答4:


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




回答5:


As temuri said, parse error is produced, when trying '$className::functionName' :

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM ...

In my case (static method with 2 arguments), best solutions is to use call_user_func_array with 2 arrays (as suggested by nikc.org):

$result = call_user_func_array(array($className, $methodName), array($ard1, $arg2));

BR




回答6:


As far as i could understand your question, you need to get the class name which can be done using get_class function. On the other hand, the Reflection class can help you here which is great when it comes to methods, arguments, etc in OOP way.




回答7:


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 >=




回答8:


Solutions like:

$yourClass::myFunctionName();

will not work. PHP will produce parse error.

Unfortunately, the only way is to use very slow call_user_func().




回答9:


I know it's an old thread, but as of PHP 5.3.0 you should be using forward_static_call

$result = forward_static_call(array('myClassName_EN', 'myFunctionName'));

Using your $language variable, it might look like:

$result = forward_static_call(array('myClassName_' . $language, 'myFunctionName'));


来源:https://stackoverflow.com/questions/2108795/dynamic-static-method-call-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!