sebastian/exporter

Provides the functionality to export PHP variables for visualization

Maintainers

Package info

github.com/sebastianbergmann/exporter

Homepage

pkg:composer/sebastian/exporter

Transparency log

Statistics

Installs: 942 377 890

Dependents: 72

Suggesters: 0

Stars: 6 820

Open Issues: 1

8.1.1 2026-07-13 11:35 UTC

README

Latest Stable Version CI Status codecov

sebastian/exporter

This component provides the functionality to export PHP variables for visualization.

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require sebastian/exporter

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev sebastian/exporter

Usage

Exporting:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter;

/*
Exception Object #4 (
    'message' => '',
    'string' => '',
    'code' => 0,
    'file' => '/home/sebastianbergmann/test.php',
    'line' => 34,
    'previous' => null,
)
*/

print $exporter->export(new Exception);

Data Types

Exporting simple types:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter;

// 46
print $exporter->export(46);

// 4.0
print $exporter->export(4.0);

// 'hello, world!'
print $exporter->export('hello, world!');

// false
print $exporter->export(false);

// NAN
print $exporter->export(acos(8));

// -INF
print $exporter->export(log(0));

// null
print $exporter->export(null);

// resource(13) of type (stream)
print $exporter->export(fopen('php://stderr', 'w'));

// Binary String: 0x000102030405
print $exporter->export(chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5));

Exporting complex types:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter;

/*
Array &0 [
    0 => Array &1 [
        0 => 1,
        1 => 2,
        2 => 3,
    ],
    1 => Array &2 [
        0 => '',
        1 => 0,
        2 => false,
    ],
]
*/

print $exporter->export([[1, 2, 3], ['', 0, false]]);

/*
Array &0 [
    'self' => Array &1 [
        'self' => Array &1,
    ],
]
*/

$array         = [];
$array['self'] = &$array;

print $exporter->export($array);

/*
stdClass Object #4 (
    'self' => stdClass Object #4,
)
*/

$obj       = new stdClass;
$obj->self = $obj;

print $exporter->export($obj);

Compact exports:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter;

// []
print $exporter->shortenedExport([]);

// [...]
print $exporter->shortenedExport([1, 2, 3, 4, 5]);

// stdClass Object ()
print $exporter->shortenedExport(new stdClass);

// Exception Object (...)
print $exporter->shortenedExport(new Exception);

// 'this\nis\na\nsuper\nlong\nstr...nspace'
print $exporter->shortenedExport(
<<<LONG_STRING
this
is
a
super
long
string
that
wraps
a
lot
and
eats
up
a
lot
of
space
LONG_STRING
);

Customizing How Objects Are Exported

By default, an object is exported property by property. Implement the ObjectExporter interface to control how objects of a type are represented:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\ExportContext;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporter;

final class MoneyExporter implements ObjectExporter
{
    public function handles(object $object): bool
    {
        return $object instanceof Money;
    }

    public function export(object $object, Exporter $exporter, int $indentation, ExportContext $context): string
    {
        assert($object instanceof Money);

        return sprintf('Money (%d %s)', $object->amount(), $object->currency());
    }
}

An object exporter is registered by passing it to Exporter's constructor. It is consulted before the default representation of an object is used:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter(objectExporter: new MoneyExporter);

// Money (1999 EUR)
print $exporter->export(new Money(1999, 'EUR'));

/*
stdClass Object #6 (
    'net' => Money (1999 EUR),
    'gross' => Money (2379 EUR),
)
*/

$price        = new stdClass;
$price->net   = new Money(1999, 'EUR');
$price->gross = new Money(2379, 'EUR');

print $exporter->export($price);

ObjectExporterChain composes multiple object exporters into a single one. They are consulted in the order in which they are composed into the chain and the first one whose handles() method returns true is asked for the representation:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporterChain;

$exporter = new Exporter(
    objectExporter: new ObjectExporterChain(
        [
            new BasketExporter,
            new MoneyExporter,
        ],
    ),
);

Enums are objects and are therefore passed to the object exporters as well. When no object exporter handles an object, the default representation is used for it.

Repeated Occurrences

When the same object occurs more than once, the default representation is only used for the first occurrence; subsequent occurrences are replaced with a reference to the object:

/*
Array &0 [
    0 => Money Object #8 (
        'amount' => 1999,
        'currency' => 'EUR',
    ),
    1 => Money Object #8,
]
*/

$money = new Money(1999, 'EUR');

print (new Exporter)->export([$money, $money]);

An object exporter is responsible for the entire representation of the object it handles. Every occurrence of such an object is therefore exported by it:

/*
Array &0 [
    0 => Money (1999 EUR),
    1 => Money (1999 EUR),
]
*/

print $exporter->export([$money, $money]);

The exception is an object that is (indirectly) nested in itself: it cannot be exported this way without recursing infinitely and is replaced with a reference to the object.

Exporting Nested Values

An object exporter may use the Exporter it is given to export values that are nested in the object it handles. The ExportContext it is given must be passed on when it does. Otherwise, the export of these nested values starts over with an empty context and, for instance, assigns references to arrays that are already in use elsewhere in the same export:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\ExportContext;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\Exporter\ObjectExporter;

final class BasketExporter implements ObjectExporter
{
    public function handles(object $object): bool
    {
        return $object instanceof Basket;
    }

    public function export(object $object, Exporter $exporter, int $indentation, ExportContext $context): string
    {
        assert($object instanceof Basket);

        return 'Basket ' . $exporter->export($object->items(), $indentation, $context);
    }
}
/*
Array &0 [
    'basket' => Basket Array &1 [
        0 => Money (1999 EUR),
    ],
]
*/

print $exporter->export(['basket' => new Basket([new Money(1999, 'EUR')])]);

Compact Exports

Exporter::shortenedExport() and Exporter::shortenedRecursiveExport() consult object exporters as well. The representation an object exporter provides is collapsed to a single line and shortened when it is longer than the configured maximum length:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter(objectExporter: new MoneyExporter);

// Money (1999 EUR)
print $exporter->shortenedExport(new Money(1999, 'EUR'));

Bear in mind that these representations are used where a short, single-line, and stable string is required. The representation of a data set is built using Exporter::shortenedRecursiveExport(), for instance, and is part of the name of a test that uses a data provider. An object exporter should therefore provide a representation that is compact and that does not change from one export to the next.

Custom Representations

Exporter::hasCustomRepresentationFor() tells whether an object exporter provides the representation for an object. This is meant for code that renders objects itself and wants to use the representation an object exporter provides when there is one:

<?php declare(strict_types=1);
use SebastianBergmann\Exporter\Exporter;

$exporter = new Exporter(objectExporter: new MoneyExporter);

// true
var_dump($exporter->hasCustomRepresentationFor(new Money(1999, 'EUR')));

// false
var_dump($exporter->hasCustomRepresentationFor(new stdClass));

Bear in mind that an object that is (indirectly) nested in itself is replaced with a reference to the object instead of being exported by an object exporter again.