Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7
Allows exporting any serializable PHP data structure to plain PHP code
License
symfony/var-exporter
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The VarExporter component provides various tools to deal with the internal stateof objects:
VarExporter::export()
allows exporting any serializable PHP data structure toplain PHP code. While doing so, it preserves all the semantics associated withthe serialization mechanism of PHP (__wakeup
,__sleep
,Serializable
,__serialize
,__unserialize
);Instantiator::instantiate()
creates an object and sets its properties withoutcalling 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 useVarExporter::export()
vsserialize()
origbinary is performance: thanks toOPcache, the resulting code is significantly faster and more memory efficientthan usingunserialize()
origbinary_unserialize()
.
Unlikevar_export()
, this works on any serializable PHP value.
It also provides a few improvements overvar_export()
/serialize()
:
- the output is PSR-2 compatible;
- the output can be re-indented without messing up with
\r
or\n
in the data; - missing classes throw a
ClassNotFoundException
instead of being unserializedtoPHP_Incomplete_Class
objects; - references involving
SplObjectStorage
,ArrayObject
orArrayIterator
instances are preserved; Reflection*
,IteratorIterator
andRecursiveIteratorIterator
classesthrow an exception when being serialized (their unserialized version is brokenanyway, seehttps://bugs.php.net/76737).
Instantiator::instantiate($class)
creates an object of the given class withoutcalling its constructor nor any other methods.
Hydrator::hydrate()
sets the properties of an existing object, includingprivate and protected ones. For example:
// Sets the public or protected $object->propertyName propertyHydrator::hydrate($object, ['propertyName' =>$propertyValue]);// Sets a private property defined on its parent Bar class:Hydrator::hydrate($object, ["\0Bar\0privateBarProperty" =>$propertyValue]);// Alternative way to set the private $object->privateBarProperty propertyHydrator::hydrate($object, [], [ Bar::class => ['privateBarProperty' =>$propertyValue],]);
Since version 8.4, PHP provides support for lazy objects via the reflection API.This native API works with concrete classes. It doesn't with abstracts nor withinternal ones.
This components provides helpers to generate lazy objects using the decoratorpattern, which works with abstract or internal classes and with interfaces:
$proxyCode = ProxyHelper::generateLazyProxy(newReflectionClass(AbstractFoo::class));// $proxyCode should be dumped into a file in production envseval('class FooLazyProxy'.$proxyCode);$foo = FooLazyProxy::createLazyProxy(initializer:function ():AbstractFoo {// [...] Use whatever heavy logic you need here// to compute the $dependencies of the $instance$instance =newFoo(...$dependencies);// [...] Call setters, etc. if neededreturn$instance;});// $foo is now a lazy-loading decorator object. The initializer will// be called only when and if a *method* is called.
In addition, this component provides traits and methods to aid in implementingthe ghost and proxy strategies in previous versions of PHP. Those are deprecatedwhen using PHP 8.4.
About
Allows exporting any serializable PHP data structure to plain PHP code
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.