agashe / var-dumper
Better alternative to var_dump function in PHP
Installs: 15
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:HTML
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-11 11:41:20 UTC
README
A variables dumper to help with debugging , designed to be a better alternative to the standard var_dump function in PHP.
Features
- Easy to use , zero configurations
- Dumps all types of data in PHP , including (int, float , strings , bool, objects, arrays, resources, enums, closures and anonymous classes)
- Auto recognition for text with known patterns like Emails , URLs .. etc
- Support dumping to CLI , Web content and files
- Pretty colorful output for CLI
- Interactive web debugging with folding for objects / arrays / long text
- Support dumping to
.json
files - Could be used for logs , using the dump to file option
Installation
composer require agashe/var-dumper
Documentation
To start using Var-Dumper , all what you have to do , is to include the autoload.php
file into your php script , then call one of the helpers functions.
<?php
require 'vendor/autoload.php';
d("data 1", "data 2");
The output of the prev example will be :
[ Time : 11-01-2024 07:43:24 / File : index.php / Line : 5 ]
string (11) => "hello world"
integer => 123
For each dump call , the dumper will automatically add the time , file name and the line number , so it became easier to debug in large projects , and log files.
Var-Dumper provides 4 helper functions that could be used anywhere in your project :
d(...$vars)
and dump(...$vars)
Functions
Those 2 functions are identical , they both can accept any numbers of variables and will serve the output based on your output type , either it's a CLI or Web.
d(null , 10.59, true);
dump(null , 10.59, true);
And here is the output :
[ Time : 11-01-2024 07:59:56 / File : test2.php / Line : 5 ]
NULL
double => 10.59
boolean => true
[ Time : 11-01-2024 07:59:56 / File : test2.php / Line : 7 ]
NULL
double => 10.59
boolean => true
The naming is just for sake of compatibility , like in case you wanted to replace old dumper , that use , d
or dump
as their function name.
dd(...$vars)
Function
The dd
function which stands for "dump and die" , unlike the 2 prev functions , this function will terminate the execution once the dump is complete.
In case you dump for web (in browser) , the dumper will return "500 Server Error" HTTP status code.
dump_tp_file($filePath, ...$vars)
Function
Dump to file accept one mandatory argument before you path you variables for the dump. And the argument is the full path to the dump file.
class Foo {
public $name;
private $age;
}
$foo = new Foo();
$foo->name = "hello world";
dump_to_file(__DIR__ . '/path/to/dump.txt' , $foo);
Now let's check dump.txt
, we should have something like :
[ Time : 11-01-2024 08:12:46 / File : index.php / Line : 12 ]
object (Foo) #2 (2) => {
name => "hello world"
age (private) => NULL
}
So far the dumper supports dumping to regular .txt
and .json
files. In case of a .json
file , the dumper will automatically detect that from the extension.
In case of .txt
files , the nes dump will be appended to the original content. First it will decode the old content , append the new json
formatted content , then re-encode everything and save.
So assume we have a .json
file called data.json
with following content :
{
"some_data": {
"item_1": "data_1",
"item_2": "data_2",
"item_3": "data_3"
}
}
So now let's our prev example , but this time with that json file :
// ...
dump_to_file(__DIR__ . '/path/to/data.json' , $foo);
Now if we opened data.json
, we will find that the content was appended :
{
"some_data": {
"item_1": "data_1",
"item_2": "data_2",
"item_3": "data_3"
},
"dump_e2bab7e": {
"timestamp": {
"time": "11-01-2024 08:23:55",
"file": "index.php",
"line": 13
},
"data": {
"object (Foo) #2 (2)": {
"name": "hello world",
"age (private)": "NULL"
}
}
}
}
The dump_e2bab7e
is just and autogenerated unique hash identifier to prevent collision between dumps.
Examples
In the following examples we will see , different kind of use cases for the dumper and how the output will look like in each of the CLI / WEB / FILE / JSON outputs.
Basic data types
d('ahmed', 5.6, false, M_PI, NULL);
dump_to_file(__DIR__ . 'data.txt', 'ahmed', 5.6, false, M_PI, NULL);
dump_to_file(__DIR__ . 'data.json', 'ahmed', 5.6, false, M_PI, NULL);
Output CLI / TXT FILE :
[ Time : 11-01-2024 09:51:28 / File : index.php / Line : 5 ]
string (5) => "ahmed"
double => 5.6
boolean => false
double => 3.1415926535898
NULL
Output JSON :
{
"dump_712db6d": {
"timestamp": {
"time": "11-01-2024 09:51:28",
"file": "index.php",
"line": 7
},
"data": {
"string (5)": "ahmed",
"double": 3.1415926535898,
"boolean": "false",
"value": null
}
}
}
Output WEB :
Text patterns
d("/^([A-Za-z1-9])$/");
d(serialize([1, 2, 3]));
d("http://www.helloworld.com");
d("test@example.com");
d('20-5-1995');
dump_to_file(__DIR__ . 'data.txt', "/^([A-Za-z1-9])$/");
dump_to_file(__DIR__ . 'data.txt', serialize([1, 2, 3]));
dump_to_file(__DIR__ . 'data.txt', "http://www.helloworld.com");
dump_to_file(__DIR__ . 'data.txt', "test@example.com");
dump_to_file(__DIR__ . 'data.txt', '20-5-1995');
dump_to_file(__DIR__ . 'data.json', "/^([A-Za-z1-9])$/");
dump_to_file(__DIR__ . 'data.json', serialize([1, 2, 3]));
dump_to_file(__DIR__ . 'data.json', "http://www.helloworld.com");
dump_to_file(__DIR__ . 'data.json', "test@example.com");
dump_to_file(__DIR__ . 'data.json', '20-5-1995');
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 5 ]
RegExp (17) => /^([A-Za-z1-9])$/
[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 6 ]
Serializable (30) => "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 7 ]
URL (25) => "http://www.helloworld.com"
[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 8 ]
Email (16) => "test@example.com"
[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 9 ]
Date/Time (9) => "20-5-1995"
Output JSON :
{
"dump_82c13d8": {
"timestamp": {
"time": "11-01-2024 11:06:49",
"file": "index.php",
"line": 17
},
"data": {
"RegExp (17)": "\/^([A-Za-z1-9])$\/"
}
},
"dump_716e868": {
"timestamp": {
"time": "11-01-2024 11:06:49",
"file": "index.php",
"line": 18
},
"data": {
"Serializable (30)": "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
}
},
"dump_8a02891": {
"timestamp": {
"time": "11-01-2024 11:06:49",
"file": "index.php",
"line": 19
},
"data": {
"URL (25)": "http:\/\/www.helloworld.com"
}
},
"dump_3d24331": {
"timestamp": {
"time": "11-01-2024 11:06:49",
"file": "index.php",
"line": 20
},
"data": {
"Email (16)": "test@example.com"
}
},
"dump_47530be": {
"timestamp": {
"time": "11-01-2024 11:06:49",
"file": "index.php",
"line": 21
},
"data": {
"Date\/Time (9)": "20-5-1995"
}
}
}
Output WEB :
Objects
class Foo
{
private $greeting = "hello";
public $bar;
}
class Bar
{
public $name;
private $age;
}
$bar = new Bar();
$bar->name = 'ahmed';
$foo = new Foo();
$foo->bar = $bar;
d($foo);
dump_to_file(__DIR__ . '/data.txt', $foo);
dump_to_file(__DIR__ . '/data.json', $foo);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:22:07 / File : index.php / Line : 26 ]
object (Foo) #4 (2) => {
greeting (private) => "hello"
[bar] object (Bar) #2 (2) => {
name => "ahmed"
age (private) => NULL
}
}
Output JSON :
{
"dump_c5606d6": {
"timestamp": {
"time": "11-01-2024 11:22:07",
"file": "index.php",
"line": 27
},
"data": {
"object (Foo) #4 (2)": {
"greeting (private)": "hello",
"[bar] object (Bar) #2 (2)": {
"name": "ahmed",
"age (private)": "NULL"
}
}
}
}
}
Output WEB :
Objects and arrays can be folded/unfolded :
Anonymous class
$class = new class
{
public $name;
public function __construct()
{
$this->name = 'omar';
}
};
d($class);
dump_to_file(__DIR__ . '/data.txt', $class);
dump_to_file(__DIR__ . '/data.json', $class);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:26:41 / File : index.php / Line : 15 ]
object (class@anonymous) #2 (1) => {
name => "omar"
}
Output JSON :
{
"dump_98cfb27": {
"timestamp": {
"time": "11-01-2024 11:26:41",
"file": "index.php",
"line": 16
},
"data": {
"object (class@anonymous) #2 (1)": {
"name": "omar"
}
}
}
}
Output WEB :
Arrays
$arr = [
'a' => 'apple',
'b' => 'banana',
'c' => [1, 2, [1, 2, 3]],
'd' => 'dates',
];
d($arr);
dump_to_file(__DIR__ . '/data.txt', $arr);
dump_to_file(__DIR__ . '/data.json', $arr);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:31:48 / File : index.php / Line : 12 ]
array (4) => [
a => "apple"
b => "banana"
[c] array (3) => [
0 => 1
1 => 2
[2] array (3) => [
0 => 1
1 => 2
2 => 3
]
]
d => "dates"
]
Output JSON :
{
"dump_431b83a": {
"timestamp": {
"time": "11-01-2024 11:31:48",
"file": "index.php",
"line": 13
},
"data": {
"array (4)": {
"a": "apple",
"b": "banana",
"[c] array (3)": {
"0": 1,
"1": 2,
"[2] array (3)": {
"0": 1,
"1": 2,
"2": 3
}
},
"d": "dates"
}
}
}
}
Output WEB :
Resources
$file = fopen(__DIR__ . '/something.txt', 'r');
d($file);
dump_to_file(__DIR__ . '/data.txt', $file);
dump_to_file(__DIR__ . '/data.json', $file);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:37:00 / File : index.php / Line : 7 ]
Resource id #12 => {
timed_out => false
blocked => true
eof => false
wrapper_type => "plainfile"
stream_type => "STDIO"
mode => "r"
unread_bytes => 0
seekable => true
uri => "/path/to/something.txt"
[options] array (0) => []
}
Output JSON :
{
"dump_caf8a63": {
"timestamp": {
"time": "11-01-2024 11:37:00",
"file": "index.php",
"line": 8
},
"data": {
"Resource id #12": {
"timed_out": "false",
"blocked": "true",
"eof": "false",
"wrapper_type": "plainfile",
"stream_type": "STDIO",
"mode": "r",
"unread_bytes": 0,
"seekable": "true",
"uri": "\/path\/to\/something.txt",
"[options] array (0)": {}
}
}
}
}
Output WEB :
Closures
$closure = function ($name, $age = '') {
return $name;
};
d($closure);
dump_to_file(__DIR__ . '/data.txt', $closure);
dump_to_file(__DIR__ . '/data.json', $closure);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:45:15 / File : index.php / Line : 9 ]
object (Closure) #2 (2) => {
name => "required"
age => "optional"
}
Output JSON :
{
"dump_9a1d82e": {
"timestamp": {
"time": "11-01-2024 11:45:15",
"file": "index.php",
"line": 10
},
"data": {
"object (Closure) #2 (2)": {
"name": "required",
"age": "optional"
}
}
}
}
Output WEB :
Long text
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in.";
d($text);
dump_to_file(__DIR__ . '/data.txt', $text);
dump_to_file(__DIR__ . '/data.json', $text);
Output CLI / TXT FILE :
[ Time : 11-01-2024 11:47:55 / File : index.php / Line : 7 ]
string (550) => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in. "
Output JSON :
{
"dump_d637a31": {
"timestamp": {
"time": "11-01-2024 11:47:55",
"file": "index.php",
"line": 8
},
"data": {
"string (550)": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in."
}
}
}
Output WEB :
And as objects and arrays , long text could be folded/unfolded by click on the text :
License
(Var-Dumper) released under the terms of the MIT license.