altre destinazioni

ultimi post

ultimi commenti

tag principali

categorie

archivi

powered by

  • WPFrontman + WP

friends

copyright

  • © 2004-2011
    Ludovico Magnocavallo
    tutti i diritti riservati

Dumping Vars the PEAR way

4 settembre 2003

0 commenti

categorie

Since it was announced on the PEAR newsgroup a few years ago, I have been using the Var_Dump class by Frederic Poeydomenge as one of my favourite PHP development tools. Dumping variables at strategic places has always been a practical and effective (if a bit simple) method of debugging in PHP and other languages. Unfortunately, when you’re working with complex data or with class instances, using the echo construct or the var_dump function is not very useful, unless you go to great lengths to extract what you really need to display from your data.

Var_Dump is a class that assists you in exactly this task, extracting all available information from your data and displaying it in a variety of ways. It is the PHP analogous to the Python pprint module, and one could spend an interesting few minutes comparing the two very different PHP and Python solutions to dumping (or pretty printing, which is more what’s happening here) data.

In its simplest form, the Var_Dump class can be used through a single static method, and a few optional constants. At the other end of the scale, a Var_Dump instance can be customized through skins, color sets, and display options to tailor its output to your exact needs. In this brief document, we will show examples of its use through a static method, since it’s the one you most probably will resort to during development. More complex examples can be found on the author’s site.

We start by importing the class code, and setting up a few variables and a class instance, that we will feed to Var_Dump.

require_once 'Var_Dump.php';

$a = array(0,1,2,34);
$b = array('a'=>'something', 'b'=>0, 0=>'b', 'c'=>$a);

class B {}

class A extends B {
    var $a;
    var $b = 1;
    function a($a) {
        $this->a = $a;
    }
}

$a_inst = new A(2);

Once we have that out of the way, we start feeding our data to Var_Dump by calling its display method as a static class method. First the simple array, without passing any extra argument to display.

Var_Dump::display($a);

Our friendly class spits out the following output:

0 Long (0)
1 Long (1)
2 Long (2)
3 Long (34)

Nice, isn’t it? Ok, let’s start using the optional arguments to display, and feed it something a bit more complex.

Var_Dump::display($b, 'b');

The second argument we passed to display in this example is the name of a key not to display, mainly used to avoid printing deeply nested data structures. As we can see from the following output, Var_Dump does a remarkable job of printing multidimensional arrays.

a String[9]  (something)
b Not parsed ()
0 String[1]  (b)
c Array(4)

    + 0 Long (0)
    | 1 Long (1)
    | 2 Long (2)
    + 3 Long (34)

On to the class instance, and the third argument you can pass to display.

Var_Dump::display($a_inst, null, VAR_DUMP_DISPLAY_MODE_HTML_TABLE);

As you have probably guessed, the third argument controls the output generated by Var_Dump through a few self-explanatory constants:

  • VAR_DUMP_DISPLAY_MODE_TEXT
  • VAR_DUMP_DISPLAY_MODE_HTML_TEXT
  • VAR_DUMP_DISPLAY_MODE_HTML_TABLE

The output of our instance is pretty interesting, as Var_Dump squeezes all available bits of information from it, including its class and its base classes, if any.

Class name String[1] (a)
Parent class String[1] (b)
Class vars (2)
a Null (Null)
b Long (1)
Class methods (1)
0 String[1] (a)
Object vars (2)
a Long (2)
b Long (1)

The fourth, and last of the arguments you can pass to display is again a constant from a self-explanatory set:

  • VAR_DUMP_NO_CLASS_VARS
  • VAR_DUMP_NO_CLASS_METHODS
  • VAR_DUMP_NO_CLASS_INFO

Let’s see what happens if we feed our instance to display, by using the VAR_DUMP_NO_CLASS_INFO constant, and reverting to the simpler output.

Var_Dump::display($a_inst, null,
    VAR_DUMP_DISPLAY_MODE_HTML, VAR_DUMP_NO_CLASS_INFO);
Class name   String[1] (a)
Parent class String[1] (b)
Object vars  Array(2) 
           
    + a Long (2)
    + b Long (1)

That’s all for tonight, if you have suggestions for PHP topics you would like to read here, drop me a message.