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
Var Exporter

Var Exporter Laravel Package

symfony/var-exporter

Symfony VarExporter lets you export any serializable PHP value to fast, OPcache-friendly PHP code (preserving __sleep/__wakeup, Serializable, __serialize). Includes Instantiator/Hydrator for bypassing constructors, deep cloning, and lazy-loading traits.

View on GitHub
Deep Wiki
Context7

Symfony VarExporter helps you work with the internal state of PHP values and objects in a fast, predictable way. It can export complex, serializable structures to plain PHP code optimized for OPcache, making runtime loading significantly faster and more memory efficient than unserialize().

It also includes utilities to create and populate objects without triggering constructors or lifecycle hooks—useful for caching, hydration, and proxy/lazy-loading scenarios.

  • VarExporter::export() exports any serializable value to PSR-2 compatible PHP code
  • Preserves serialization semantics (__sleep/__wakeup, Serializable, __serialize/__unserialize)
  • Instantiator creates objects without calling constructors or methods
  • Hydrator sets public/protected/private properties (including parent private props)
  • DeepCloner performs efficient deep clones while preserving copy-on-write for arrays/strings
Frequently asked questions about Var Exporter
How can I use Symfony VarExporter to optimize Laravel Eloquent model serialization?
Replace `serialize()`/`unserialize()` calls in Eloquent models with `VarExporter::export()` and `file_get_contents()` for PHP code. This reduces memory usage and speeds up hydration by 2–10x, especially for complex relationships. For example, cache model dumps as PHP files instead of binary formats. Test with `php artisan tinker` to verify exported code rehydrates correctly.
Will Symfony VarExporter break existing Laravel session storage using serialized objects?
No, but you’ll need to migrate gradually. Start by replacing `serialize()` in session drivers (e.g., `FileSessionHandler`) with `VarExporter::export()`, then update stored sessions to PHP code. Use Laravel’s `session()->put()` with the exported string. For existing sessions, implement a fallback to `unserialize()` during transition.
Does VarExporter support Laravel’s JSON serialization interfaces (JsonSerializable, Arrayable)?
Yes, but with caveats. VarExporter respects `__serialize()`/`__unserialize()` methods, so classes implementing these will work as-is. For `JsonSerializable`, ensure the `toJson()` output matches the exported structure. Test with `JsonSerializable` models by comparing `VarExporter::export($model)` against `json_encode($model)`.
Can I use VarExporter’s Instantiator to bypass Laravel’s service container for object creation?
Yes, but use it judiciously. The `Instantiator` skips constructors, which may conflict with Laravel’s DI bindings (e.g., `__construct()` dependency injection). Reserve it for performance-critical paths like queue jobs or cached objects. Wrap it in a service provider to avoid polluting the container.
How do I handle circular references in Eloquent relationships when using VarExporter?
VarExporter preserves circular references by default, but for Eloquent, explicitly break cycles during export using `VarExporter::setExportHandles()` or `VarExporter::setExportHandlesCallback()`. For example, exclude `parent` relationships from export to avoid infinite loops. Test with `Model::with('children')->first()` to ensure no recursion errors.
Is Symfony VarExporter compatible with Laravel’s queue system (e.g., Redis, database queues)?
Yes, but replace `serialize()` in queue payloads with `VarExporter::export()`. For Redis queues, update the payload serializer in `app/Providers/AppServiceProvider` to use the exported PHP code. Test job execution with `php artisan queue:work` to confirm objects rehydrate correctly without constructor calls.
What’s the performance impact of using DeepCloner vs. Laravel’s `clone()` for collections?
DeepCloner is significantly faster (3–5x) and more memory-efficient than `unserialize(serialize())` for deep object graphs. For Laravel collections, replace `clone($collection)` with `DeepCloner::clone($collection)`. Benchmark with `php -r 'var_dump(memory_get_usage());'` to compare memory usage during hydration.
How can I integrate VarExporter’s lazy-loading proxies with Laravel Eloquent?
Use `LazyProxyTrait` (PHP 8.4+) or generate proxies manually for older versions. For Eloquent, extend `LazyProxyTrait` in a base model and override `load()` to defer queries. Example: `class LazyModel extends Model { use LazyProxyTrait; }`. Test with `Model::find(1)->relation` to ensure lazy-loaded relationships trigger queries only on access.
Does VarExporter work with Laravel’s cached views or Blade components?
Yes, but cache views as PHP code instead of serialized data. Replace `Cache::put('view', serialize($view))` with `Cache::put('view', VarExporter::export($view))`. For Blade, use `Blade::render()` with exported data. Verify cached views render correctly by clearing the cache (`php artisan cache:clear`).
Are there alternatives to VarExporter for Laravel that don’t require Symfony dependencies?
Limited alternatives exist. `igbinary` improves `serialize()` performance but lacks OPcache benefits. For PHP code export, consider `var_export()` with manual cleanup, but it’s slower and less reliable for complex objects. VarExporter’s Symfony integration ensures compatibility with Laravel’s ecosystem (e.g., OPcache, debug tools) without bloating dependencies.
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
milesj/emojibase
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