var exporter
v7.2.0
The VarExporter component provides various tools to deal with the internal state of objects:
VarExporter::export()
allows exporting any serializable PHP data structure to
plain PHP code. While doing so, it preserves all the semantics associated with
the serialization mechanism of PHP (__wakeup
, __sleep
, Serializable
,
__serialize
, __unserialize
);Instantiator::instantiate()
creates an object and sets its properties without
calling its constructor nor any other methods;Hydrator::hydrate()
can set the properties of an existing object;Lazy*Trait
can make a class behave as a lazy-loading ghost or virtual proxy.The reason to use VarExporter::export()
vs serialize()
or
igbinary is performance: thanks to
OPcache, the resulting code is significantly faster and more memory efficient
than using unserialize()
or igbinary_unserialize()
.
Unlike var_export()
, this works on any serializable PHP value.
It also provides a few improvements over var_export()
/serialize()
:
r
or n
in the data;ClassNotFoundException
instead of being unserialized
to PHP_Incomplete_Class
objects;SplObjectStorage
, ArrayObject
or ArrayIterator
instances are preserved;Reflection*
, IteratorIterator
and RecursiveIteratorIterator
classes
throw an exception when being serialized (their unserialized version is broken
anyway, see https://bugs.php.net/76737).Instantiator::instantiate($class)
creates an object of the given class without
calling its constructor nor any other methods.
Hydrator::hydrate()
sets the properties of an existing object, including
private and protected ones. For example: