Software testing. Programmer cartoon character with magnifier looking for defects in programme, application. Software bugs, errors, risks. Vector isolated concept metaphor illustration

PHP debugging with KINT

What is Kint.

PHP Kint is a debugging and diagnostic tool for PHP developers. It provides a simple and easy-to-use way to display and analyze PHP variables and objects in a readable and hierarchical format. Kint can help you quickly identify bugs and errors in your PHP code by allowing you to inspect the values of variables and objects at various stages of execution.   
Kint can be used in any PHP project and can be installed via Composer or downloaded directly from the Kint GitHub repository. Its usage is very straightforward, with only a few lines of code required to display a variable or object’s content. Kint GitHub repo

Adding PHP file to load before any page.

You can add php file which will be loaded before any other script is executed.  
To add php file to php you need to edit php.ini file and add path to file for auto_prepend_file. And restart the apache server.

Restart apache2 Ubuntu: 

systemctl restart apache2

php.ini

auto_prepend_file = "D:\xampp\htdocs\globalFns\index.php"

index.php

<?php
// Function to add pre tag before print_r
function print_pre($var)
{
    echo "<pre>";
    print_r($var);
    echo "</pre>";
}

Now you can use function print_pre anywhere in another project.

Adding Kint

Now install kint using composer


composer require kint-php/kint --dev
    

Now include the autoload file in index.php

index.php


<?php
require_once __DIR__."/vendor/autoload.php";
// Function to add pre tag before print_r
function print_pre($var)
{
    echo "<pre>";
    print_r($var);
    echo "</pre>";
}

Now you will be able to use kint function in any php project

example.php

<?php
Kint::dump($GLOBALS, $_SERVER); // pass any number of parameters
d($GLOBALS, $_SERVER); // or simply use d() as a shorthand
Kint::trace(); // Debug backtrace
s($GLOBALS); // Basic output mode
~d($GLOBALS); // Text only output mode
Kint::$enabled_mode = false; // Disable kint
d('Get off my lawn!'); // Debugs no longer have any effect

Limiting kint depth

Sometimes you will have large objects which will crash the page when you dump that for that we will create a new function dlimit and add alias to the kint.

index.php

<?php
require_once __DIR__."/vendor/autoload.php";
\Kint::$aliases[] = 'dlimit';
\Kint\Renderer\RichRenderer::$folder = false;
// Function to add pre tag before print_r
function print_pre($var)
{
    echo "<pre>";
    print_r($var);
    echo "</pre>";
}
//function to limit depth of kint dump
function dlimit($var, $limit = 3, $ob_clean = 10)
{
    //sometimes kint is not rendered because output buffer
    $ob_content = "";
    for ($i = 0; $i < $ob_clean; $i++) {
        $ob_content .= ob_get_clean();
    }
    //if no limit is passed use kint default depth
    if ($limit == 0) {
        $limit = \Kint::$depth_limit;
    }
    $prevLimit = \Kint::$depth_limit;
    \Kint::$depth_limit = $limit;
    //if we have output in buffer first dump that
    if ($ob_content) {
        echo '<b>output buffer content before your kint</b>';
        d($ob_content);
        echo '<b>your kint data</b>';
    }
    d($var);
    \Kint::$depth_limit = $prevLimit;
}

You can add other php libraries to the index file in this way for debugging and profiling.

Add a Comment

Your email address will not be published. Required fields are marked *