sebastian/exporter Documentation

repository·main·Indexed 27 days ago

https://github.com/sebastianbergmann/exporter

A PHP component for exporting PHP variables into human-readable string formats for visualization and debugging. It provides the SebastianBergmann\Exporter\Exporter class with methods like export() for detailed representations of simple and complex data types, including circular references, and shortenedExport() for compact output.

Tokens
629
Snippets
3
Records
4
Agent score
42%

What's inside sebastian/exporter

  1. Export PHP variables with the Exporter class

    main

    The SebastianBergmann\Exporter\Exporter class provides methods to convert PHP variables into a string format suitable for visualization.

    Use the export() method to get a detailed string representation of any variable, including objects, arrays, and simple types.

    <?php
    use SebastianBergmann\Exporter\Exporter;
    
    $exporter = new Exporter;
    
    // Exporting an Exception object
    print $exporter->export(new Exception);
  2. Install sebastian/exporter via Composer

    main

    You can add this library as a dependency to your project using Composer.

    To add it as a local, per-project dependency, use:

    composer require sebastian/exporter

    If you only need the library during development (e.g., for running test suites), add it as a development-time dependency:

    composer require --dev sebastian/exporter
  3. Export simple and complex data types

    main

    The export() method supports a wide range of PHP data types:

    • Simple types: integers, floats, strings, booleans, NAN, -INF, null, and resources.
    • Binary strings: represented in hex format.
    • Complex types: nested arrays and objects (including stdClass).
    • Circular references: correctly handles arrays or objects that reference themselves.
    <?php
    use SebastianBergmann\Exporter\Exporter;
    
    $exporter = new Exporter;
    
    // Simple types
    print $exporter->export(46);
    print $exporter->export(4.0);
    print $exporter->export('hello, world!');
    print $exporter->export(false);
    print $exporter->export(null);
    
    // Complex types (Arrays and Objects)
    print $exporter->export(array(array(1,2,3), array("",0,FALSE)));
    
    // Circular references
    $array = array();
    $array['self'] = &$array;
    print $exporter->export($array);
  4. Generate compact exports with shortenedExport()

    main
    If you need a less verbose representation of a variable, use the shortenedExport() method. This method produces a compact version of the output, such as using (...) for large arrays or long strings to save space.