{"record":{"id":"d8fed80bfc03ada5","repo":"yiisoft/yii2","slug":"object-configuration-must-be-an-array-containing-a","errorCode":null,"errorMessage":"Object configuration must be an array containing a \"class\" or \"__class\" element.","messagePattern":"Object configuration must be an array containing a \"class\" or \"__class\" element\\.","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"error","filePath":"framework/BaseYii.php","lineNumber":374,"sourceCode":"        }\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;\n\n    /**\n     * @return Logger message logger\n     */\n    public static function getLogger()\n    {\n        if (self::$_logger !== null) {\n            return self::$_logger;\n        }\n\n        return self::$_logger = static::createObject('yii\\log\\Logger');\n    }\n\n    /**\n     * Sets the logger object.","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/BaseYii.php#L356-L392","documentation":"When Yii::createObject() receives an array it requires a class reference: '__class' or 'class'; every remaining key becomes container/config input. An array with neither key cannot name a class to instantiate, so InvalidConfigException is thrown. Note that '__class' takes precedence and also strips a legacy 'class' key when both are present, and the isset check is case-sensitive ('Class'/'__Class' do not count).","triggerScenarios":"Yii::createObject(['ttl' => 3600]) - pure options with no class key; config assembled with ArrayHelper::merge where a later array overwrote or dropped the map carrying 'class'; keys typo'd as 'Class' or '_class'; arrays decoded from JSON where the class key was removed.","commonSituations":"Overriding a vendor extension's config and replacing instead of merging; dynamically assembled configuration from multiple sources; partial copy-paste of an example snippet whose class line was not copied.","solutions":["Add 'class' => Your\\Class::class as the first key of the array","If the array comes from merging, check merge order and structure - the base array must carry the 'class' key","Dump the array right before the createObject call to see where the key is lost, then fix the producer"],"exampleFix":"// before\n$mutex = Yii::createObject(['expire' => 30]);\n\n// after\n$mutex = Yii::createObject([\n    'class' => \\yii\\mutex\\FileMutex::class,\n    'expire' => 30,\n]);","handlingStrategy":"validation","validationCode":"if (is_array($config) && !isset($config['class']) && !isset($config['__class'])) {\n    throw new \\InvalidArgumentException('Config array needs a class or __class key');\n}\n$object = Yii::createObject($config);","typeGuard":null,"tryCatchPattern":"try {\n    $object = Yii::createObject($config);\n} catch (\\yii\\base\\InvalidConfigException $e) {\n    // message: Object configuration must be an array containing a \"class\" or \"__class\" element.\n    Yii::error('Missing class key in config: ' . print_r($config, true));\n    throw $e;\n}","preventionTips":["Write the class key first in config arrays and end it with the ::class constant so IDEs and refactors track it","Instantiate every 'components' entry in a config smoke test","Pick one convention ('class' or '__class') and use it consistently"],"tags":["di-container","createobject","configuration"],"backgroundTag":"config-missing-class-key","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}