{"record":{"id":"6eb7b88968afbba9","repo":"yiisoft/yii2","slug":"unsupported-configuration-type-gettype-type","errorCode":null,"errorMessage":"Unsupported configuration type: ' . gettype($type)","messagePattern":"Unsupported configuration type: ' \\. gettype\\(\\$type\\)","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/BaseYii.php","lineNumber":359,"sourceCode":"     *   The callable should return a new instance of the object being created.\n     *\n     * @param array $params the constructor parameters\n     * @return T the created object\n     * @throws InvalidConfigException if the configuration is invalid.\n     * @see \\yii\\di\\Container\n     */\n    public static function createObject($type, array $params = [])\n    {\n        if (is_string($type)) {\n            return static::$container->get($type, $params);\n        }\n\n        if (is_callable($type, true)) {\n            return static::$container->invoke($type, $params);\n        }\n\n        if (!is_array($type)) {\n            throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));\n        }\n\n        if (isset($type['__class'])) {\n            $class = $type['__class'];\n            unset($type['__class'], $type['class']);\n            return static::$container->get($class, $params, $type);\n        }\n\n        if (isset($type['class'])) {\n            $class = $type['class'];\n            unset($type['class']);\n            return static::$container->get($class, $params, $type);\n        }\n\n        throw new InvalidConfigException('Object configuration must be an array containing a \"class\" or \"__class\" element.');\n    }\n\n    private static $_logger;","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/BaseYii.php#L341-L377","documentation":"Yii::createObject() is the framework factory: it accepts a class-name string (resolved through the DI container), anything is_callable (invoked through the container), or a configuration array. Any other type - null, int, float, bool, resource, or an object without __invoke() - fails all three checks and throws InvalidConfigException('Unsupported configuration type: <type>'); the message tells you exactly what type arrived.","triggerScenarios":"Yii::createObject(Yii::$app->params['factory']) where the params key is missing so null is passed; config parsed from JSON/YAML/env vars that yields a scalar instead of an array or class name; passing a plain object expecting it to be treated as configuration (objects are not configs); a variable that was conditionally initialized and is null on some code path.","commonSituations":"Config-driven plugin systems where a nested key was silently dropped; refactors where a variable that used to hold a class name now holds null; external data (JSON settings) fed into createObject without validation.","solutions":["Inspect the exact value right before the call (var_dump($type) or a breakpoint) and fix the producer so it yields a class name, callable, or config array","Branch on null/missing values before calling createObject instead of passing them through","If you intended a factory call, make the value a closure or an [object, 'method'] array - verify with is_callable($type, true)"],"exampleFix":"// before\n$builder = Yii::$app->params['cacheBuilder']; // key missing => null\n$cache = Yii::createObject($builder);\n\n// after\n$builder = Yii::$app->params['cacheBuilder'] ?? \\yii\\caching\\ArrayCache::class;\n$cache = Yii::createObject($builder);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function createObjectSafe($type, array $params = [])\n{\n    if (is_string($type) || is_array($type) || is_callable($type, true)) {\n        return Yii::createObject($type, $params);\n    }\n    throw new \\InvalidArgumentException(\n        'createObject expects a class name, callable, or config array, got ' . gettype($type)\n    );\n}","tryCatchPattern":"try {\n    $object = Yii::createObject($type);\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    Yii::error('Bad factory input: ' . $e->getMessage());\n    throw $e;\n}","preventionTips":["Type factory/config inputs as string|array|callable in docblocks and validate at the boundary","Never pass possibly-null config values straight into createObject","Add a boot smoke test that instantiates every configured factory entry"],"tags":["di-container","createobject","configuration","factory"],"backgroundTag":"invalid-config-type","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}