{"record":{"id":"16da722a3444e5bd","repo":"cakephp/cakephp","slug":"alias-s-is-not-an-existing-class-and-therefore-cannot-be","errorCode":null,"errorMessage":"Alias (%s) is not an existing class and therefore cannot be resolved","messagePattern":"Alias \\((.+?)\\) is not an existing class and therefore cannot be resolved","errorType":"exception","errorClass":"NotFoundException","httpStatus":null,"severity":"error","filePath":"src/Container/ReflectionContainer.php","lineNumber":49,"sourceCode":"     * @param bool $cacheResolutions\n     */\n    public function __construct(bool $cacheResolutions = false)\n    {\n        $this->cacheResolutions = $cacheResolutions;\n    }\n\n    /**\n     * @inheritDoc\n     */\n    public function get(string $id, array $args = [])\n    {\n        // Only use cache when no custom args are provided\n        if ($this->cacheResolutions && $args === [] && array_key_exists($id, $this->cache)) {\n            return $this->cache[$id];\n        }\n\n        if (!$this->has($id)) {\n            throw new NotFoundException(\n                sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id),\n            );\n        }\n\n        /** @var class-string $id */\n        $reflector = new ReflectionClass($id);\n        $construct = $reflector->getConstructor();\n\n        if ($construct && !$construct->isPublic()) {\n            throw new NotFoundException(\n                sprintf('Alias (%s) has a non-public constructor and therefore cannot be instantiated', $id),\n            );\n        }\n\n        $resolution = $construct === null\n            ? new $id()\n            : $reflector->newInstanceArgs($this->reflectArguments($construct, $args));\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/Container/ReflectionContainer.php#L31-L67","documentation":"ReflectionContainer::get() resolves services by instantiating the class named by $id using reflection. Before doing so it checks $this->has($id), which requires $id to be an existing class (or interface with implementations resolvable); otherwise it throws NotFoundException because a non-class alias cannot be reflected. Unlike the full Container, reflection resolution only works for real class names.","triggerScenarios":"$reflectionContainer->get('service.alias') where the alias is a string id, not a FQCN; calling get() with a class name that was never autoloaded (missing require/include, composer dump-autoload not run); asking for an interface without a registered alias.","commonSituations":"Using the reflection container standalone (not as a delegate of the main container) with string-based service ids; autoloader misconfiguration after moving a class; typos in namespaces (case-sensitive class names).","solutions":["Pass the fully-qualified class name instead of a service alias, or register the alias in the main container and resolve through it","Attach the ReflectionContainer as a delegate ($container->delegate($reflection)) so aliases resolve via definitions first","Fix autoloading: run composer dump-autoload and verify class_exists($id) is true","Check the namespace/casing of the class name"],"exampleFix":"// before\n$db = $reflectionContainer->get('db'); // alias, not a class\n// after\n$db = $container->get('db'); // main container, or:\n$db = $reflectionContainer->get(App\\Db::class);","handlingStrategy":"validation","validationCode":"if (!class_exists($id) && !interface_exists($id)) {\n    throw new LogicException(\"ReflectionContainer requires a real class; '$id' is not autoloadable\");\n}","typeGuard":"function isReflectionResolvable(string $id): bool {\n    return class_exists($id) || interface_exists($id);\n}","tryCatchPattern":"try {\n    $obj = $reflectionContainer->get($id);\n} catch (NotFoundException $e) {\n    $obj = $container->has($id) ? $container->get($id) : null;\n}","preventionTips":["Pass ::class constants, never string aliases, to the reflection container","Use the reflection container as a delegate of the main container rather than standalone","Run composer dump-autoload after adding/moving classes and smoke-test class_exists"],"tags":["php","dependency-injection","reflection","autoloading"],"backgroundTag":"class-not-found","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}