{"record":{"id":"e72353c67235a18c","repo":"yiisoft/yii2","slug":"failed-to-instantiate-class-instance-required-p","errorCode":null,"errorMessage":"Failed to instantiate class \"Instance\". Required parameter \"id\" is missing","messagePattern":"Failed to instantiate class \"Instance\"\\. Required parameter \"id\" is missing","errorType":"exception","errorClass":"InvalidConfigException","httpStatus":null,"severity":"warning","filePath":"framework/di/Instance.php","lineNumber":211,"sourceCode":"                return null;\n            }\n            throw $e;\n        }\n    }\n\n    /**\n     * Restores class state after using `var_export()`.\n     *\n     * @param array $state\n     * @return Instance\n     * @throws InvalidConfigException when $state property does not contain `id` parameter\n     * @see https://www.php.net/manual/en/function.var-export.php\n     * @since 2.0.12\n     */\n    public static function __set_state($state)\n    {\n        if (!isset($state['id'])) {\n            throw new InvalidConfigException('Failed to instantiate class \"Instance\". Required parameter \"id\" is missing');\n        }\n\n        return new self($state['id']);\n    }\n}\n","sourceCodeStart":193,"sourceCodeEnd":217,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/di/Instance.php#L193-L217","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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."],"exampleFix":"// before\n$instance = Instance::__set_state([]);\n\n// after\n$instance = Instance::__set_state(['id' => 'cache']);","handlingStrategy":"validation","validationCode":"if (!isset($state['id'])) {\n    throw new \\InvalidArgumentException('Cannot restore Instance: state must contain an \"id\" key.');\n}\n$instance = \\yii\\di\\Instance::__set_state($state);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["yii2","dependency-injection","instance","var-export","serialization"],"backgroundTag":"invalid-object-state","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}