|
| 1 | +.. index:: |
| 2 | + single: VarExporter |
| 3 | + single: Components; VarExporter |
| 4 | + |
| 5 | +The VarExporter Component |
| 6 | +========================= |
| 7 | + |
| 8 | + The VarExporter component exports any serializable PHP data structure to |
| 9 | + plain PHP code and allows to instantiate and populate objects without |
| 10 | + calling their constructors. |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +.. code-block:: terminal |
| 16 | +
|
| 17 | + $ composer require --dev symfony/var-exporter |
| 18 | +
|
| 19 | +Alternatively, you can clone the `<https://github.com/symfony/var-exporter>`_ repository. |
| 20 | + |
| 21 | +.. include:: /components/require_autoload.rst.inc |
| 22 | + |
| 23 | +Exporting/Serializing Variables |
| 24 | +------------------------------- |
| 25 | + |
| 26 | +The main feature of this component is to serialize PHP data structures to plain |
| 27 | +PHP code, similar to PHP's :phpfunction:`var_export` function:: |
| 28 | + |
| 29 | + use Symfony\Component\VarExporter\VarExporter; |
| 30 | + |
| 31 | + $exported = VarExporter::export($someVariable); |
| 32 | + // store the $exported data in some file or cache system for later reuse |
| 33 | + $data = file_put_contents('exported.php', $exported); |
| 34 | + |
| 35 | + // later, regenerate the original variable when you need it |
| 36 | + $regeneratedVariable = require 'exported.php'; |
| 37 | + |
| 38 | +The reason to use this component instead of ``serialize()`` or ``igbinary`` is |
| 39 | +performance: thanks to `OPcache`_, the resulting code is significantly faster |
| 40 | +and more memory efficient than using ``unserialize()`` or ``igbinary_unserialize()``. |
| 41 | + |
| 42 | +In addition, there are some minor differences: |
| 43 | + |
| 44 | +* If the original variable defines them, all the semantics associated with |
| 45 | + ``serialize()`` (such as ``__wakeup()``, ``__sleep()``, and ``Serializable``) |
| 46 | + are preserved (``var_export()`` ignores them); |
| 47 | +* References involving ``SplObjectStorage``, ``ArrayObject`` or ``ArrayIterator`` |
| 48 | + instances are preserved; |
| 49 | +* Missing classes throw a ``ClassNotFoundException`` instead of being |
| 50 | + unserialized to ``PHP_Incomplete_Class`` objects; |
| 51 | +* ``Reflection*``, ``IteratorIterator`` and ``RecursiveIteratorIterator`` |
| 52 | + classes throw an exception when being serialized. |
| 53 | + |
| 54 | +The exported data is a `PSR-2`_ compatible PHP file. Consider for example the |
| 55 | +following class hierarchy:: |
| 56 | + |
| 57 | + abstract class AbstractClass |
| 58 | + { |
| 59 | + protected $foo; |
| 60 | + private $bar; |
| 61 | + |
| 62 | + protected function setBar($bar) |
| 63 | + { |
| 64 | + $this->bar = $bar; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + class ConcreteClass extends AbstractClass |
| 69 | + { |
| 70 | + public function __construct() |
| 71 | + { |
| 72 | + $this->foo = 123; |
| 73 | + $this->setBar(234); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +When exporting the ``ConcreteClass`` data with VarExporter, the generated PHP |
| 78 | +file looks like this:: |
| 79 | + |
| 80 | + <?php |
| 81 | + return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate( |
| 82 | + $o = [ |
| 83 | + clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\ConcreteClass'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\ConcreteClass')), |
| 84 | + ], |
| 85 | + null, |
| 86 | + [ |
| 87 | + 'Symfony\\Component\\VarExporter\\Tests\\AbstractClass' => [ |
| 88 | + 'foo' => [ |
| 89 | + 123, |
| 90 | + ], |
| 91 | + 'bar' => [ |
| 92 | + 234, |
| 93 | + ], |
| 94 | + ], |
| 95 | + ], |
| 96 | + $o[0], |
| 97 | + [] |
| 98 | + ); |
| 99 | + |
| 100 | +Instantiating PHP Classes |
| 101 | +------------------------- |
| 102 | + |
| 103 | +The other main feature provided by this component is an instantiator which can |
| 104 | +create objects and set their properties without calling their constructors or |
| 105 | +any other methods:: |
| 106 | + |
| 107 | + use Symfony\Component\VarExporter\Instantiator; |
| 108 | + |
| 109 | + // creates an empty instance of Foo |
| 110 | + $fooObject = Instantiator::instantiate(Foo::class); |
| 111 | + |
| 112 | + // creates a Foo instance and sets one of its properties |
| 113 | + $fooObject = Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]); |
| 114 | + |
| 115 | + // creates a Foo instance and sets a private property defined on its parent Bar class |
| 116 | + $fooObject = Instantiator::instantiate(Foo::class, [], [ |
| 117 | + Bar::class => ['privateBarProperty' => $propertyValue], |
| 118 | + ]); |
| 119 | + |
| 120 | +Instances of ``ArrayObject``, ``ArrayIterator`` and ``SplObjectHash`` can be |
| 121 | +created by using the special ``"\0"`` property name to define their internal value:: |
| 122 | + |
| 123 | + // Creates an SplObjectHash where $info1 is associated to $object1, etc. |
| 124 | + $theObject = Instantiator::instantiate(SplObjectStorage::class, [ |
| 125 | + "\0" => [$object1, $info1, $object2, $info2...] |
| 126 | + ]); |
| 127 | + |
| 128 | + // creates an ArrayObject populated with $inputArray |
| 129 | + $theObject = Instantiator::instantiate(ArrayObject::class, [ |
| 130 | + "\0" => [$inputArray] |
| 131 | + ]); |
| 132 | + |
| 133 | +.. _`OPCache`: https://php.net/opcache |
| 134 | +.. _`PSR-2`: https://www.php-fig.org/psr/psr-2/ |
0 commit comments