# VarDump

VarDump

Namespace Chevere\VarDump

The VarDump package provides an alternative to var_dump (opens new window) with a richer feature set, including formatting for the generation of a myriad of dump documents.

💡 VarDump introduction

Read VarDump for PHP (opens new window) at Rodolfo's blog for a compressive introduction to this package.

# Installing

VarDump is available through Packagist (opens new window) and the repository source is at GitHub (opens new window).

composer require --dev chevere/var-dump

# Demo

HTML demo

# Features

  • Provides vd (VarDump) and vdd (VarDump & die) helpers
  • Colorful typed variable introspection
  • Reports visibility
  • Detects circular references
  • Stream-based
    • Writes line-by-line
    • Handles really large documents
  • Provides caller information
  • Console document
    • Colorful console output (where available)
  • Plain document
    • Same as console (no-color)
    • Same as copy HTML text
  • HTML document
    • Colorful representation
    • Portable (also used on xrDebug)

# Helpers

💡 TL;DR: You can replace var_dump in both CLI and HTTP context using the following helpers:

# VarDump (vd)

Function vd is a drop-in replacement for var_dump. It prints information about one or more variables to the output stream.

vd($var1, $var2,);
// more code

# VarDump and die (vdd)

Function vdd does same as vd, but with die(0) which halts further execution.

vdd($var);
// exit()

# Advanced usage

A VarDump needs a format and an output. In the example below a VarDump object is created with console colored formatting and output:

use Chevere\VarDump\Formats\ConsoleFormat;
use Chevere\VarDump\Outputs\ConsoleOutput;
use Chevere\VarDump\VarDump;
use function Chevere\VarDump\varDumpConsole;

$varDump = new VarDump(
    formatter: new ConsoleFormat,
    outputter: new ConsoleOutput
);
// Same as:
$varDump = varDumpConsole();

# Instance helpers

The following helper functions can be used to initialize a VarDump object.

use function Chevere\VarDump\varDumpPlain;
use function Chevere\VarDump\varDumpConsole;
use function Chevere\VarDump\varDumpHtml;

// Plain text
$varDump = varDumpPlain();
// Colored console
$varDump = varDumpConsole();
// HTML
$varDump = varDumpHtml();

The varDump helper can be used to access to the VarDumpInstance. If no instance has been created it will provide a console VarDump.

use function Chevere\VarDump\varDump;

$varDump = varDump();

# Passing variables

The method withVars is used to pass ...$variables (variadic).

$varDump = $varDump->withVars('a var', [], null);

# Shifting traces

The dump information could be affected by layers on top of VarDump, the method withShift can be used to indicate how many previous backtrace should be removed.

$varDump = $varDump->withShift(1); // removes the first trace

# Process

The method process is used to trigger the var dumping process. It requires to pass a writer where the dump information will be written.

use Chevere\Writer\StreamWriter;
use function Chevere\Writer\streamFor;

$varDump->process(
    writer: new StreamWriter(
      streamFor('php://stdout', 'w')
    )
);

# VarDumpInstance

By initializing Chevere/VarDump/VarDumpInstance you can change the default VarDump context, which is used in vd and vdd helpers.

In the code below, varDumpHtml has been passed to configure VarDumpInstance to handle everything with HTML formatting.

use Chevere\VarDump\VarDumpInstance;
use function Chevere\VarDump\varDump;
use function Chevere\VarDump\varDumpHtml;

// Default VarDump console
$varDump = varDump();
// Set instance to VarDump Html
new VarDumpInstance(varDumpHtml());
// Instance VarDump html
$varDump = varDump();