Couple of convenience methods to add to your bootstrap.php

Although most of the convenience methods will be removed from CakePHP going forward (see the note about basics.php), and I say good riddance, you still have the opportunity to add your own to bootstrap.php.

For example a couple that I like are wrappers for var_dump(). The main reason being is that var_dump() will properly display NULL and FALSE. Unlike debug() and pr() (alias to) print_r(), which will show… well… nothing.

So here you go:

function vd($var) {
  if(Configure::read() > 0) {
    echo '<pre>';
      echo var_dump($var);
    echo '</pre>';
  }
}

Or, if you’d like to die() out:

function vdd($var) {
  if(Configure::read() > 0) {    
      die(var_dump($var));
  }
}

8 thoughts on “Couple of convenience methods to add to your bootstrap.php

  1. function vd($var) {
    if (Configure::read() > 0) {
    foreach($var as $a => $b) {
    echo $a;
    if(is_array($b)) {
    vd($b);
    } else {
    echo $b;
    }
    }
    }
    }

  2. I think there is a small mistake in the vdd function. The close tag for pre will never occur because is after die().
    In this way it works fine.

    function vdd($var) {
    if(Configure::read() > 0) {
    echo '

    ';
        var_dump($var);
        echo '

    ';
    die(); // or exit;
    }
    }

    Anyway is not a relevant mistake. You make a great job.

  3. @CAT Shannon

    LOL, Duh… very good point :) Anyways in that case the pre tags are not really needed anyway. I’ve just simplified the function a little. But your suggestions is just as good.

    @petteyg359

    Thanks for sharing.

    1. The wrapper functions may save you a few keystrokes, or many if you use them often, but they slow down execution. Using a wrapper function inside of your own function saves you a few keystrokes one time only. Just type the full function name…

  4. @petteyg359

    Exactly, that’s why they are being removed from the usage inside the core. However, when developing they are simply short-cuts and in case of someone accidentally forgetting to remove print_r() in production, can actually quite valuable.

    @Angel S. Moreno

    Same problem, doesn’t properly display certain values.

    A side note, it is actually quite nice to implement a solution like: http://krumo.sourceforge.net/
    (Hmm… I should probably do an example for cake…)

Leave a comment