sebastian/exporter
Exports PHP variables into readable, stable string representations for visualization and debugging. Handles scalars, arrays, objects, resources, binary strings, and recursive references, producing clear output useful in test failures and developer tooling.
Installation:
composer require --dev sebastian/exporter
Add --dev if only needed for development/testing.
Basic Usage:
use SebastianBergmann\Exporter\Exporter;
$exporter = new Exporter();
$output = $exporter->export($yourVariable);
First Use Case:
Replace dd() or var_dump() in Laravel debug statements:
// In a controller or service
$user = User::with('roles')->find(1);
$exporter = new Exporter();
dd($exporter->export($user)); // Clean, structured output
Exporter class: Methods like export(), shortenedExport(), and toArray().Debugging Complex Data:
// Eloquent relationships with circular references
$exporter = new Exporter();
$output = $exporter->export($user->with('posts', 'roles')->get());
API Response Inspection:
$response = $this->getJson('/api/users');
$exporter = new Exporter();
$this->assertStringContainsString('"data":', $exporter->export($response->json()));
Test Assertions:
$this->assertEquals(
'Array &0 ( ... )',
$exporter->export($this->getMockArray())
);
Custom Debug Helper:
// In `app/Helpers/DebugHelper.php`
if (app()->environment('local')) {
$exporter = new Exporter();
$exporter->export(request()->all());
}
Laravel Debugbar:
Extend the Data panel to use Exporter for structured variable display.
Debugbar::info($exporter->export($model));
Tinker:
Override Tinker\Console::handle() to auto-export variables:
Tinker::extend('e', function ($value) {
return (new Exporter())->export($value);
});
Artisan Commands: Use for CLI debugging:
$exporter = new Exporter();
$this->line($exporter->export($this->getLargeDataset()));
Logging:
Replace Log::debug() for complex objects:
Log::debug('Complex data', [
'data' => $exporter->shortenedExport($complexObject)
]);
Shortened Output:
// For large arrays/objects
$exporter->shortenedExport($largeArray, 100); // Limit to 100 chars
Array Size Limits:
$exporter->setArrayLimit(50); // Truncate arrays >50 items
Caching Exports: Cache repeated exports (e.g., in tests):
static $cache = [];
$key = spl_object_hash($object);
if (!isset($cache[$key])) {
$cache[$key] = $exporter->export($object);
}
PHP Version Limits:
^8.0. Use ^7.0 for newer PHP.NAN/INF warnings automatically (no action needed).Circular References:
shortenedExport() for cleaner output:
$exporter->shortenedExport($selfReferentialObject);
Large Arrays:
$exporter->setArrayLimit(null); // Disable truncation
Binary Strings:
0x000102).$exporter->exportString($binaryString, true); // Force readable output
Private Properties:
toArray() drops private properties redeclared in child classes (fixed in v7.0.3+).Lazy Objects:
shortenedExport() to skip lazy properties.SplObjectStorage:
Unexpected Output:
\0) in strings:
$exporter->exportString($string, false); // Force hex for debugging
Performance Issues:
Xdebug to identify slow exports (e.g., deep recursion).shortenedExport() for quick checks.CI/CD Warnings:
E_WARNING for NAN/INF by updating to ^8.0 or later.Custom Types:
Exporter to handle custom classes:
class CustomExporter extends Exporter {
protected function exportCustomObject($obj) {
return "Custom: " . $obj->getId();
}
}
String Length Limits:
$exporter = new Exporter(500); // Shorter strings
Array Export:
Resource Handling:
resource(#) of type(stream).Custom Exporters:
Exporter to add support for domain-specific types:
class LaravelExporter extends Exporter {
protected function exportCarbon($carbon) {
return $carbon->toIso8601String();
}
}
Formatters:
exportFloat(), exportString(), etc., for custom formatting.Output Filters:
shortenedExport() or setArrayLimit() to control verbosity.Integration with Laravel:
Exporter to the container:
$this->app->singleton(Exporter::class, function () {
return new Exporter(app()->environment('local') ? 2000 : 500);
});
How can I help you explore Laravel packages today?