{"record":{"id":"5ca7e44b3ec89e34","repo":"phalcon/cphalcon","slug":"data-for-the-serializer-must-be-of-type-string","errorCode":null,"errorMessage":"Data for the serializer must be of type string","messagePattern":"Data for the serializer must be of type string","errorType":"exception","errorClass":"Phalcon\\Storage\\Serializer\\Exceptions\\InvalidSerializationInput","httpStatus":null,"severity":"error","filePath":"phalcon/Storage/Serializer/Base64.zep","lineNumber":27,"sourceCode":" */\n\nnamespace Phalcon\\Storage\\Serializer;\n\nuse Phalcon\\Storage\\Serializer\\Exceptions\\InvalidSerializationInput;\nuse Phalcon\\Storage\\Serializer\\Exceptions\\InvalidUnserializationInput;\nuse Phalcon\\Traits\\Php\\Base64Trait;\n\nclass Base64 extends AbstractSerializer\n{\n    use Base64Trait;\n\n    /**\n     * Serializes data\n     */\n    public function serialize() -> string\n    {\n        if typeof this->data !== \"string\" {\n            throw new InvalidSerializationInput();\n        }\n\n        return this->phpBase64Encode(this->data);\n    }\n\n    /**\n     * Unserializes data\n     */\n    public function unserialize(mixed data) -> void\n    {\n        var result;\n\n        if typeof data !== \"string\" {\n            throw new InvalidUnserializationInput();\n        }\n\n        let result = this->phpBase64Decode(data, true);\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Storage/Serializer/Base64.zep#L9-L45","documentation":"Phalcon\\Storage\\Serializer\\Base64 only encodes strings: serialize() throws InvalidSerializationInput when the data assigned via setData()/constructor is not a string (int, float, bool, array, object). It surfaces from cache adapters whose defaultSerializer is base64 when a non-string value is stored.","triggerScenarios":"Configuring 'defaultSerializer' => 'base64' on a Storage adapter (or building the serializer directly) and calling $cache->set('k', ['a' => 1]) or set('k', 42); calling (new Base64($data))->serialize() with mixed data.","commonSituations":"Config copied from a string-only cache into an app that now stores arrays/objects; choosing base64 to avoid PHP unserialize() and then storing structured data; numeric counters stored via increment-adjacent code paths.","solutions":["Switch the adapter to the Php (or Json) serializer for mixed-type payloads","Keep base64 only for string caches and json_encode()/base64_encode() structured values yourself before set()","Cast scalars to string when the value is known to be scalar: $cache->set('k', (string) $count)"],"exampleFix":"// before\nnew Redis($factory, ['defaultSerializer' => SerializerFactory::PHP]);\n// ... elsewhere: (new Base64($data))->serialize() with $data = ['a' => 1] -> throws\n\n// after\n(new \\Phalcon\\Storage\\Serializer\\Php($data))->serialize(); // handles mixed types","handlingStrategy":"type-guard","validationCode":"if (!is_string($value) && $adapterConfig['defaultSerializer'] === 'base64') {\n    $value = json_encode($value); // or switch adapter serializer to 'php'\n}\n$cache->set($key, $value);","typeGuard":"function isBase64Serializable(mixed $data): bool\n{\n    return is_string($data);\n}","tryCatchPattern":null,"preventionTips":["Reserve the Base64 serializer for caches that only ever hold strings","Use Php or Json serializer when values include arrays, objects or scalars","Encode structured values yourself at the boundary when base64 storage is a hard requirement"],"tags":["php","phalcon","serialization","base64","cache","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}