sebastian/exporter
Exports PHP variables to readable, deterministic strings for visualization and debugging. Handles scalars, arrays (including recursion), objects, resources, and special float values (NAN/INF). Useful for test output, diffs, and diagnostics.
Install as a dev dependency:
composer require --dev sebastian/exporter
Begin by replacing var_dump()/dd() calls in tests or CLI debugging with Exporter. Instantiate once (it’s lightweight and stateless beyond config), then use:
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter;
dump($exporter->export($someComplexObject));
The README Usage section is the best starting point—skip the changelog and jump straight to the simple types and complex types examples.
trait ExportsVariables {
protected function export($var, bool $compact = false): string {
static $exporter;
$exporter ??= new Exporter(null, 50, 100); // configurable limits
return $compact ? $exporter->shortenedExport($var) : $exporter->export($var);
}
}
dump() with cleaner, recursive-safe output:
function d(...$vars) {
$exporter = new Exporter;
foreach ($vars as $var) {
fwrite(STDERR, $exporter->export($var) . "\n");
}
return $vars[0] ?? null;
}
Collection or Model instances—even with lazy relationships—without triggeringN+1 queries (thanks to lazy-object skipping since v6.2.0).shortenedExport() to show concise diffs:
$this->assertSame(
$expected,
$actual,
'Expected ' . $exporter->shortenedExport($expected) . ' but got ' . $exporter->shortenedExport($actual)
);
new Exporter(null, 25, 200); // maxElements = 25, maxString = 200 chars
≤6.3.1, ≤7.0.0) emit warnings for NAN/INF and casts. Pin to ^7.0.2 or ^6.3.2 if using PHP 8.5+.shortenedExport() Truncation: Aggressively shortens long strings/arrays—fine for assertion summaries, but use export() for full debugging. Watch for truncated nested structures where recursion markers (&0) may be lost.(int|null $maxElements = null, int $maxString = 100).resource types: they may hold open handles.xdebug.var_display_max_depth overrides dumps, set it to -1 (unlimited) or -2 (respect max depth) for consistency. Exporter ignores Xdebug config, so output remains predictable across environments.shortenedExport() with maxElements=10 is ~5x faster than full export()—ideal for CI failure summaries. Profile with microtime(true) before/after in non-critical paths.How can I help you explore Laravel packages today?