{"record":{"id":"e30be6f8846b9667","repo":"doctrine/instantiator","slug":"the-provided-type-s-is-a-trait-and-cannot-be-i","errorCode":null,"errorMessage":"The provided type \"%s\" is a trait, and cannot be instantiated","messagePattern":"The provided type \"(.+?)\" is a trait, and cannot be instantiated","errorType":"exception","errorClass":"Doctrine\\Instantiator\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Exception/InvalidArgumentException.php","lineNumber":26,"sourceCode":"use 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',\n            $reflectionClass->getName(),\n        ));\n    }\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/doctrine/instantiator/blob/cbb879d6eea7ce605d95f4c455679df73ee682ea/src/Exception/InvalidArgumentException.php#L8-L44","documentation":"doctrine/instantiator builds constructor-less instances only for concrete classes: its gate in getReflectionClass() (src/Instantiator.php:143) requires class_exists(), which is false for traits. fromNonExistingClass() then checks trait_exists() and, finding the name is a trait, reports that it cannot be instantiated. The error indicates the string passed to Instantiator::instantiate() is a trait name rather than a class name.","triggerScenarios":"Calling (new Instantiator())->instantiate(AuditableTrait::class) or any trait FQCN. Traits fail class_exists(), so fromNonExistingClass() executes; trait_exists($className) returns true and the trait message variant is thrown. Happens when fixture factories, hydration frameworks, or code-generation tools feed a list of declared types into instantiate() without filtering traits.","commonSituations":"A configuration or mapping table stores trait names alongside class names and the wrong row is selected; refactor moved behavior into a trait but a call site still passes the old name; generic tooling iterates all declared types (class_exists || interface_exists || trait_exists) from a manifest and passes traits to the instantiator.","solutions":["Pass the concrete class that uses the trait instead of the trait itself, e.g. instantiate(User::class) rather than AuditableTrait::class.","Filter type lists before instantiation: skip any name where trait_exists($name) is true.","If the name arrives dynamically (config, DB), validate it with class_exists($name) before calling instantiate() and fail with a clear message naming the offending entry."],"exampleFix":"// before\n$object = $instantiator->instantiate(AuditableTrait::class);\n// InvalidArgumentException: The provided type \"AuditableTrait\" is a trait,\n// and cannot be instantiated\n\n// after: instantiate a class that consumes the trait\n$object = $instantiator->instantiate(User::class);","handlingStrategy":"validation","validationCode":"use function trait_exists;\n\nif (trait_exists($className)) {\n    throw new LogicException(sprintf('%s is a trait and cannot be instantiated.', $className));\n}\n\n$object = (new Instantiator())->instantiate($className);","typeGuard":"function isInstantiableClassName(string $className): bool\n{\n    return class_exists($className) && ! trait_exists($className, false);\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 a trait')) {\n        // the name pointed at a trait: locate the concrete class that uses it\n    }\n    throw $e;\n}","preventionTips":["Keep traits and classes in separate config sections so trait names never reach type-to-instance factories.","Filter any dynamic type list with class_exists() before instantiating.","After moving code into a trait, update call sites that still reference the old class name."],"tags":["php","instantiation","trait","reflection"],"backgroundTag":"cannot-instantiate-trait","analyzedSha":"cbb879d6eea7ce605d95f4c455679df73ee682ea","analyzedAt":"2026-08-21T03:06:19.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}