Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report

symfony/var-exporter v7.0.2

Allows exporting any serializable PHP data structure to plain PHP code
Package summary
Share
0
issues
0
licenses
Package created
29 Aug 2018
Version published
27 Dec 2023
Maintainers
1
Total deps
0
Direct deps
0
License
MIT
Generating a report...
Hold on while we generate a fresh audit report for this package.

Frequently Asked Questions

What does symfony/var-exporter do?

The Symfony/Var-Exporter is a PHP package that aids in exporting any serializable PHP data structure into plain PHP code, preserving all the semantics associated with the serialization mechanism of PHP. It offers performance advantages over the typical serialization methods such as serialize() or igbinary, thanks to OPcache, and works seamlessly with any serializable PHP value. Beyond serialization, it also includes classes for creating objects without calling constructors or other methods (Instantiator::instantiate()), and setting the properties of an existing object (Hydrator::hydrate()). Moreover, it provides lazy-loading characteristics via Lazy*Trait classes.

How do you use symfony/var-exporter?

To utilize the Symfony/Var-Exporter, you first need to install it via composer. Once installed, you can directly use its methods in your code. To perform data serialization, you can use the VarExporter::export() method. Here is a basic usage example:

use Symfony\Component\VarExporter\VarExporter;

$data = [
    'a' => 1, 
    'b' => 2
];   // Sample data to be serialized

$serialized = VarExporter::export($data);  // Serializing the data

To instantiate an object without calling its constructor, you can use Instantiator::instantiate(). For updating an object's properties, Hydrator::hydrate() comes into play. Here's a simple example:

use Symfony\Component\VarExporter\Instantiator;

$instance = Instantiator::instantiate(SomeClass::class); // Creating instance of SomeClass without using constructor

use Symfony\Component\VarExporter\Hydrator;

Hydrator::hydrate($instance, ['property' => 'value']); // Set the properties of the instance

For leveraging lazy-loading, Lazy*Trait can be utilized. Here's an instance of creating a lazy-loading object:

use Symfony\Component\VarExporter\Cloner\LazyGhostTrait;

class MyClass
{
    use LazyGhostTrait;

    // class definition
}

MyClass::createLazyGhost(function (MyClass $instance): void {  // named initializer
    // initialization logic here
});

Where are the symfony/var-exporter docs?

The complete and detailed documentation for Symfony/Var-Exporter is available on the official Symfony website at https://symfony.com/doc/current/components/var_exporter.html. This documentation provides deep insights into how to correctly use the package, explanations about its core concepts, and extensive code examples for different use-cases.