Skip to content

Commit 495ed38

Browse files
Updated README.md and added images for it.
1 parent bc575e2 commit 495ed38

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-1
lines changed

README.md

+134-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,137 @@ Code debug made easier and more enjoyable. This WordPress plugin includes a sui
55
Tools include:
66

77
* [Whoops - the "PHP errors for cool kids"](http://filp.github.io/whoops/)
8-
* [VarDumper Component](https://symfony.com/doc/current/components/var_dumper.html)
8+
* [VarDumper from Symfony](https://symfony.com/doc/current/components/var_dumper.html)
9+
* [Kint - a modern and powerful PHP debugging helper](https://kint-php.github.io/kint/)
10+
11+
## Debugging Variable Dumpers
12+
13+
This plugin provides two different tools for dumping variables:
14+
15+
* VarDumper from Symfony
16+
* Kint
17+
18+
VarDumper provides a simple container that displays where you place it.
19+
20+
Kint gathers all the data and displayed it at the bottom of the screen as a fixed position container. It also provides a call stack, which can be handy, and tracing functionality if you need it.
21+
22+
### Which one should you use?
23+
24+
It depends.
25+
26+
1. You want to simply display the contents of a variable: Use VarDumper's functions, i.e. `vdump()`, `vd()`, `vdd()`, or `vddd()`.
27+
2. You want the call stack in addition to the variable: Use Kint's functions: `d()`, `dd()`, or `ddd()`.
28+
29+
### Functions
30+
31+
| Task | VarDumper | Kint |
32+
| :--- | :--- | :--- |
33+
| Dumps the given variable(s) | `vd( mixed $var );` | `d( mixed $var [ , mixed $var2, ...] );` |
34+
| Dumps the given variable(s) | `vdump( mixed $var );` | `Kint::dump( mixed $var [ , mixed $var2, ...] );` |
35+
| Dumps and dies | `vdd( mixed $var );` | `dd( mixed $var [ , mixed $var2, ...] );` |
36+
| Dumps and dies | `vddd( mixed $var );` | `ddd( mixed $var [ , mixed $var2, ...] );` |
37+
| Dumps and dies | `vddd( mixed $var );` | `ddd( mixed $var [ , mixed $var2, ...] );` |
38+
| Dumps plain text | na | `s( mixed $var [ , mixed $var2, ...] );` |
39+
| Dumps debug trace | na | `Kint::trace();` |
40+
41+
For example:
42+
43+
```php
44+
add_action( 'loop_start', function() {
45+
46+
echo '<h2>VarDumper</h2>';
47+
echo '<pre><code>vd( get_the_title() );</code></pre>';
48+
vd( get_the_title() );
49+
50+
echo '<h2>Kint</h2>';
51+
echo '<pre><code>d( get_the_title() );</code></pre>';
52+
d( get_the_title() );
53+
54+
echo '<h2>Kint</h2>';
55+
echo '<pre><code>s( get_the_title() );</code></pre>';
56+
s( get_the_title() );
57+
58+
} );
59+
```
60+
61+
It renders like this:
62+
63+
![screenshot 1](screenshot-1.png)
64+
65+
Notice the subtle differences between the three different ways of dumping the title out to the browser:
66+
67+
* VarDumper's `vd()` provides a very simple highlighter around the title.
68+
* Kint's `d()` is shown at the bottom of the screen and contains the function, string length, title, and call stack.
69+
* Kint's `s()` provides a similar display as `vd()` except that it also includes what was passed into the function (shown in the dashed box), string length, value (title), and where the function was called from in your code.
70+
71+
#### How about for arrays, objects, etc?
72+
73+
Here's the example code for `vd()`:
74+
75+
```php
76+
add_action( 'loop_start', function() {
77+
$array = [
78+
'ID' => get_the_ID(),
79+
'title' => get_the_title(),
80+
];
81+
82+
vd( $array );
83+
} );
84+
```
85+
86+
It renders as:
87+
88+
![screenshot of vd( $array )](screenshot-2.png)
89+
90+
Here's the example code for `d()`:
91+
92+
```php
93+
add_action( 'loop_start', function() {
94+
$array = [
95+
'ID' => get_the_ID(),
96+
'title' => get_the_title(),
97+
];
98+
99+
d( $array );
100+
} );
101+
```
102+
103+
It renders as:
104+
105+
![screenshot of d( $array )](screenshot-3.png)
106+
107+
Here's the example code for `s()`:
108+
109+
```php
110+
add_action( 'loop_start', function() {
111+
$array = [
112+
'ID' => get_the_ID(),
113+
'title' => get_the_title(),
114+
];
115+
116+
s( $array );
117+
} );
118+
```
119+
120+
It renders as:
121+
122+
![screenshot of s( $array )](screenshot-4.png)
123+
124+
## Whoops - An Awesome PHP Error Tool
125+
126+
The built-in PHP error container is basic and not as helpful as it could be. On top of that, it's rather ugly. Wouldn't you agree?
127+
128+
The Whoops package gives you a cool interface that is helpful, interactive, and quite nice to look at .
129+
130+
Consider the error this code would produce:
131+
132+
```php
133+
add_action( 'loop_start', function() {
134+
does_not_exist();
135+
} );
136+
```
137+
138+
![Whoops error interface](docs/whoops.gif)
139+
140+
You can learn more about Whoops by visiting [http://filp.github.io/whoops/](http://filp.github.io/whoops/).
141+

screenshot-1.png

107 KB
Loading

screenshot-2.png

13.6 KB
Loading

screenshot-3.png

96.9 KB
Loading

screenshot-4.png

32.4 KB
Loading

src/functions.php

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ function vd( $var ) {
5454
}
5555

5656
if ( ! function_exists( 'dd' ) ) {
57+
// Add dd() to Kint.
58+
Kint::$aliases[] = 'dd';
5759
/**
5860
* Kint: Dumps the given variable(s) and then ends the execution of the program.
5961
*
@@ -88,6 +90,9 @@ function vdd( $var ) {
8890
}
8991

9092
if ( ! function_exists( 'ddd' ) ) {
93+
// Add ddd() to Kint.
94+
Kint::$aliases[] = 'ddd';
95+
9196
/**
9297
* Kint: Dumps the given variable and then ends the execution of the program.
9398
*

0 commit comments

Comments
 (0)