VarExporter is a Symfony component for exporting, instantiating, hydrating, cloning, and lazy-loading PHP objects. Install it with Composer in PHP projects.
Overview
VarExporter provides utilities that work with the internal state of PHP objects. VarExporter::export turns serializable data structures into plain PHP code while preserving PHP serialization semantics such as __wakeup, __sleep, and related interfaces.
DeepCloner deep-clones values while keeping copy-on-write behavior for strings and arrays, which can be faster and more memory efficient than unserialize(serialize()). For lazy loading, ProxyHelper::generateLazyProxy builds decorator proxies for abstract or internal classes and interfaces when native lazy objects are not sufficient.
The component can use the ext-deepclone extension for performance, or a polyfill fallback. The extension also exposes deepclonehydrate for hydrating objects without calling constructors, including private, protected, and readonly properties.
Compared with serialize or igbinary, export output runs faster under OPcache than unserialization. Output is PSR-2 compatible, re-indentable without breaking embedded newlines, and missing classes raise ClassNotFoundException instead of incomplete class objects. The package is widely pulled in by Symfony-based applications and other PHP ecosystems via Composer.
Key features
- Export serializable data to PHP code
- Deep clone with copy-on-write preservation
- Lazy proxy generation for interfaces
- Native lazy object helpers for PHP 8.4+
- deepclone extension or polyfill support
- Hydrate without calling constructors
- PSR-2 compatible export output
- Stricter behavior on missing classes
Use cases
- Caching warmed PHP data structures
- Efficient repeated deep copies
- Lazy-loaded domain object graphs
- Symfony framework component usage
- Faster reload than serialize in OPcache