VarDumper enhances functionality of
var_export(). It is dealing with recursive references,
may highlight syntax and export closures.
- PHP 7.4 or higher.
The package could be installed with composer:
composer require yiisoft/var-dumper --prefer-dist
In case you want to echo a string representing variable contents use the following code:
That is useful for quick debugging. By default, it goes deep
10 levels into variable and highlights syntax. You may
adjust these settings via second and third argument of the method respectively.
For convenience, you can use the functions:
// Prints variables: d($variable, /* Further variables to dump. */); // Prints variables and terminate the current script: dd($variable, /* Further variables to dump. */);
To get a string representing variable contents, same as above but without
$string = \Yiisoft\VarDumper\VarDumper::create($variable)->asString(10, true);
10 is maximum recursion depth and
true is telling dumper to
In order to get a valid PHP expression string that can be evaluated by PHP parser, and the evaluation result will give back the variable value, use the following code:
$string = \Yiisoft\VarDumper\VarDumper::create($variable)->export();
It is similar to
var_export() but uses short array syntax, handles closures, and serializes objects.
In the above
export() will give you nicely formatted code. You can remove formatting by passing
false as the first
$useVariables argument allows specifying array of variables that will be in
use statement for closures.
That is especially useful if an object contains callbacks that should get info from upper scope.
$serializeObjects argument when given
false allows to force turn off using of serialization for objects so instead
closures and reflection API are used the same was as for exporting closures. De-serialization performance is better.
Closures are way more readable.
Current limitations are:
- Variables or properties that are anonymous classes or contain anonymous classes are not supported.
The package is tested with PHPUnit. To run tests:
The code is statically analyzed with Psalm. To run static analysis:
The Yii Caching Library is free software. It is released under the terms of the BSD License.
LICENSE for more information.
Maintained by Yii Software.