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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With