I'm currently working on a Laravel project and I'm facing an issue with loading a namespaced PHP class. I've been trying to load the class using a string type variable, but it's not working.
Here's an example of what I've been trying to do:
// This works fine
$instance = new MyClass;
// But this doesn't work
$className = 'App\Path\To\MyClass';
$instance = new $className;
I find it strange that when I directly name the instance of the class, it works perfectly. However, when I use a string type variable to hold the namespace, it fails to load the class properly.
Am I doing something wrong here? Is there a different way to load a namespaced class using a string type variable? Any help or suggestions would be greatly appreciated.