Why am I getting PHP Fatal error: Uncaught Error: Class 'MyClass' not found?
You can instantiate a class dynamically in PHP using string variables, but only if the interpreter knows exactly which file to load. The code you started with works fine because everything lives in a single context; the autoloader hasn't been asked to do any heavy lifting, and the class definition is physically present before the new keyword executes.
Once you move that logic into separate files and rely on an autoloader, the rules change. When your script calls Container::get('MyClass'), the string 'MyClass' is just a label to you. To PHP, it's opaque until the runtime resolves it. The error occurs at this specific line in your src/Container.php:
static::$registry[$key] = new $key;
If the class isn't currently loaded in memory, PHP needs to load it first. It does this by looking for a file that matches the class name. In a standard setup without an explicit autoloader, PHP follows a strict naming convention: it looks for a file named MyClass.php in the current directory or includes directories defined in include_path.
Your project structure uses PSR-4 autoloading via Composer. This is a robust system, but it requires you to register it before attempting to instantiate any classes that aren't already loaded. In your main.php, you correctly require the autoloader:
require __DIR__.'/vendor/autoload.php';
use scratchers\nstest\Container;
This registers the logic needed to map namespaces like scratchers\\nstest to the src/ folder. However, there is a critical timing issue here. You are passing 'MyClass' as a string to the Container class. That string does not contain the namespace prefix (scratchers\\nstest).
When PHP tries to resolve new $key, it looks for a file named src/MyClass.php. While that file exists, the autoloader isn't automatically invoked every time you use a dynamic variable in the new expression. The autoloader only triggers when you reference a fully qualified class name or when it's explicitly told to load something missing.
If you switch to hardcoded instantiation:
static::$registry[$key] = new MyClass; // Assumes current namespace context
It works because PHP knows the current namespace is scratchers\\nstest, so it implicitly prepends that to MyClass, resolving it to scratchers\\nstest\\MyClass. The autoloader then sees a missing class and loads src/MyClass.php automatically.
The solution requires passing the fully qualified class name as the string key, or ensuring the autoloader is explicitly configured to handle dynamic names. The most common pitfall in this scenario is assuming that the autoloader handles partial names automatically during dynamic instantiation. It doesn't; it only handles missing classes based on strict naming rules.
To fix this without hardcoding the class name in the new statement, you must pass the fully qualified name as the string argument. Update your Container::get method to prepend the base namespace:
public static function get($key){
$fullyQualifiedClass = 'scratchers\\nstest\\' . $key;
if(!array_key_exists($key, static::$registry)){
static::$registry[$key] = new $fullyQualifiedClass;
}
return static::$registry[$key];
}
Alternatively, you can change the registry key to store the fully qualified name, but the simplest approach is to resolve the namespace within the method. If your application follows PSR-4 strictly, you might also consider using class_exists() with the autoloader in place before attempting instantiation, though the direct string manipulation above is often sufficient for this specific pattern.
If you are building a DI container or a similar registry where keys are user-defined strings, remember that the string must map to a path the autoloader understands. If you want to keep using simple strings like 'MyClass', you must ensure your Container class resides in the root namespace or explicitly handle the namespace prefix logic internally so it doesn't leak into your registry keys.
This behavior highlights a common trade-off in dynamic PHP development: flexibility versus explicitness. While hardcoding looks rigid, it leverages the language's implicit scoping rules and automatic loading mechanisms. Dynamic instantiation gives you control but demands that you manually satisfy the autoloader's requirements regarding namespace resolution.