phalcon/cphalcon · error · Phalcon\Storage\Serializer\Exceptions\InvalidSerializationInput

Data for the serializer must be of type string

Error message

Data for the serializer must be of type string

What it means

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.

Source

Thrown at phalcon/Storage/Serializer/Base64.zep:27

 */

namespace Phalcon\Storage\Serializer;

use Phalcon\Storage\Serializer\Exceptions\InvalidSerializationInput;
use Phalcon\Storage\Serializer\Exceptions\InvalidUnserializationInput;
use Phalcon\Traits\Php\Base64Trait;

class Base64 extends AbstractSerializer
{
    use Base64Trait;

    /**
     * Serializes data
     */
    public function serialize() -> string
    {
        if typeof this->data !== "string" {
            throw new InvalidSerializationInput();
        }

        return this->phpBase64Encode(this->data);
    }

    /**
     * Unserializes data
     */
    public function unserialize(mixed data) -> void
    {
        var result;

        if typeof data !== "string" {
            throw new InvalidUnserializationInput();
        }

        let result = this->phpBase64Decode(data, true);

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Switch the adapter to the Php (or Json) serializer for mixed-type payloads
  2. Keep base64 only for string caches and json_encode()/base64_encode() structured values yourself before set()
  3. Cast scalars to string when the value is known to be scalar: $cache->set('k', (string) $count)

Example fix

// before
new Redis($factory, ['defaultSerializer' => SerializerFactory::PHP]);
// ... elsewhere: (new Base64($data))->serialize() with $data = ['a' => 1] -> throws

// after
(new \Phalcon\Storage\Serializer\Php($data))->serialize(); // handles mixed types
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_string($value) && $adapterConfig['defaultSerializer'] === 'base64') {
    $value = json_encode($value); // or switch adapter serializer to 'php'
}
$cache->set($key, $value);

Type guard

function isBase64Serializable(mixed $data): bool
{
    return is_string($data);
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/5ca7e44b3ec89e34. Report an issue: GitHub.