{"record":{"id":"07273f65f38140e8","repo":"doctrine/instantiator","slug":"the-provided-class-s-is-abstract-and-cannot-be","errorCode":null,"errorMessage":"The provided class \"%s\" is abstract, and cannot be instantiated","messagePattern":"The provided class \"(.+?)\" is abstract, and cannot be instantiated","errorType":"exception","errorClass":"Doctrine\\Instantiator\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Exception/InvalidArgumentException.php","lineNumber":40,"sourceCode":"            return new self(sprintf('The provided type \"%s\" is an interface, and cannot be instantiated', $className));\n        }\n\n        if (trait_exists($className)) {\n            return new self(sprintf('The provided type \"%s\" is a trait, and cannot be instantiated', $className));\n        }\n\n        return new self(sprintf('The provided class \"%s\" does not exist', $className));\n    }\n\n    /**\n     * @phpstan-param ReflectionClass<T> $reflectionClass\n     *\n     * @template T of object\n     */\n    public static function fromAbstractClass(ReflectionClass $reflectionClass): self\n    {\n        return new self(sprintf(\n            'The provided class \"%s\" is abstract, and cannot be instantiated',\n            $reflectionClass->getName(),\n        ));\n    }\n\n    public static function fromEnum(string $className): self\n    {\n        return new self(sprintf(\n            'The provided class \"%s\" is an enum, and cannot be instantiated',\n            $className,\n        ));\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":53,"githubUrl":"https://github.com/doctrine/instantiator/blob/cbb879d6eea7ce605d95f4c455679df73ee682ea/src/Exception/InvalidArgumentException.php#L22-L53","documentation":"Thrown by fromAbstractClass() when getReflectionClass() (src/Instantiator.php:153-154) finds the requested class is abstract. Even though the instantiator bypasses constructors via ReflectionClass::newInstanceWithoutConstructor(), PHP forbids creating instances of abstract classes, so the library refuses up front. The message names the abstract class so you can redirect the call to a concrete subclass.","triggerScenarios":"Calling (new Instantiator())->instantiate(AbstractController::class), AbstractRepository::class, or any abstract base FQCN. The ReflectionClass built at src/Instantiator.php:151 reports isAbstract() === true and fromAbstractClass($reflection) is thrown with the class name from $reflectionClass->getName().","commonSituations":"A factory or container resolves by abstract type name and forgot to map it to a concrete implementation; a base class was made abstract during a framework/library upgrade but call sites still reference it; fixture/prototype tooling receives 'base entity' names from metadata that lists parents instead of leaf classes.","solutions":["Instantiate a concrete subclass instead: pass UserRepository::class rather than AbstractRepository::class.","If the abstract type comes from a container binding or config, fix the mapping so the concrete implementation name is resolved before calling instantiate().","After a dependency upgrade, grep call sites for the newly abstract class name and update them to the concrete replacements."],"exampleFix":"// before\n$repository = $instantiator->instantiate(AbstractRepository::class);\n// InvalidArgumentException: The provided class \"AbstractRepository\" is abstract,\n// and cannot be instantiated\n\n// after: ask for the concrete subclass\n$repository = $instantiator->instantiate(UserRepository::class);","handlingStrategy":"validation","validationCode":"$reflection = new ReflectionClass($className);\n\nif ($reflection->isAbstract()) {\n    throw new LogicException(sprintf('%s is abstract: pass a concrete subclass.', $className));\n}\n\n$object = (new Instantiator())->instantiate($className);","typeGuard":"function isConcreteClass(string $className): bool\n{\n    return class_exists($className)\n        && ! (new ReflectionClass($className))->isAbstract();\n}","tryCatchPattern":"use Doctrine\\Instantiator\\Exception\\InvalidArgumentException;\n\ntry {\n    $instance = $instantiator->instantiate($className);\n} catch (InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'is abstract')) {\n        // resolve the abstract type to a mapped concrete implementation, then retry\n        $className = $this->container->getConcreteClass($className);\n        return $instantiator->instantiate($className);\n    }\n    throw $e;\n}","preventionTips":["Make container bindings map abstract types to concrete implementations and resolve before instantiating.","Filter metadata-driven type lists with ReflectionClass::isAbstract().","After upgrading a dependency that made a base class abstract, grep call sites for the old name."],"tags":["php","instantiation","abstract-class","reflection"],"backgroundTag":"cannot-instantiate-abstract-class","analyzedSha":"cbb879d6eea7ce605d95f4c455679df73ee682ea","analyzedAt":"2026-08-21T03:06:19.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}