yiisoft/yii2 · error · InvalidConfigException
Object configuration must be an array containing a "class" o
Error message
Object configuration must be an array containing a "class" or "__class" element.
What it means
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).
Source
Thrown at framework/BaseYii.php:374
}
if (!is_array($type)) {
throw new InvalidConfigException('Unsupported configuration type: ' . gettype($type));
}
if (isset($type['__class'])) {
$class = $type['__class'];
unset($type['__class'], $type['class']);
return static::$container->get($class, $params, $type);
}
if (isset($type['class'])) {
$class = $type['class'];
unset($type['class']);
return static::$container->get($class, $params, $type);
}
throw new InvalidConfigException('Object configuration must be an array containing a "class" or "__class" element.');
}
private static $_logger;
/**
* @return Logger message logger
*/
public static function getLogger()
{
if (self::$_logger !== null) {
return self::$_logger;
}
return self::$_logger = static::createObject('yii\log\Logger');
}
/**
* Sets the logger object.View on GitHub (pinned to 66f00d18a2)
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
Example fix
// before
$mutex = Yii::createObject(['expire' => 30]);
// after
$mutex = Yii::createObject([
'class' => \yii\mutex\FileMutex::class,
'expire' => 30,
]); Defensive patterns
Strategy: validation
Validate before calling
if (is_array($config) && !isset($config['class']) && !isset($config['__class'])) {
throw new \InvalidArgumentException('Config array needs a class or __class key');
}
$object = Yii::createObject($config); Try / catch
try {
$object = Yii::createObject($config);
} catch (\yii\base\InvalidConfigException $e) {
// message: Object configuration must be an array containing a "class" or "__class" element.
Yii::error('Missing class key in config: ' . print_r($config, true));
throw $e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unsupported configuration type: ' . gettype($type)
- Invalid path alias: $alias
- The "id" configuration for the Application is required.
- The "basePath" configuration for the Application is required
- Unknown bootstrapping component ID: $mixed
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/d8fed80bfc03ada5.
Report an issue: GitHub.