PHPackages                             henzeb/var-export-wrapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. henzeb/var-export-wrapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

henzeb/var-export-wrapper
=========================

wrapping objects and closures for var\_export

v1.4.0(10mo ago)115.6k↓28.5%21AGPL-3.0-onlyPHPPHP &gt;=8.0CI passing

Since Jul 31Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/henzeb/var-export-wrapper)[ Packagist](https://packagist.org/packages/henzeb/var-export-wrapper)[ Docs](https://github.com/henzeb/laravel-console)[ RSS](/packages/henzeb-var-export-wrapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (1)

var\_export wrapper
===================

[](#var_export-wrapper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d3d1128d14aedc63c8408e7b0c96642c1617ea652816dafd1bde4f51902c17d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656e7a65622f7661722d6578706f72742d777261707065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henzeb/var-export-wrapper)[![Total Downloads](https://camo.githubusercontent.com/d603fd7e9c5996ac1787f2dc285fef3780211da9113f1408198678d02ec80090/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656e7a65622f7661722d6578706f72742d777261707065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/henzeb/var-export-wrapper)

var\_export is a good choice when caching config. This is how it is done in Laravel for example. But not every object is automatically exportable.

Imagine you want to make an `ImageManipulationService` that resizes your images. You want it to be configurable, so you get something like this:

```
return [
    [
        'suffix' => 'thumb',
        'width' => 300,
        'height' => 200,
        'ratio' => true,
        'pixelate' => 3,
    ]
    // ...
]
```

You would need an option for each possible feature, or implement the new wanted feature whenever requested. You could work with invokable classes, but that won't make it readable.

What if you could do this:

```
return [
     'constraints' => [
        'aspectRatio' => fn(Constraint $constraint) => $constraint->aspectRatio()
    ],
    'images' => [
        'suffix' => 'thumb',
        'manipulate' => function (Image $image) {
                $image->resize(
                    300,
                    20,
                    config('constraints.aspectRatio')
                )->pixelate(3);
            }
        }
    ]
    // ...
]
```

With this package, you can export closures and objects that do not implement `__set_state`. You do not need laravel to use it, but when you do, it automatically parses your configuration for `artisan config:cache`

Installation
------------

[](#installation)

Just install with the following command.

```
composer require henzeb/var-export-wrapper
```

Usage
-----

[](#usage)

### exportify

[](#exportify)

`exportify` is what wraps the object or an array of objects

```
use function Henzeb\VarExportWrapper\Support\Functions\exportify;

exportify(fn()=>true); // returns instance of VarExportable
exportify(new ExportableClass()); // returns instance of ExportableClass
exportify(new RegularClass()); // returns instance of VarExportable

exportify(['recursive' => [new RegularClass(), fn()=>true]]); // returns nested array with 2 VarExportable instances
```

Note: `exportify` also iterates through objects implementing `Traversable` or `ArrayAccess`.

#### \_\_get\_state

[](#__get_state)

`exportify` does not automatically wrap properties inside the object, and this is fine in most cases. But sometimes you want to export objects such as closures inside an object, or specify what to export. in order to do that, you can implement `__get_state` on your object. This method should return an array with the properties you want to restore with `__get_state`.

```
class User {
    private $name;
    private $email;

    public function __construct($name, $email) {
        $this->name = $name;
        $this->email = $email;
    }

    public function __get_state(): array {
        return [
            'name' => $this->name,
            'email' => $this->email,
        ];
    }

    public static function __set_state($state): self {
        return new self($state['name'], $state['email']);
    }
}
```

Note: you do not need to use exportify here yourself, it is done automatically.

is\_exportable
--------------

[](#is_exportable)

Validates the given object or array. If not exportable, it returns false.

```
use function Henzeb\VarExportWrapper\Support\Functions\is_exportable;

is_exportable(fn()=>true); // returns false
is_exportable(new RegularClass()); // returns false
is_exportable(new ExportableClass()); // returns true
is_exportable(STDIN); // returns true
is_exportable([[fn()=>true]]); // returns false
is_exportable([[new ExportableClass()]]); // returns true
is_exportable([[new ExportableClass(), fn()=>true]]); // returns false
```

### var\_export

[](#var_export)

`var_export` is the supercharged version of the native function, but under the hood it will automatically wrap everything that is not exportable by default in a `VarExportable` instance, before actually exporting the value.

```
use function Henzeb\VarExportWrapper\Support\Functions\var_export;

var_export(fn()=>true); // dumps the var_export string after wrapping the closure.
var_export(new RegularClass()); // dumps the var_export string after wrapping the object
var_export(new ExportableClass()); // dumps the var_export string without wrapping
var_export([[fn()=>>true]]); // dumps the var_export string after wrapping closure

var_export(fn()=>true, true); // returns the var_export string after wrapping the closure.
var_export(new RegularClass(), true); // returns the var_export string after wrapping the object
var_export(new ExportableClass(), true); // returns the var_export string without wrapping
var_export([[fn()=>true]], true); // returns the var_export string after wrapping closure

```

### var\_export\_file

[](#var_export_file)

`var_export_file` is the same as `var_export`, but it exports to a file instead of returning.

```
use function Henzeb\VarExportWrapper\Support\Functions\var_export_file;

var_export_file('/tmp/config.php',[[fn()=>>true]]); // writes the var_export string to /tmp/config.php after wrapping closure
```

### var\_import

[](#var_import)

`var_import` is useful when you want to import a var\_exported string or file. This function will automatically unwrap the `VarExportable` instances. You can also pass an array that was imported in another way, but still contains `VarExportable` instances.

```
use function Henzeb\VarExportWrapper\Support\Functions\var_import;
use function Henzeb\VarExportWrapper\Support\Functions\var_export;

var_import(var_export(fn()=>true)); // returns the closure
var_import('path/to/var_export.php'); // returns the object which is exported in the specified file
var_import([new \Henzeb\VarExportWrapper\VarExportable(fn()=>'hello')]); // returns the array with closure
```

### Laravel Config

[](#laravel-config)

When installed in a Laravel installation, you can just start using closures and objects inside your configuration. When calling `artisan config:cache`, var\_export wrapper automatically wraps them in a wrapper.

### Closures under the hood

[](#closures-under-the-hood)

To be able to export closures, it has to serialize them. It uses [laravel/serializable](https://github.com/laravel/serializable-closure)to achieve that. This means that if you've set a secret key, the closure is signed, otherwise it's natively serialized and thus unsigned.

You do not need to wrap closures before passing them to `exportify`

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Henze Berkheij](https://github.com/henzeb)

License
-------

[](#license)

The GNU AGPLv. Please see [License File](LICENSE.md) for more information. \]()

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance64

Regular maintenance activity

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~140 days

Recently: every ~175 days

Total

6

Last Release

319d ago

PHP version history (3 changes)v1.0.0PHP ^7.4|^8.0

v1.2.0PHP ^8.0

v1.3.0PHP &gt;=8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15928532?v=4)[henzeb](/maintainers/henzeb)[@henzeb](https://github.com/henzeb)

---

Top Contributors

[![henzeb](https://avatars.githubusercontent.com/u/15928532?v=4)](https://github.com/henzeb "henzeb (9 commits)")[![mgcodeur](https://avatars.githubusercontent.com/u/91603608?v=4)](https://github.com/mgcodeur "mgcodeur (5 commits)")[![chrispage1](https://avatars.githubusercontent.com/u/2487374?v=4)](https://github.com/chrispage1 "chrispage1 (1 commits)")

---

Tags

exportvar\_exportwrapperclosurehenzebvar

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/henzeb-var-export-wrapper/health.svg)

```
[![Health](https://phpackages.com/badges/henzeb-var-export-wrapper/health.svg)](https://phpackages.com/packages/henzeb-var-export-wrapper)
```

###  Alternatives

[symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

2.1k378.1M441](/packages/symfony-var-exporter)[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k27.6M92](/packages/simplesoftwareio-simple-qrcode)[artisaninweb/laravel-soap

A SoapClient wrapper integration for Laravel

6314.5M12](/packages/artisaninweb-laravel-soap)[brick/varexporter

A powerful alternative to var\_export(), which can export closures and objects without \_\_set\_state()

19429.7M72](/packages/brick-varexporter)[riimu/kit-phpencoder

Highly customizable alternative to var\_export for PHP code generation

717.8M32](/packages/riimu-kit-phpencoder)[mhor/php-mediainfo

PHP wrapper around the mediainfo command

120574.8k7](/packages/mhor-php-mediainfo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
