{"id":13562740,"url":"https://github.com/symfony/var-exporter","last_synced_at":"2025-05-14T09:03:13.847Z","repository":{"id":40541859,"uuid":"146569764","full_name":"symfony/var-exporter","owner":"symfony","description":"Allows exporting any serializable PHP data structure to plain PHP code","archived":false,"fork":false,"pushed_at":"2025-04-27T19:06:26.000Z","size":644,"stargazers_count":2075,"open_issues_count":0,"forks_count":7,"subscribers_count":5,"default_branch":"7.2","last_synced_at":"2025-04-30T00:07:17.962Z","etag":null,"topics":["clone","component","construct","export","hydrate","instantiate","lazy-loading","php","proxy","serialize","symfony","symfony-component"],"latest_commit_sha":null,"homepage":"https://symfony.com/var-exporter","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/symfony.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"fabpot","tidelift":"packagist/symfony/symfony","custom":"https://symfony.com/sponsor"}},"created_at":"2018-08-29T08:30:30.000Z","updated_at":"2025-04-27T13:35:16.000Z","dependencies_parsed_at":"2022-07-12T18:02:43.548Z","dependency_job_id":"0d84a9a2-7ebd-47a3-82a6-37a19dcb1a35","html_url":"https://github.com/symfony/var-exporter","commit_stats":{"total_commits":185,"total_committers":31,"mean_commits":5.967741935483871,"dds":"0.44324324324324327","last_synced_commit":"1a6a89f95a46af0f142874c9d650a6358d13070d"},"previous_names":[],"tags_count":225,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symfony%2Fvar-exporter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symfony%2Fvar-exporter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symfony%2Fvar-exporter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symfony%2Fvar-exporter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/symfony","download_url":"https://codeload.github.com/symfony/var-exporter/tar.gz/refs/heads/7.2","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252416540,"owners_count":21744449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clone","component","construct","export","hydrate","instantiate","lazy-loading","php","proxy","serialize","symfony","symfony-component"],"created_at":"2024-08-01T13:01:11.813Z","updated_at":"2025-05-06T18:53:03.281Z","avatar_url":"https://github.com/symfony.png","language":"PHP","funding_links":["https://github.com/sponsors/fabpot","https://tidelift.com/funding/github/packagist/symfony/symfony","https://symfony.com/sponsor"],"categories":["PHP"],"sub_categories":[],"readme":"VarExporter Component\n=====================\n\nThe VarExporter component provides various tools to deal with the internal state\nof objects:\n\n- `VarExporter::export()` allows exporting any serializable PHP data structure to\n  plain PHP code. While doing so, it preserves all the semantics associated with\n  the serialization mechanism of PHP (`__wakeup`, `__sleep`, `Serializable`,\n  `__serialize`, `__unserialize`);\n- `Instantiator::instantiate()` creates an object and sets its properties without\n  calling its constructor nor any other methods;\n- `Hydrator::hydrate()` can set the properties of an existing object;\n- `Lazy*Trait` can make a class behave as a lazy-loading ghost or virtual proxy.\n\nVarExporter::export()\n---------------------\n\nThe reason to use `VarExporter::export()` *vs* `serialize()` or\n[igbinary](https://github.com/igbinary/igbinary) is performance: thanks to\nOPcache, the resulting code is significantly faster and more memory efficient\nthan using `unserialize()` or `igbinary_unserialize()`.\n\nUnlike `var_export()`, this works on any serializable PHP value.\n\nIt also provides a few improvements over `var_export()`/`serialize()`:\n\n * the output is PSR-2 compatible;\n * the output can be re-indented without messing up with `\\r` or `\\n` in the data;\n * missing classes throw a `ClassNotFoundException` instead of being unserialized\n   to `PHP_Incomplete_Class` objects;\n * references involving `SplObjectStorage`, `ArrayObject` or `ArrayIterator`\n   instances are preserved;\n * `Reflection*`, `IteratorIterator` and `RecursiveIteratorIterator` classes\n   throw an exception when being serialized (their unserialized version is broken\n   anyway, see https://bugs.php.net/76737).\n\nInstantiator and Hydrator\n-------------------------\n\n`Instantiator::instantiate($class)` creates an object of the given class without\ncalling its constructor nor any other methods.\n\n`Hydrator::hydrate()` sets the properties of an existing object, including\nprivate and protected ones. For example:\n\n```php\n// Sets the public or protected $object-\u003epropertyName property\nHydrator::hydrate($object, ['propertyName' =\u003e $propertyValue]);\n\n// Sets a private property defined on its parent Bar class:\nHydrator::hydrate($object, [\"\\0Bar\\0privateBarProperty\" =\u003e $propertyValue]);\n\n// Alternative way to set the private $object-\u003eprivateBarProperty property\nHydrator::hydrate($object, [], [\n    Bar::class =\u003e ['privateBarProperty' =\u003e $propertyValue],\n]);\n```\n\n`Lazy*Trait`\n------------\n\nThe component provides two lazy-loading patterns: ghost objects and virtual\nproxies (see https://martinfowler.com/eaaCatalog/lazyLoad.html for reference).\n\nGhost objects work only with concrete and non-internal classes. In the generic\ncase, they are not compatible with using factories in their initializer.\n\nVirtual proxies work with concrete, abstract or internal classes. They provide an\nAPI that looks like the actual objects and forward calls to them. They can cause\nidentity problems because proxies might not be seen as equivalents to the actual\nobjects they proxy.\n\nBecause of this identity problem, ghost objects should be preferred when\npossible. Exceptions thrown by the `ProxyHelper` class can help decide when it\ncan be used or not.\n\nGhost objects and virtual proxies both provide implementations for the\n`LazyObjectInterface` which allows resetting them to their initial state or to\nforcibly initialize them when needed. Note that resetting a ghost object skips\nits read-only properties. You should use a virtual proxy to reset read-only\nproperties.\n\n### `LazyGhostTrait`\n\nBy using `LazyGhostTrait` either directly in your classes or by using\n`ProxyHelper::generateLazyGhost()`, you can make their instances lazy-loadable.\nThis works by creating these instances empty and by computing their state only\nwhen accessing a property.\n\n```php\nclass FooLazyGhost extends Foo\n{\n    use LazyGhostTrait;\n}\n\n$foo = FooLazyGhost::createLazyGhost(initializer: function (Foo $instance): void {\n    // [...] Use whatever heavy logic you need here\n    // to compute the $dependencies of the $instance\n    $instance-\u003e__construct(...$dependencies);\n    // [...] Call setters, etc. if needed\n});\n\n// $foo is now a lazy-loading ghost object. The initializer will\n// be called only when and if a *property* is accessed.\n```\n\n### `LazyProxyTrait`\n\nAlternatively, `LazyProxyTrait` can be used to create virtual proxies:\n\n```php\n$proxyCode = ProxyHelper::generateLazyProxy(new ReflectionClass(Foo::class));\n// $proxyCode contains the reference to LazyProxyTrait\n// and should be dumped into a file in production envs\neval('class FooLazyProxy'.$proxyCode);\n\n$foo = FooLazyProxy::createLazyProxy(initializer: function (): Foo {\n    // [...] Use whatever heavy logic you need here\n    // to compute the $dependencies of the $instance\n    $instance = new Foo(...$dependencies);\n    // [...] Call setters, etc. if needed\n\n    return $instance;\n});\n// $foo is now a lazy-loading virtual proxy object. The initializer will\n// be called only when and if a *method* is called.\n```\n\nResources\n---------\n\n * [Documentation](https://symfony.com/doc/current/components/var_exporter.html)\n * [Contributing](https://symfony.com/doc/current/contributing/index.html)\n * [Report issues](https://github.com/symfony/symfony/issues) and\n   [send Pull Requests](https://github.com/symfony/symfony/pulls)\n   in the [main Symfony repository](https://github.com/symfony/symfony)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsymfony%2Fvar-exporter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsymfony%2Fvar-exporter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsymfony%2Fvar-exporter/lists"}