The main problem is that use cannot have a variable as part of it, as it is used when parsing a file, and the variable is only available at run time.
This is an example of how you can do what you think ...
<?php namespace Controller; ini_set('display_errors', 'On'); error_reporting(E_ALL); class Index { public function show() { echo "Showing..."; } } $classandmethod = "Controller\ Index@show "; list($className,$method) = explode("@", $classandmethod); $a= new $className(); $a->$method();
It displays ...
Showing...
Of course, you can say that all these classes must be in the Controller namespace, so the code will be changed to
$classandmethod = " Index@show "; list($className,$method) = explode("@", $classandmethod); $className = "Controller\\".$className; $a= new $className(); $a->$method();
source share