PHP dynamic class name laravel

I am using Laravel and having trouble creating a class with a string. The class is in the same name space of the class calling it.

The below code fails on the third line, I am unsure what I am doing wrong.

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


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.

Need Your Help

Recognizing Tail-recursive functions with Flex+Bison and convert code to an Iterative form

c recursion bison calculator flex-lexer

I'm writing a calculator with an ability to accept new function definitions. Being aware of the need of newbies to try recursive functions such as Fibonacci, I would like my calculator to be able to

dynamic_cast returns NULL but it shouldn't

c++ qt plugins dynamic-cast

I'm having the following class hierarchy: