Im working on a project for a legacy codebase, using php 5.2.6. Part of this project involves something akin to A/B testing using one PHP class vs another. The two classes have many of the same function names, with very similar signatures, but different approaches within the methods. I am wondering if its possible to call static methods within the classes using a dynamic / variable-based class name.
For example, id like to set the class name as such:
$class = isset($some_condition) && $some_condition ? 'NewClassName' : 'LegacyClassName';
...and then call functions as such:
$class::myStaticFunction();
Im aware that this can be accomplished with call_user_func(), but Im having trouble finding alternative approaches (if there are any). I simply would rather not regex replace all the calls to the legacy class with call_user_func() statements.
e.g.,
$stuff = call_user_func($class . '::myStaticFunction()');
...does work just fine.
Does anyone know if there is a more simple way to express: $dynamicClassName::staticFunction() with PHP 5.2? Perhaps I am missing something with my syntax, etc.
You can upgrade your PHP version. Since 5.3 you can use they way you want to call your function.
There is no other way in 5.2.
You can use this writing, maybe more easy to read :
$stuff = call_user_func_array(array($class, $method), array($arg1, $arg2));