I have a class that I need to instantiate based on a variable passed from a form. The form either passes the string Product or Sku to my PHP script. The fully qualified class that needs to be instantiated is either:
$obj = new MyNameSpace\Product();
Or
$obj = new MyNameSpace\Sku();
I can't figure out how to properly create this namespace on the fly. I've tried the following:
$obj = new "MyNameSpace\\".$class();
Which throws this error:
PHP Parse error: syntax error, unexpected '"MyNameSpace\\"' (T_CONSTANT_ENCAPSED_STRING)
I've read other questions and answers that say to escape the slash in order to make it a literal slash, but even that does not seem to be working for me. For example:
$class = "Product";
$string = "MyNameSpace\\".$class;
echo $string;
$string prints as MyNameSpace\\Product instead of MyNameSpace\Product. What am I missing here?