{"record":{"id":"5b138c2ef401edec","repo":"yiisoft/yii2","slug":"invalid-data-type-class-type-is-expected","errorCode":null,"errorMessage":"Invalid data type: $class. $type is expected.","messagePattern":"Invalid data type: \\$class\\. \\$type is expected\\.","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/di/Instance.php","lineNumber":141,"sourceCode":"        if (is_array($reference)) {\n            if (!$container instanceof Container) {\n                $container = Yii::$container;\n            }\n            if (isset($reference['__class'])) {\n                $class = $reference['__class'];\n                unset($reference['__class'], $reference['class']);\n            } elseif (isset($reference['class'])) {\n                $class = $reference['class'];\n                unset($reference['class']);\n            } else {\n                $class = $type;\n            }\n            $component = $container->get($class, [], $reference);\n            if ($type === null || $component instanceof $type) {\n                return $component;\n            }\n\n            throw new InvalidConfigException('Invalid data type: ' . $class . '. ' . $type . ' is expected.');\n        } elseif (empty($reference)) {\n            throw new InvalidConfigException('The required component is not specified.');\n        }\n\n        if (is_string($reference)) {\n            $reference = new static($reference);\n        } elseif ($type === null || $reference instanceof $type) {\n            return $reference;\n        }\n\n        if ($reference instanceof self) {\n            try {\n                $component = $reference->get($container);\n            } catch (\\ReflectionException $e) {\n                throw new InvalidConfigException('Failed to instantiate component or class \"' . $reference->id . '\".', 0, $e);\n            }\n            if ($type === null || $component instanceof $type) {\n                return $component;","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/Instance.php#L123-L159","documentation":"yii\\di\\Instance::ensure() resolved the given array configuration through the container, but the resulting object is not an instance of the expected $type. ensure() is Yii's mechanism for accepting either a component ID, a config array, or an object wherever a component of a certain class/interface is required; when the array's '__class'/'class' entry produces an incompatible object, it throws InvalidConfigException naming the class it got and the type it expected.","triggerScenarios":"Passing an inline config array to a typed component slot, e.g. 'mailer' => ['__class' => app\\components\\SmsSender::class] where the property is documented/typed as yii\\mail\\MailerInterface and validated via Instance::ensure($value, MailerInterface::class).","commonSituations":"Configuring framework components inline in config/web.php with the wrong class; swapping a component implementation that does not implement the required interface (custom cache, custom mailer); copy-pasted config between components with different base types.","solutions":["Set '__class'/'class' to a class that implements/subclasses the expected type (e.g. a Mailer implementation for a mailer slot).","If you have a custom implementation, make it extend/implement the expected base class or interface.","Register the object under a component ID and pass the ID string instead of an inline array, after fixing the registration.","Remove an overly specific Instance::ensure() type constraint if any implementation is actually acceptable."],"exampleFix":"// before - property expects yii\\mail\\MailerInterface\n'mailer' => ['__class' => app\\components\\SmsSender::class],\n\n// after\n'mailer' => ['__class' => yii\\swiftmailer\\Mailer::class],\n// or make SmsSender implement yii\\mail\\MailerInterface","handlingStrategy":"type-guard","validationCode":"// before passing an inline config to a typed slot\nif (is_array($config) && isset($config['class']) && !is_subclass_of($config['class'], $expectedType)) {\n    throw new \\InvalidArgumentException('Configured class does not satisfy ' . $expectedType);\n}\n$component = \\yii\\di\\Instance::ensure($config, $expectedType);","typeGuard":"/** Narrows a mixed value to something Instance::ensure() can accept for $type. */\nfunction isComponentReference($value, string $type): bool\n{\n    if ($value instanceof $type) {\n        return true;\n    }\n    if (is_string($value)) {\n        return Yii::$app->has($value) || class_exists($value);\n    }\n    if (is_array($value)) {\n        $class = $value['__class'] ?? $value['class'] ?? null;\n        return $class !== null && is_a($class, $type, true);\n    }\n    return false;\n}","tryCatchPattern":"use yii\\base\\InvalidConfigException;\n\ntry {\n    $mailer = \\yii\\di\\Instance::ensure($config, \\yii\\mail\\MailerInterface::class);\n} catch (InvalidConfigException $e) {\n    // message names the class produced vs expected - surface as a boot-time config error\n    Yii::error($e->getMessage(), 'config');\n    throw;\n}","preventionTips":["Make custom component implementations extend/implement the documented base type.","Add a startup assertion that is_a($config['class'], RequiredType::class, true) for critical components.","Keep an interface per component slot and validate config in a bootstrap check on deploy.","Never copy component config between slots without checking the expected type in the target property's PHPDoc."],"tags":["yii2","dependency-injection","config","instance","type-mismatch"],"backgroundTag":"di-type-mismatch","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}