yiisoft/yii2 · error · InvalidConfigException

Unsupported configuration type: ' . gettype($type)

Error message

Unsupported configuration type: ' . gettype($type)

What it means

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.

Source

Thrown at framework/BaseYii.php:359

     *   The callable should return a new instance of the object being created.
     *
     * @param array $params the constructor parameters
     * @return T the created object
     * @throws InvalidConfigException if the configuration is invalid.
     * @see \yii\di\Container
     */
    public static function createObject($type, array $params = [])
    {
        if (is_string($type)) {
            return static::$container->get($type, $params);
        }

        if (is_callable($type, true)) {
            return static::$container->invoke($type, $params);
        }

        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;

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. 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
  2. Branch on null/missing values before calling createObject instead of passing them through
  3. If you intended a factory call, make the value a closure or an [object, 'method'] array - verify with is_callable($type, true)

Example fix

// before
$builder = Yii::$app->params['cacheBuilder']; // key missing => null
$cache = Yii::createObject($builder);

// after
$builder = Yii::$app->params['cacheBuilder'] ?? \yii\caching\ArrayCache::class;
$cache = Yii::createObject($builder);
Defensive patterns

Strategy: type-guard

Type guard

function createObjectSafe($type, array $params = [])
{
    if (is_string($type) || is_array($type) || is_callable($type, true)) {
        return Yii::createObject($type, $params);
    }
    throw new \InvalidArgumentException(
        'createObject expects a class name, callable, or config array, got ' . gettype($type)
    );
}

Try / catch

try {
    $object = Yii::createObject($type);
} catch (\yii\base\InvalidConfigException $e) {
    Yii::error('Bad factory input: ' . $e->getMessage());
    throw $e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/6eb7b88968afbba9. Report an issue: GitHub.