Ladybug provides an easy and extensible var_dump/print_r replacement for PHP 5.3+ projects. For example, with this library, the following is possible:
<?php
$var1 = NULL;
$var2 = 15;
$var3 = 15.5;
$var4 = 'hello world!';
$var5 = false;
ladybug_dump($var1, $var2, $var3, $var4, $var5);
As a result:
NULL int 15 float 15.5 string(12) "hello world!" bool FALSE
It is possible to dump any variable, including arrays, objects and resources:
<?php
$var = array(1, 2, 3);
ladybug_dump($var)
<?php
$var = new Foo();
ladybug_dump($var)
<?php
$connection = mysql_connect('localhost', 'dbuser', 'dbpassword');
mysql_select_db('dbname', $connection);
$result = mysql_query('SELECT * FROM user', $connection);
ladybug_dump($result);
<?php
$img = imagecreatefrompng(__DIR__ . '/images/ladybug.png');
ladybug_dump($img);
$ php examples/array.php
There are more examples in examples
directory.
As easy as download, include the library and use the provided helpers.
<?php
require_once 'lib/Ladybug/Autoloader.php';
Ladybug\Ladybug_Autoloader::register();
ladybug_dump($var1);
If you want to clone the project, you will have to execute git submodule init
and git submodule update
in
order to download the dependencies.
The are 5 helpers:
ladybug_dump($var1[, $var2[, ...]])
: Dumps one or more variables
ladybug_dump_die($var1[, $var2[, ...]])
: Dumps one or more variables and
terminates the current script
ladybug_dump_return($format, $var1[, $var2[, ...]])
: Dumps one or more variables and
returns the dump in any of the following formats:
- yml: Returns the dump in YAML
- json: Returns the dump in JSON
- xml: Returns the dump in XML
- php: Returns the dump in PHP arrays
ladybug_dump_ini([$extension])
: Dumps all configuration options
ladybug_dump_ext()
: Dumps loaded extensions
There are also some shortcuts in case you are not using this function names:
ld($var1[, $var2[, ...]])
: shortcut for ladybug_dump
ldd($var1[, $var2[, ...]])
: shortcut for ladybug_dump_die
ldr($format, $var1[, $var2[, ...]])
: shortcut for ladybug_return
Almost any display option can be easily customizable, using the function
ladybug_set($key, $value)
. Available options and default values:
array.max_nesting_level = 8
object.max_nesting_level = 3
object.show_data = TRUE
object.show_classinfo = TRUE
object.show_constants = TRUE
object.show_methods = TRUE
object.show_properties = TRUE
processor.active = TRUE
bool.html_color = '#008'
bool.cli_color = 'blue'
float.html_color = '#800'
float.cli_color = 'red'
int.html_color = '#800'
int.cli_color = 'red'
string.html_color = '#080'
string.cli_color = 'green'
string.show_quotes = TRUE
The library is easily extensible by adding new classes in lib/Ladybug/Extension/Object
and lib/Ladybug/Extension/Resource
directories. These new classes will have to
extend from LadybugExtension
class.
For example, there is already an extension to dump the rows of a mysql resultset,
in lib/Ladybug/Extension/Resource/MysqlResult.php
, so once is defined, Ladybug
will be able to find it and use its dump
method.
If you want to add a new dumper for DateTime object, you should
create a new class in lib/Ladybug/Extension/Object/Datetime.php
, that will
extend from LadybugExtension
and will have to provide a public method called
dump
.
Take a look at LadybugBundle