EDIT with my own comment afterwards
I think the problem is that when PHP is parsing the file to "compile", first it translates class names to their fully qualified name. So Index will be translated to Controller\Home\Index. After that, is when PHP translates variables into their value. So if I use a variable as a class name, it won't qualified its name, because that step has already happened. And thats why is not finding the class. That's just a guess, but most likely it is that way Blockquote
End Edit
Im using UniversalClassLoader from Symfony2 project to auto load my classes, but I've found some strange error that I can't solve.
The auto loading thing is working fine, but then I ran into this problem:
$controller = new Index(); // It works!
$controller_name = "Controller\\Home\\Index";
$controller2 = new $controller_name(); // It works!
$controller_name = "Index";
$controller3 = new $controller_name(); // Fatal error: Class 'Index' not found
The 2 first cases work just fine. In the first one, since Im using "use Controller\Home;" at the start of my script, I can use just "new Index();" without problems. But if instead of writing "Index", I use a string variable like $var = "Index", it does NOT work. I can't understand why. I need this script to be dynamic, thats why I need a variable for this.
Thank you!
additional long tail searches:
- php fully qualified name from variable
- php instantiate class from alias in variable