Instantiating an object with a string variable defaults to non-namespaced scope. Given two classes in the same namespace.
<?php
namespace foo;
class bar {
public function createSubclass(string $type) {
return new $type();
}
?>
<?php
namespace foo;
class baz {
}
?>
<?php
$barObj = new bar();
$barObj->createSubclass('baz');
// result: Uncaught Error: Class 'baz' not found
?>
Change bar class to:
<?php
namespace foo;
class bar {
public function createSubclass(string $type) {
type = '\\'.__NAMESPACE__.'\\'.type;
return new $type();
}
?>