My favorites | Sign in
Project Home Downloads Issues Source
Project Information
Members
Featured
Downloads

Kint - debugging helper for PHP developers

Kint for PHP is a zero-setup debugging tool to output information about variables and stack traces prettily and comfortably. Extremely easy to use but powerful and customizable.

Kint is an ideological successor (and a superior alternative) to Krumo, which you may know as "Var_dump 2.0". It is no longer updated for quite some time and I've found many issues with it in personal use.

Kint boasts a lot of new unique features over Krumo such as the actual name of the variable being dumped, modifiers (+dd($var) produces different results than dd($var)) - and much more. It also bundles a full-featured backtrace output - the most efficient and comfortable to use out there. Check out the demo below for a quick preview.

Download the current version (2012-02-15 v32)

See live demo


Advantages over vanilla var_dump and/or print_r

  • Much more elegant and readable output - structured, collapsible, html source is escaped and visible on the page - not only when viewing the source of the page.
  • The name of the variable or whole expression is displayed along its value.
  • The place (file, line and method) where the output was called from is displayed too, so you don't have to wander where you left debugging info when you're done.
  • Accepts any number of parameters in one call. It also groups them for you to see, what was dumped in different iterations. Furthermore, calls from different places in the source are color-coded so you can see loops and repeated calls easily.
  • Does not choke on recursive variables.
  • Much more information is displayed about the variable in many cases, for example static properties of the dumped objects class [todo: add more examples]
  • Complex variables are dumped with a fixed nested depth so that it doesn't hang up your browser for enormous objects. This can be turned off on the fly - read on.
  • Ability to automatically call ob_clean() before outputting, in most cases this means the dump comes at the top of the page, not obstructed by page elements.
  • Ability to dynamically turn off any output so you can debug live systems without users suspecting anything.
  • Modifiers to alter the output (100% unique feature) - see below.
  • Much shorter name - d($var) (it's an alias for Kint::dump). See below for complete usage.

Installation

Simply place the kint directory anywhere comfy and include the Kint.class.php file from there.

You can optionally copy the included config.default.php and rename to config.php to override default values, but that's, again, entirely optional and you're fine without it.

Usage

To dump a variable:

     Kint::dump($variable1,$variable2);
     // or simply
     d($variable1,$variable2); // shorthand alias of Kint::dump()

There is often a need to halt execution after dumping a particular variable:

    dd($variable); // execution will stop after this statement; same as d($variable);die;

To print out variable information in simple text (no CSS style or javascript, but the output is white-space formatted), use

    s($variable); // stands for "simple"
    // or, as before
    sd($variable); // this will halt execution after displaying data

You can also use modifiers; see examples below (note the trailing symbol):

    +d($variable);
    -Kint::dump($variable); // works on direct and shorthand calls
    @d($variable);
  1. + will output bypassing the nesting depth limit of the variable (useful for outputting very complex objects, be warned, it may cause your browser to hang in extreme cases);
  2. - will run ob_clean() beforehand, so this dump is (most of the time) shown at the top of the page - instead of being wrapped in HTML that was already outputted. Best used with dd();
  3. @ will return the output of the Kint::dump() instead of displaying it on screen;

Note, these are possible because the class analyzes the PHP code itself where it was called from.

To display backtrace

   Kint::trace();

The displayed information for each step of the trace includes the source snippet, passed arguments and the object at the time of calling. Optionally accepts an array to output as trace.

Mouse navigation

  • Variable nodes and trace elements are expanded on click.
  • A click on the [+] icon will expand the clicked node and all of its children.
  • Double-clicking any [+] toggles expansion of all nodes on the page.
  • A single click on a variable or key name will auto-select it for easy copying.
  • When viewing trace, clicking the [+] will show source snippet where code took place, clicking on the Class->method() will display the object on which it was called (only applicable for dynamic method calls, obviously), and clicking on arguments will show passed arguments.

Configuration

You can find some configuration options in kint/config.php [todo: update with newly introduced settings]

  • pathDisplayCallback is of type callable Receives file name and line number as its two parameters. You can use it to:
    1. Output the filename as a link for your editor if it supports it (eg. IDE://{filename}:{linenum})
    2. Shorten the displayed paths. If you have configured directories like this: define('DIR_SYSTEM',dirname(__FILE__) . '/system/'), you can replace the filename path to represent 'DIR_SYSTEM' instead of eg. '/var/www/project/system/classes/'
  • maxStrLength the maximum length of a string before it is truncated and displayed separately
  • maxLevels the default depth at which the outputting stops, note that this can be overridden on the fly with the '+' modifier (see above).
  • enabled set this to false to prevent Kint from outputting any data.
  • skin the name of the included css file, skinning is yet to be improved.

For PhpStorm users

You can configure Kint to display the "called from" place in code as a link directly to your IDE. This is probably possible to do with other editors, but this example is PhpStorm specific.

  1. Install the Remote Call plugin via the IDE settings->plugins->Browse repositories
  2. Once Kint is installed, copy and rename config.default.php to config.php - leave it in the same directory.
  3. Now you can change this line:
  4. $_kintSettings['pathDisplayCallback'] = "kint::_debugPath";
to
$_kintSettings['pathDisplayCallback'] = "_kintLine";
and include the following function anywhere, advisably, in the same file
function _kintLine( $file, $line = NULL )
{
	// this whole `if()` block is OPTIONAL, delete it if you can't be bothered, or 
	// you can add more directory constants here to shorten your outputted file names.
	// directory constants are usually defined something like this: 
	// define( 'DIR_ROOT', realpath( __DIR__ . '/../../../' ) . '/' );
	if ( strpos( $file, DIR_ROOT ) === 0 ) {
		$shortenedName = 'DIR_ROOT' . DIRECTORY_SEPARATOR . substr( $file, strlen( DIR_ROOT ) );
	} else {
		$shortenedName = $file;
	}

	if ( !$line ) { // means this is called from resource type dump
		return $shortenedName;
	}

	// you can change the href for other editors, however the *class* of the anchor 
	// is used in javascript which stops browser redirection, but still calls the href
	return "<u><a class=\"kint-ide-link\" href=\"http://localhost:8091/?message={$file}:{$line}\">"
			. $shortenedName
			. "</a></u> line <i>{$line}</i>";
}

Just click on the link under any dump that displays where it was called from and if your editor is open, you will be taken directly to that place in the file.

Notes

This tool is used and tested extensively in real life development and is very stable. Issues and feature requests are very welcome and will be fixed/implemented as soon as possible.

Avoid using this tool in non-development servers, I cannot be held responsible for the performance, security and stability issues Kint may cause.

Version history

v.01 (2011-01-24)

  • Public release.
v.02 (2011-01-26)
  • Lots of interface improvements, mouse navigation additions.
  • Javascript and css minified for the downloaded version.
  • Configuration changed from a class with static variables to a global array to allow expressions in configuration values. The array is uniquely named and unset upon first initialization of the class.
v.03 (2011-02-07) v.12 (2011-03-24)
  • Rewrote the javascript code to not require jQuery so it's no longer included in the output (~60k of code saved).
  • As a result, the JS will only work on the latest browsers (IE9, Firefox3, Chrome). Who develops on an older browser anyway :)
  • Lots of small fixes here and there.
  • Changed the case of the class file http://code.google.com/p/kint/issues/detail?id=2
v.24 (2011-12-29)
  • note, the huge time gap is due to v.20 was released as a beta months ago;
  • fixed issues (mostly feature requests) from the issue tracker - up to, and including, #15;
  • implemented color coding to easily see dumps that are called from loops;
  • reorganized file structure;
  • added object output in trace. If a dynamic method was called, click on ClassName->methodName to see the object output;
  • s() and sd() rewritten to be much more informative, they now output the same info as the full-featured Kint::dump() - except the callee line;
v.26 (2011-12-29)
  • now dumped objects display the static values of their parent class - both public and private/protected
  • fixed recursive array display
v.31
  • fixed issues #17, #18, #20, #21, #22
  • doubleclick on minus icon in kint output collapses all
  • minor bugs and style inconsistencies
v.32
  • bugfix

To do

  • Add more advanced loop detection
  • Add some basic benchmarking features;
  • Implement better support for skins;
  • Implement a keyboard interface (ideas are VERY welcome);
  • Add more modifiers, ability to combine them;
  • Provide better documentation;
  • Tidy up this page :)

Author

Rokas Šleinius (CV)

Design by Mindaugas Stankaitis

Powered by Google Project Hosting