Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
PHP 5.3+ Extensible Dumper
PHP

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
examples
lib/Ladybug
tests
.gitignore
.gitmodules
.travis.yml
CHANGELOG.md
LICENSE
Ladybug.zip
README.md
composer.json
phpunit.xml.dist

README.md

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(
        array(
            'name' => 'Raul',
            'age' => 29
        ),
        array(
            'name' => 'John',
            'age' => 27
        )
    );

    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

Using Composer (recommended)

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. Then, you must load ladybug helpers:

<?php
require_once __DIR__ . '/vendor/autoload.php'; // file generated by composer. Change to fit your needs
Ladybug\Loader::loadHelpers();

ladybug_dump($var1);

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.

Download zip file

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);

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:

  • yaml: 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:

  • general.expanded = false
  • 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.