Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
197 lines (141 sloc) 5.86 KB

Ladybug: Simple and Extensible PHP Dumper

Build Status

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

Examples

It is possible to dump any variable, including arrays, objects and resources:

Dumping an array

<?php
    $var = array(1, 2, 3);
    ladybug_dump($var)

Dumping an object

<?php
    $var = new Foo();
    ladybug_dump($var)

Dumping a mysql resultset

<?php
    $connection = mysql_connect('localhost', 'dbuser', 'dbpassword');
    mysql_select_db('dbname', $connection);
    $result = mysql_query('SELECT * FROM user', $connection);

    ladybug_dump($result);

Dumping a GD image

<?php
    $img = imagecreatefrompng(__DIR__ . '/images/ladybug.png');
    ladybug_dump($img);

CLI (Command-line interface) support

$ php examples/array.php

There are more examples in examples directory.

Installation

As easy as download, include the library and use the provided helpers.

<?php
require_once 'lib/Ladybug/Autoloader.php';
Ladybug\Autoloader::register();

// alternatively, use another PSR-0 compliant autoloader (like the Symfony2 ClassLoader 
// for instance) and load the helpers manually: Ladybug\Loader::loadHelpers();

ladybug_dump($var1);

Using Composer

Composer is a project dependency manager for PHP. You have to list your dependencies in a composer.json file:

{
    "require": {
        "raulfraile/Ladybug": "master-dev"
    }
}

To actually install Ladybug in your project, download the composer binary and run it:

wget http://getcomposer.org/composer.phar
# or
curl -O http://getcomposer.org/composer.phar

php composer.phar install

After running the install command, you must see a new vendor directory that must contain the Ladybug code.

Using Ladybug as a git submodule

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.

Helpers

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

Customizable

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
  • string.show_quotes = '/Asset/tree.min.css'

Extensible

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.

Symfony2 users

Take a look at LadybugBundle

Something went wrong with that request. Please try again.