Dynamic static method call in PHP?

PhpClassFunctionDynamic

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

Use the call_user_func function:

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

Example:

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

Solution 2 - Php

I think you could do:

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

This is called Variable Functions

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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.

Solution 8 - Php

Solutions like:

$yourClass::myFunctionName();

will not work. PHP will produce parse error.

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

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomView Question on Stackoverflow
Solution 1 - PhpBen EverardView Answer on Stackoverflow
Solution 2 - PhpAdam HopkinsonView Answer on Stackoverflow
Solution 3 - PhpSilvio DonniniView Answer on Stackoverflow
Solution 4 - PhpDimchoView Answer on Stackoverflow
Solution 5 - PhpahmetunalView Answer on Stackoverflow
Solution 6 - PhpKorbenDallasView Answer on Stackoverflow
Solution 7 - PhpSarfrazView Answer on Stackoverflow
Solution 8 - PhptemuriView Answer on Stackoverflow
Solution 9 - PhpBeauView Answer on Stackoverflow