PHPackages                             bermudaphp/var-export - 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. bermudaphp/var-export

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

bermudaphp/var-export
=====================

v2.0.1(11mo ago)31392MITPHPPHP ^8.1

Since Jan 11Pushed 11mo ago1 watchersCompare

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

READMEChangelog (7)Dependencies (1)Versions (8)Used By (2)

VarExporter
===========

[](#varexporter)

**[Русская версия](README_ru.md)**

PHP library for exporting variables to their string representation with advanced formatting options and closure support.

Features
--------

[](#features)

- **Multiple data types support**: arrays, closures, scalar values, and more
- **Two formatting modes**: standard (compact) and pretty (indented)
- **Closure export**: Full closure source code extraction with namespace resolution
- **Configurable formatting**: Custom indentation, key sorting, trailing commas
- **Special value handling**: INF, NAN, magic constants
- **PHP-Parser integration**: Advanced AST analysis for closures
- **Exception handling**: Detailed error information for debugging

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

[](#installation)

```
composer require bermudaphp/var-export
```

Quick Start
-----------

[](#quick-start)

```
use Bermuda\VarExport\VarExporter;

// Simple array export
$array = ['foo' => 'bar', 'nested' => [1, 2, 3]];
echo VarExporter::export($array);
// Output: ['foo' => 'bar', 'nested' => [1, 2, 3]]

// Pretty formatting
echo VarExporter::exportPretty($array);
// Output:
// [
//     'foo' => 'bar',
//     'nested' => [
//         1,
//         2,
//         3
//     ]
// ]
```

Usage Examples
--------------

[](#usage-examples)

### Basic Variable Export

[](#basic-variable-export)

```
use Bermuda\VarExport\VarExporter;

// Scalar values
echo VarExporter::export(42);           // 42
echo VarExporter::export('hello');      // 'hello'
echo VarExporter::export(true);         // true
echo VarExporter::export(null);         // null

// Special float values
echo VarExporter::export(INF);          // INF
echo VarExporter::export(-INF);         // -INF
echo VarExporter::export(NAN);          // NAN
```

### Array Export

[](#array-export)

```
use Bermuda\VarExport\VarExporter;

$data = [
    'users' => [
        ['name' => 'John', 'age' => 30],
        ['name' => 'Jane', 'age' => 25]
    ],
    'config' => [
        'debug' => true,
        'timeout' => 30
    ]
];

// Standard formatting (single line)
echo VarExporter::export($data);

// Pretty formatting (multi-line with indentation)
echo VarExporter::exportPretty($data);
```

### Closure Export

[](#closure-export)

```
use Bermuda\VarExport\VarExporter;

$multiplier = 2;
$closure = function($x) use ($multiplier) {
    return $x * $multiplier;
};

// Export closure with full source code
echo VarExporter::export($closure);
// Output: function($x) use ($multiplier) { return $x * $multiplier; }

class A {
    public function call()
    {
        $closure = fn(): string => self::class ;
        dd(VarExporter::export($closure));
    }
}

^ "fn(): string => \A::class"
```

### Configuration Options

[](#configuration-options)

```
use Bermuda\VarExport\{VarExporter, FormatterConfig, FormatterMode};

$config = new FormatterConfig(
    mode: FormatterMode::PRETTY,
    indent: '  ',                    // 2 spaces instead of 4
    maxDepth: 50,                   // Maximum nesting depth
    sortKeys: true,                 // Sort array keys
    trailingComma: true            // Add trailing comma in arrays
);

$array = ['c' => 3, 'a' => 1, 'b' => 2];
echo VarExporter::export($array, $config);
```

### Using Convenience Functions

[](#using-convenience-functions)

```
use function Bermuda\VarExport\{export_var, export_array, export_closure};

// Export any variable with pretty formatting by default
echo export_var(['foo' => 'bar']);

// Export array specifically
echo export_array([1, 2, 3]);

// Export closure specifically
$fn = fn($x) => $x * 2;
echo export_closure($fn);
```

Advanced Configuration
----------------------

[](#advanced-configuration)

### Custom Formatting

[](#custom-formatting)

```
use Bermuda\VarExport\{VarExporter, FormatterConfig, FormatterMode};

// Create custom configuration
$config = new FormatterConfig(
    mode: FormatterMode::PRETTY,
    indent: "\t",                   // Use tabs
    sortKeys: true,                 // Sort keys alphabetically
    trailingComma: true            // Add trailing commas
);

// Apply to specific export
$result = VarExporter::export($data, $config);

// Set as default for all exports
VarExporter::setDefaultConfig($config);
```

### Custom Exporters

[](#custom-exporters)

```
use Bermuda\VarExport\{VarExporter, ArrayExporter, ClosureExporter};

// Register custom array exporter
$customArrayExporter = new ArrayExporter($customConfig);
VarExporter::setExporter($customArrayExporter);

// Register custom closure exporter
$customClosureExporter = new ClosureExporter($customConfig);
VarExporter::setExporter($customClosureExporter);
```

Configuration Options
---------------------

[](#configuration-options-1)

OptionTypeDefaultDescription`mode``FormatterMode``STANDARD`Formatting mode (STANDARD or PRETTY)`indent``string``'    '`Indentation string (4 spaces by default)`maxDepth``int``100`Maximum nesting depth to prevent infinite recursion`sortKeys``bool``false`Whether to sort array keys`trailingComma``bool``false`Whether to add trailing commas in arraysSupported Types
---------------

[](#supported-types)

### ✅ Supported

[](#-supported)

- **Arrays**: Indexed and associative arrays with full nesting support
- **Closures**: Anonymous functions and arrow functions with source code extraction
- **Scalars**: integers, floats, strings, booleans, null
- **Special values**: INF, -INF, NAN

### ❌ Not Supported

[](#-not-supported)

- **Objects**: Regular objects (except closures)
- **Resources**: File handles, database connections, etc.

Error Handling
--------------

[](#error-handling)

The library provides detailed exception information:

```
use Bermuda\VarExport\{VarExporter, ExportException};

try {
    $object = new stdClass();
    VarExporter::export($object);
} catch (ExportException $e) {
    echo "Export failed: " . $e->getMessage();
    // Access the problematic variable
    $problematicVar = $e->var;
}
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- nikic/php-parser (for closure export functionality)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance52

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~147 days

Recently: every ~207 days

Total

7

Last Release

338d ago

Major Versions

v1.2.2 → v2.02025-05-27

### Community

Maintainers

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

---

Top Contributors

[![Shelamkoff](https://avatars.githubusercontent.com/u/20490712?v=4)](https://github.com/Shelamkoff "Shelamkoff (72 commits)")

---

Tags

exporterphp81printervar-export

### Embed Badge

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

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

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[roave/backward-compatibility-check

Tool to compare two revisions of a public API to check for BC breaks

5953.3M56](/packages/roave-backward-compatibility-check)[coenjacobs/mozart

Composes all dependencies as a package inside a WordPress plugin

4723.6M20](/packages/coenjacobs-mozart)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[recca0120/laravel-erd

Laravel ERD automatically generates Entity-Relationship Diagrams from your Laravel models and displays them using Vuerd.

36072.0k](/packages/recca0120-laravel-erd)[ondrejmirtes/better-reflection

Better Reflection - an improved code reflection API

136.2M4](/packages/ondrejmirtes-better-reflection)

PHPackages © 2026

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