{"record":{"id":"8e789fc6aaa71eca","repo":"doctrine/instantiator","slug":"the-provided-type-s-is-an-interface-and-cannot","errorCode":null,"errorMessage":"The provided type \"%s\" is an interface, and cannot be instantiated","messagePattern":"The provided type \"(.+?)\" is an interface, and cannot be instantiated","errorType":"exception","errorClass":"Doctrine\\Instantiator\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Exception/InvalidArgumentException.php","lineNumber":22,"sourceCode":"\nnamespace Doctrine\\Instantiator\\Exception;\n\nuse InvalidArgumentException as BaseInvalidArgumentException;\nuse ReflectionClass;\n\nuse function interface_exists;\nuse function sprintf;\nuse function trait_exists;\n\n/**\n * Exception for invalid arguments provided to the instantiator\n */\nclass InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface\n{\n    public static function fromNonExistingClass(string $className): self\n    {\n        if (interface_exists($className)) {\n            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',","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/doctrine/instantiator/blob/cbb879d6eea7ce605d95f4c455679df73ee682ea/src/Exception/InvalidArgumentException.php#L4-L40","documentation":"doctrine/instantiator creates objects without invoking their constructors. Its internal getReflectionClass() gate (src/Instantiator.php:143) requires class_exists() to return true; interfaces never satisfy that, so fromNonExistingClass() runs and detects via interface_exists() that the given name is an interface. The error means the string passed to Instantiator::instantiate() names an interface type, which can never be instantiated directly in PHP.","triggerScenarios":"Calling (new Instantiator())->instantiate($className) where $className is an interface FQCN, e.g. Psr\\Log\\LoggerInterface::class or Doctrine\\Instantiator\\InstantiatorInterface::class. class_exists() returns false for interfaces, control enters fromNonExistingClass(), interface_exists($className) is true, and the interface message variant is returned. Typical caller code: DI containers, fixture/proxy factories, or test libraries that feed a type map straight into instantiate().","commonSituations":"A service container or config file maps an identifier to an interface instead of a concrete implementation; generic factory code iterates 'all types' (classes, interfaces, traits) from reflection or a manifest and passes each to instantiate(); a variable that was supposed to hold the resolved implementation still holds the abstraction name after a refactor.","solutions":["Resolve the interface to a concrete implementing class before calling instantiate(), e.g. look up the container binding and pass FileLogger::class instead of LoggerInterface::class.","If the class name comes from configuration, fix the mapping entry so it points to the concrete class.","If you iterate lists of type names, filter them first: keep only names where class_exists($name) is true (interfaces and traits fail this check automatically)."],"exampleFix":"// before\n$logger = (new Instantiator())->instantiate(LoggerInterface::class);\n// InvalidArgumentException: The provided type \"LoggerInterface\" is an interface,\n// and cannot be instantiated\n\n// after: pass the concrete implementation, not the abstraction\n$logger = (new Instantiator())->instantiate(FileLogger::class);","handlingStrategy":"validation","validationCode":"use function interface_exists;\n\nif (interface_exists($className)) {\n    throw new LogicException(\n        sprintf('%s is an interface: resolve a concrete implementation before instantiating.', $className)\n    );\n}\n\n$object = (new Instantiator())->instantiate($className);","typeGuard":"/** @phpstan-assert-if-true !interface-string $className */\nfunction isInstantiableClassName(string $className): bool\n{\n    return class_exists($className) && ! interface_exists($className, false);\n}","tryCatchPattern":"use Doctrine\\Instantiator\\Exception\\InvalidArgumentException;\n\ntry {\n    $instance = $instantiator->instantiate($className);\n} catch (InvalidArgumentException $e) {\n    // bad input: interface/trait/missing/abstract/enum name — surface it at the config boundary\n    throw new InvalidTypeConfiguration($e->getMessage(), 0, $e);\n}","preventionTips":["Always resolve container bindings to concrete class names before calling instantiate().","Filter type lists with class_exists($name) — it is false for interfaces and traits automatically.","Prefer ClassName::class constants over hand-written strings so static analysis can verify types."],"tags":["php","instantiation","interface","reflection","doctrine-instantiator"],"backgroundTag":"cannot-instantiate-interface","analyzedSha":"cbb879d6eea7ce605d95f4c455679df73ee682ea","analyzedAt":"2026-08-21T03:06:19.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}