Without a doubt, PHP developers are lucky of having awesome debug utilities functions included in PHP as var_dump and debug_backtrace to know graphically (on text description) the content of some variable. Some languages like C, C# and others doesn't offer such an utility by default. This is really helpful when you are debugging why your code doesn't work or you are creating something from scratch.
However, some structures of a variable are pretty hard to understand and read even with the magical method var_dump. That's why there libraries that decided to make such feature even better like Kint. Kint for PHP is a pretty useful tool designed to present your debugging data in the absolutely best way possible graphically. In other words, it’s var_dump() and debug_backtrace() on steroids. Easy to use, but powerful and customizable. It is an essential addition to your development toolbox, so if you still don't understand the description of this library, it is used to see what is inside variables:
This treeview is displayed in the view, where you can take a look at the data by simply clicking anywhere on the bar to unfold it. Some features of the treeview are:
- Double click + to unfold all children
- Press d to toggle keyboard navigation.
- Press the “â” icon on the right to see what you’d need to run to get at a piece of data.
- Change tabs to see different views of data.
- You can sort tables of data by clicking on the headers.
Kint automatically detects, unpacks, and parses common formats like XML, base64, serialize, and JSON. Besides it detects common patterns like colors, filenames, tables, and timestamps and displays extra information about them providing the developer with the exact piece of code they need to access some information nested deep in the hierarchy.
You can use this library easily on any PHP project that uses an equal or higher version to 5.1.2. You can either install it with composer by simply running the following command in your project:
composer require kint-php/kint
Alternatively just download the kint.php file from the repository and then add it in your PHP file like:
<?php require 'kint.php';
After the installation by any of the 2 ways, you will be able to use the methods that Kint has to offer to help you with the debugging.
The project is made to be easy to use. Just like the default debug methods of the language do, you can use the static method dump of the Kint class to debug variables and even using shortcuts:
<?php // If using composer include the kint namespace use Kint; // Debug multiple arguments Kint::dump($GLOBALS, $_SERVER, "Hello World"); // Or use the d shortcut of Kint d($GLOBALS, $_SERVER, "Hello World"); // Debug backtrace Kint::trace(); // Debug backtrace shortcut d(1); // Basic output mode s($GLOBALS); // Text only output mode ~d($GLOBALS); // Disable kint Kint::$enabled_mode = false; // Debugs no longer have any effect d('Get off my lawn!');
Tips and tricks
- Kint is enabled by default, set
Kint::$enabled_mode = false;to turn its functionality completely off. The best practice is to enable Kint in a development environment only (or for example
Kint::$enabled_mode = ($_SERVER['REMOTE_ADDR'] === '<your IP>');) - so even if you accidentally leave a dump in production, no one will know.
- Kint has keyboard shortcuts! When Kint is visible, press D on the keyboard and you will be able to traverse the tree with arrows, hjkl, and tab keys - and expand/collapse nodes with space or enter.
- Clicking the + sign or the bar will open/close it.
- Double clicking the + sign will open/close it and all its children.
- Triple clicking the + sign in will open/close everything on the page.
- See the tiny arrows on the right of the output? Click them to open the node in a separate tab, or to show the access path for the value.
- There are a couple of real-time modifiers you can use:
~d($var)this call will output in plain text format.
+d($var)will disregard depth level limits and output everything. (Careful, this can hang your browser on large objects!)
!d($var)will expand the output automatically.
-d($var)will attempt to
ob_cleanthe previous output.
- You can combine modifiers too:
- To change display theme, use
Kint_Renderer_Rich::$theme = '<theme name>';where available options are:
'aante-light.css'. You can also pass the absolute path to a CSS file to use that instead:
ddd()were removed in Kint 2, but you can make them yourself with helper aliases
- Read the full documentation for more information.
How to contribute
The source code of Kint is hosted on Github and you can report issues, create pull requests as the project works under the MIT license.
Become a more social person