Thinking about this again, I'm not sure whether variable variables is the reason that this works. Variable variables would suggest I should be saying new $$mystr? But it works just going new $mystr...
Very strange to me! What am I missing?
new is a php keyword, followed by the string of the name of the object to instantiate. Whether it's the classname, or a variable representing the string of the classname doesn't matter.
If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.
$instance = new SimpleClass();
// This can also be done with a variable:
$className = 'SimpleClass';
$instance = new $className(); // new SimpleClass()
// Way #1
$className = "App\MyClass";
$instance = new $className();
// ----------------------
// Way #2
$className = "App\MyClass";
$class = new \ReflectionClass($className);
// Create a new Instance without arguments:
$instance = $class->newInstance();
// Create a new Instance with arguments (need a contructor):
$instance = $class->newInstanceArgs(["Banana", "Apple"]);