Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Serializer Laravel Package

laminas/laminas-serializer

Serialize and deserialize PHP data using multiple adapters (e.g., PHP native, JSON, XML, WDDX) with consistent options, error handling, and extensibility. Useful for caching, sessions, message payloads, and storage across different formats.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require laminas/laminas-serializer

Then, create a serializer instance with your preferred adapter. The most common choice is Json for interoperability (especially in APIs), or PhpSerialized for performance in internal storage (e.g., caching):

use Laminas\Serializer\Serializer;

// For JSON (safe for API payloads and cross-language compatibility)
$serializer = new Serializer(Serializer::ADAPTER_JSON);

// For PHP-specific serialization (e.g., Redis cache, internal storage)
$serializer = new Serializer(Serializer::ADAPTER_PHP_SERIALIZE);

Your first use case? Storing complex PHP arrays or objects in a cache:

$data = ['user' => 'alice', 'permissions' => ['read', 'write']];
$cache->save($serializer->serialize($data), 'user_perms_alice');

Implementation Patterns

  • Adapter Abstraction: Inject SerializerInterface as a dependency (e.g., via a factory) to keep your code decoupled from the underlying format. Configure the adapter in config/autoload/serializer.global.php.
  • Caching Layer Integration: Wrap cache adapters (e.g., Redis, APCu) with your serializer—avoid calling serialize()/unserialize() manually.
  • API Response Handling: Use Json adapter for APIs, but always set Json::encodeOptions to exclude JSON_PRESERVE_ZERO_FRACTION and include JSON_THROW_ON_ERROR (if using laminas/laminas-serializer ≥ v2.18):
    $serializer = new Serializer(Serializer::ADAPTER_JSON, [
        'adapterOptions' => [
            'encodeOptions' => JSON_THROW_ON_ERROR,
        ],
    ]);
    
  • Custom Adapters: Extend Laminas\Serializer\Adapter\AdapterInterface for niche needs (e.g., Protocol Buffers, MessagePack). Register via the AdapterManager or directly instantiate with setAdapter().

Gotchas and Tips

  • Igbinary is Optional: Though supported, Igbinary requires manual installation. Don’t assume it’s available—catch RuntimeException on instantiation or fallback gracefully.
  • PHP Serialized ≠ JSON: PhpSerialized stores stdClass and internal class names. If you unmarshal into different class versions (e.g., after refactor), you’ll get __PHP_Incomplete_Class. Prefer Json for long-lived storage or microservice boundaries.
  • Unserialize() is Unsafe: Never pass untrusted input directly to unserialize(). The package mitigates this somewhat via exceptions, but always validate or decode via Json for public-facing endpoints.
  • Empty Arrays vs Objects: JSON adapter serializes empty arrays as [] (JSON), but with PhpSerialized, empty arrays are a:0:{}—watch for this in cache key collisions or diff tools.
  • Exception Handling: Catch Laminas\Serializer\Exception\RuntimeException for format errors (e.g., corrupted cache entries) and Laminas\Serializer\Exception\LogicException for misconfiguration (e.g., unsupported adapter name). Log the raw payload in dev environments for debugging.
  • PHP 8.2+ Compatibility: Avoid serialize()-compatible adapters for objects with __serialize() if you need strict type fidelity—PhpSerialized adapter preserves __sleep()/__wakeup() semantics, but JSON loses class identity by design.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4