{"record":{"id":"1c5d3272e47c7c4d","repo":"phalcon/cphalcon","slug":"the-object-is-read-only","errorCode":null,"errorMessage":"The object is read only","messagePattern":"The object is read only","errorType":"exception","errorClass":"Phalcon\\Support\\Collection\\Exceptions\\ReadOnlyViolation","httpStatus":null,"severity":"error","filePath":"phalcon/Support/Collection/ReadOnlyCollection.zep","lineNumber":73,"sourceCode":"        let this->constructed = false;\n\n        try {\n            parent::__unserialize(data);\n        } catch \\Throwable, ex {\n            let this->constructed = true;\n\n            throw ex;\n        }\n\n        let this->constructed = true;\n    }\n\n    /**\n     * @throws ReadOnlyViolation\n     */\n    public function clear() -> void\n    {\n        throw new ReadOnlyViolation();\n    }\n\n    /**\n     * @throws ReadOnlyViolation\n     */\n    public function init(array data = []) -> void\n    {\n        if (this->constructed) {\n            throw new ReadOnlyViolation();\n        }\n\n        parent::init(data);\n    }\n\n    /**\n     * Delete the element from the collection\n     *\n     * @throws ReadOnlyViolation","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Support/Collection/ReadOnlyCollection.zep#L55-L91","documentation":"Phalcon\\Support\\Collection\\ReadOnlyCollection is the immutable variant of Collection: it initializes once in the constructor and then all mutators are locked. clear() unconditionally throws Phalcon\\Support\\Collection\\Exceptions\\ReadOnlyViolation (\"The object is read only\") because wiping the data would mutate frozen state. The class exists so components can hand out safe, shareable views of data.","triggerScenarios":"Calling ->clear() on a ReadOnlyCollection obtained from a library or another component; reusing generic 'clear then repopulate' code written for the mutable Collection: $col->clear(); $col->init($newData);","commonSituations":"Library-returned value objects or shared config exposed as read-only on purpose. Generic collection utilities (cache reset, batch refresh) break when pointed at the immutable variant.","solutions":["Build a new object instead of clearing: $mutable = new Collection($readOnly->toArray()); then mutate the copy","Return a fresh empty instance if you need a 'cleared' value: new ReadOnlyCollection([])","Guard shared helpers: check instanceof ReadOnlyCollection before calling any mutator"],"exampleFix":"// before\n$collection->clear(); // throws ReadOnlyViolation\n\n// after\n$collection = new Collection([]); // new instance, source untouched","handlingStrategy":"type-guard","validationCode":"use Phalcon\\Support\\Collection;\nuse Phalcon\\Support\\Collection\\ReadOnlyCollection;\n\nif ($collection instanceof ReadOnlyCollection) {\n    $collection = new Collection($collection->toArray()); // mutable copy\n}\n\n$collection->clear();","typeGuard":"function isReadOnlyCollection($collection): bool\n{\n    return $collection instanceof \\Phalcon\\Support\\Collection\\ReadOnlyCollection;\n}","tryCatchPattern":"use Phalcon\\Support\\Collection\\Exceptions\\ReadOnlyViolation;\n\ntry {\n    $collection->clear();\n} catch (ReadOnlyViolation $e) {\n    $collection = new \\Phalcon\\Support\\Collection($collection->toArray());\n}","preventionTips":["Treat collections you do not own as read-only until proven mutable","Write shared helpers against CollectionInterface and branch on ReadOnlyCollection before mutating","Prefer building new instances over in-place clear/replace when porting generic collection code"],"tags":["phalcon","collection","immutability","read-only"],"backgroundTag":"immutable-collection-mutation","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}