PHP dynamic class name laravel PHP dynamic class name laravel laravel laravel

PHP dynamic class name laravel


Ok the answer to this is I needed a namespace on the class.

In composer.json

"psr-4": {    "SplitTest\\": "app/library/SplitTest/"}

Then called the class as so:

$class= "//SplitTest//Variant";$s = new Variant();$nc = new $class();

If you to the psr-4 definition you will need to run

php artisan dump-auto


This is actually what namespaces are for:

$s = new \OneNamespaceName\Variant();

This is often used in a Factory pattern. So namespaces are per-file so you need to include this in the the class declaration for Variant.