0 follower

Final Class Yiisoft\VarDumper\VarDumper

InheritanceYiisoft\VarDumper\VarDumper

VarDumper provides enhanced versions of the PHP functions var_dump() and var_export().

It can:

  • Correctly identify the recursively referenced objects in a complex object structure.
  • Recursively control depth to avoid indefinite recursive display of some peculiar variables.
  • Export closures and objects.
  • Highlight output.
  • Format output.

Public Methods

Hide inherited methods

Method Description Defined By
asJson() Dumps a variable as a JSON string. Yiisoft\VarDumper\VarDumper
asPrimitives() Dumps the variable as PHP primitives in JSON decoded style. Yiisoft\VarDumper\VarDumper
asString() Dumps a variable in terms of a string. Yiisoft\VarDumper\VarDumper
create() Yiisoft\VarDumper\VarDumper
dump() Prints a variable. Yiisoft\VarDumper\VarDumper
export() Exports a variable as a string containing PHP code. Yiisoft\VarDumper\VarDumper
getDefaultHandler() Yiisoft\VarDumper\VarDumper
setDefaultHandler() Yiisoft\VarDumper\VarDumper
withOffset() Sets offset string to use to indicate nesting level. Yiisoft\VarDumper\VarDumper

Constants

Hide inherited constants

Constant Value Description Defined By
DEPTH_LIMIT_EXCEEDED_PROPERTY '$__depth_limit_exceeded__$' Yiisoft\VarDumper\VarDumper
OBJECT_CLASS_PROPERTY '$__class__$' Yiisoft\VarDumper\VarDumper
OBJECT_ID_PROPERTY '$__id__$' Yiisoft\VarDumper\VarDumper
VAR_TYPE_ARRAY 'array' Yiisoft\VarDumper\VarDumper
VAR_TYPE_OBJECT 'object' Yiisoft\VarDumper\VarDumper
VAR_TYPE_PROPERTY '$__type__$' Yiisoft\VarDumper\VarDumper
VAR_TYPE_RESOURCE 'resource' Yiisoft\VarDumper\VarDumper

Method Details

Hide inherited methods

asJson() public method

Dumps a variable as a JSON string.

This method provides similar functionality to the json_encode() but is more robust when handling complex objects.

public asJson( boolean $format true, integer $depth 10 ): string
$format boolean

Use whitespaces in returned data to format it

$depth integer

Maximum depth that the dumper should go into the variable. Defaults to 10.

return string

The json representation of the variable.

throws JsonException

                public function asJson(bool $format = true, int $depth = 10): string
{
    /** @var mixed $output */
    $output = $this->asPrimitives($depth);
    if ($format) {
        return json_encode($output, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
    }
    return json_encode($output, JSON_THROW_ON_ERROR);
}

            
asPrimitives() public method

Dumps the variable as PHP primitives in JSON decoded style.

public asPrimitives( integer $depth 10 ): mixed
$depth integer

Maximum depth that the dumper should go into the variable. Defaults to 10.

throws ReflectionException

                public function asPrimitives(int $depth = 10): mixed
{
    return $this->exportPrimitives($this->variable, $depth, 0);
}

            
asString() public method

Dumps a variable in terms of a string.

This method achieves the similar functionality as var_dump() and print_r() but is more robust when handling complex objects.

public asString( integer $depth 10 ): string
$depth integer

Maximum depth that the dumper should go into the variable. Defaults to 10.

return string

The string representation of the variable.

throws ReflectionException

                public function asString(int $depth = 10): string
{
    return $this->dumpInternal($this->variable, true, $depth, 0);
}

            
create() public static method

public static create( mixed $variable ): Yiisoft\VarDumper\VarDumper
$variable mixed

Variable to dump.

return Yiisoft\VarDumper\VarDumper

An instance containing variable to dump.

                public static function create(mixed $variable): self
{
    return new self($variable);
}

            
dump() public static method

Prints a variable.

This method achieves the similar functionality as var_dump() and print_r() but is more robust when handling complex objects.

public static dump( mixed $variable, integer $depth 10, boolean $highlight true ): void
$variable mixed

Variable to be dumped.

$depth integer

Maximum depth that the dumper should go into the variable. Defaults to 10.

$highlight boolean

Whether the result should be syntax-highlighted.

throws ReflectionException

                public static function dump(mixed $variable, int $depth = 10, bool $highlight = true): void
{
    self::getDefaultHandler()->handle($variable, $depth, $highlight);
}

            
export() public method

Exports a variable as a string containing PHP code.

The string is a valid PHP expression that can be evaluated by PHP parser and the evaluation result will give back the variable value.

This method is similar to var_export(). The main difference is that it generates more compact string representation using short array syntax.

It also handles closures with Yiisoft\VarDumper\ClosureExporter and objects by using the PHP functions serialize() and unserialize().

public export( boolean $format true, string[] $useVariables = [], boolean $serializeObjects true ): string
$format boolean

Whatever to format code.

$useVariables string[]

Array of variables used in use statement (['$params', '$config'])

$serializeObjects boolean

If it is true all objects will be serialized except objects with closure(s). If it is false only objects of internal classes will be serialized.

return string

A PHP code representation of the variable.

throws ReflectionException

                public function export(bool $format = true, array $useVariables = [], bool $serializeObjects = true): string
{
    $this->useVarInClosures = $useVariables;
    $this->serializeObjects = $serializeObjects;
    return $this->exportInternal($this->variable, $format, 0);
}

            
getDefaultHandler() public static method

public static getDefaultHandler( ): Yiisoft\VarDumper\HandlerInterface

                public static function getDefaultHandler(): HandlerInterface
{
    return self::$defaultHandler ??= new EchoHandler();
}

            
setDefaultHandler() public static method

public static setDefaultHandler( Yiisoft\VarDumper\HandlerInterface $handler ): void
$handler Yiisoft\VarDumper\HandlerInterface

                public static function setDefaultHandler(HandlerInterface $handler): void
{
    self::$defaultHandler = $handler;
}

            
withOffset() public method

Sets offset string to use to indicate nesting level.

public withOffset( string $offset ): Yiisoft\VarDumper\VarDumper
$offset string

The offset string.

return Yiisoft\VarDumper\VarDumper

New instance with a given offset.

                public function withOffset(string $offset): self
{
    $new = clone $this;
    $new->offset = $offset;
    return $new;
}