yiisoft/yii2 · warning · InvalidConfigException
Failed to instantiate class "Instance". Required parameter "
Error message
Failed to instantiate class "Instance". Required parameter "id" is missing
What it means
yii\di\Instance implements magic __set_state() so that objects exported with var_export() can be reconstructed when the generated code is eval'd/included. If the exported state array lacks the 'id' key, reconstruction is impossible and __set_state() throws InvalidConfigException 'Failed to instantiate class "Instance". Required parameter "id" is missing'. In normal use var_export() always includes public properties, so this fires only when the state array was hand-built or altered.
Source
Thrown at framework/di/Instance.php:211
return null;
}
throw $e;
}
}
/**
* Restores class state after using `var_export()`.
*
* @param array $state
* @return Instance
* @throws InvalidConfigException when $state property does not contain `id` parameter
* @see https://www.php.net/manual/en/function.var-export.php
* @since 2.0.12
*/
public static function __set_state($state)
{
if (!isset($state['id'])) {
throw new InvalidConfigException('Failed to instantiate class "Instance". Required parameter "id" is missing');
}
return new self($state['id']);
}
}
View on GitHub (pinned to 66f00d18a2)
Solutions
- Ensure the state array passed to Instance::__set_state() contains 'id', e.g. ['id' => 'cache'].
- Don't strip public properties when post-processing var_export() output of Instance objects.
- Prefer PHP serialize()/igbinary or a dedicated cache serializer over var_export for objects.
Example fix
// before $instance = Instance::__set_state([]); // after $instance = Instance::__set_state(['id' => 'cache']);
Defensive patterns
Strategy: validation
Validate before calling
if (!isset($state['id'])) {
throw new \InvalidArgumentException('Cannot restore Instance: state must contain an "id" key.');
}
$instance = \yii\di\Instance::__set_state($state); Prevention
- Never strip public properties from var_export() output that will be eval'd back into objects.
- Prefer serialize()/unserialize() or Igbinary for object caching instead of var_export pipelines.
- If you post-process exported state, assert array_key_exists('id', $state) before including the file.
When it happens
Trigger: Calling Instance::__set_state([]) (or an array without 'id') directly, e.g. in tests or custom serialization code; caching pipelines that var_export() an Instance, strip properties for size, and eval the result later.
Common situations: Opcache/config-cache systems based on var_export(); unit tests exercising __set_state; custom var_export-based dumps that filter keys and accidentally drop 'id'.
Related errors
- Invalid data type: $class. $type is expected.
- The required component is not specified.
- Failed to instantiate component or class "$reference->id".
- "$reference->id" refers to a get_class($component) component
- Invalid data type: $valueType. $type is expected.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/e72353c67235a18c.
Report an issue: GitHub.