Rectangle 27 96

call_user_func('myClassName_' . $language . '::myFunctionName');
is_callable
method_exists
call_user_func_array

This sort of works and seems like the cleanest solution. Thanks

@nikc.org You can also pass parameters directly to call_user_func() if compiling them into an array is inconvenient.

Is there a way to do this using ReflectionMethod class?

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 93

call_user_func('myClassName_' . $language . '::myFunctionName');
is_callable
method_exists
call_user_func_array

This sort of works and seems like the cleanest solution. Thanks

@nikc.org You can also pass parameters directly to call_user_func() if compiling them into an array is inconvenient.

Is there a way to do this using ReflectionMethod class?

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 3

If you're working within a class in PHP, then I would recommend using the overloaded __call function in PHP5. You can find the reference here.

Basically __call does for dynamic functions what __set and __get do for variables in OO PHP5.

Dynamic class method invocation in PHP - Stack Overflow

php
Rectangle 27 113

$this->{$methodName}($arg1, $arg2, $arg3);
$this->$methodName($arg1, $arg2, $arg3);
call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3));

I guess maybe I did have the syntax right, so something else is wrong with my code as it's not quite functioning correctly. Hmm...

Dynamic class method invocation in PHP - Stack Overflow

php
Rectangle 27 13

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

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 13

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

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 11

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

It's too bad this only works in >=5.3.0 :(

For me, this is the best solution. And according to my bench, this is the quickest too : 100.000 iterations took 0.28s with direct call, 0.32 with this solution, and 0.47 with call_user_func

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 11

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

It's too bad this only works in >=5.3.0 :(

For me, this is the best solution. And according to my bench, this is the quickest too : 100.000 iterations took 0.28s with direct call, 0.32 with this solution, and 0.47 with call_user_func

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 6

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

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

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 6

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

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

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 9

$this->$methodName($arg1, $arg2, $arg3);

Thanks. I had thought of that, but hadn't tried it yet.

Dynamic class method invocation in PHP - Stack Overflow

php
Rectangle 27 3

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

Please could you tell me why it's such a bad idea?

you can use language as a parameter, instead you choose to define a new class for it. Say you wanna support 200 different languages, will you sit and write 200 different classes. Also it will make your code extremely difficult to read and understand.

Ah.... no there won't be 200 languages. It's a conscious downgrade of good practice to get something else done in a more practical way.

just a note: this works only since php 5.3 - better check if your host provider supports that version (the one I'm using doesn't).

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 3

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

Please could you tell me why it's such a bad idea?

you can use language as a parameter, instead you choose to define a new class for it. Say you wanna support 200 different languages, will you sit and write 200 different classes. Also it will make your code extremely difficult to read and understand.

Ah.... no there won't be 200 languages. It's a conscious downgrade of good practice to get something else done in a more practical way.

just a note: this works only since php 5.3 - better check if your host provider supports that version (the one I'm using doesn't).

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic
Rectangle 27 4

You can use the Overloading in PHP: Overloading

class Test {

    private $name;

    public function __call($name, $arguments) {
        echo 'Method Name:' . $name . ' Arguments:' . implode(',', $arguments);
        //do a get
        if (preg_match('/^get_(.+)/', $name, $matches)) {
            $var_name = $matches[1];
            return $this->$var_name ? $this->$var_name : $arguments[0];
        }
        //do a set
        if (preg_match('/^set_(.+)/', $name, $matches)) {
            $var_name = $matches[1];
            $this->$var_name = $arguments[0];
        }
    }
}

$obj = new Test();
$obj->set_name('Any String'); //Echo:Method Name: set_name Arguments:Any String
echo $obj->get_name();//Echo:Method Name: get_name Arguments:
                      //return: Any String

Dynamic class method invocation in PHP - Stack Overflow

php
Rectangle 27 3

public function problematicFunction($object) {
    if ($object instanceof $this->className) {
        // Do your stuff
    } else {
        throw new InvalidArgumentException("YOur error Message");
    }
}

PHP : Function argument must be an Object with dynamic class name - St...

php class function dynamic arguments
Rectangle 27 2

I don't know if I'd call it a "bug," but it's certainly an idiosyncrasy of PHP prior to PHP 7. This, and a whole class of similar issues, was addressed by the Uniform Variable Syntax RFC.

Thanks, it looks like this is explicitly addressed in the RFC: "Static property fetches and method calls can now be applied to any dereferencable expression. E.g. $foo['bar']::$baz, $foo::$bar::$baz and $foo->bar()::baz() are all valid now." Is there any workaround for PHP <7, other than just assigning to a string variable first?

Nope, gotta use the intermediate variable. (Well, there are actually some pretty obscure tricks you might be able to pull with the syntax but they'd give you an awful mess that isn't worth the trade-off, and I don't even want to suss out which [if any] would work here.)

syntax - Why can't I directly use function return values as dynamic cl...

php syntax syntax-error late-static-binding
Rectangle 27 2

I don't know if I'd call it a "bug," but it's certainly an idiosyncrasy of PHP prior to PHP 7. This, and a whole class of similar issues, was addressed by the Uniform Variable Syntax RFC.

Thanks, it looks like this is explicitly addressed in the RFC: "Static property fetches and method calls can now be applied to any dereferencable expression. E.g. $foo['bar']::$baz, $foo::$bar::$baz and $foo->bar()::baz() are all valid now." Is there any workaround for PHP <7, other than just assigning to a string variable first?

Nope, gotta use the intermediate variable. (Well, there are actually some pretty obscure tricks you might be able to pull with the syntax but they'd give you an awful mess that isn't worth the trade-off, and I don't even want to suss out which [if any] would work here.)

syntax - Why can't I directly use function return values as dynamic cl...

php syntax syntax-error late-static-binding
Rectangle 27 4

It was always possible for php5.0 and above.

Also, it's mentioned in documentation (static)

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

Good point, though I think this (no bug) is really infamous... For example, I was reading my code, thinking that this method definition was dynamic. I was really surprised to realize it was a static method. I think this is bad, cause it might make the developer believe that the method is declared as dynamic, and though can't be accessed from outside the class without being instanciated.

@Cooluhuru, i do not think so because i am using it (helper methods that not affect to object state).

It Works for you cause they are protected.

@Cooluhuru , have you got your answer?

class - PHP static function being called in dynamic environment - Stac...

php class dynamic static-methods
Rectangle 27 1

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.

class - Dynamic static method call in PHP? - Stack Overflow

php class function dynamic