The most useful function you will ever use in the Zend

Zend_Debug::dump() is a really handy part of the framework that simply dumps the contents of whatever you give it to print_r and wraps it in <pre></pre>.

I got tired of typing it out every time, so I added the following shorthand functions to my bootstrap file. (The only acceptable use of a function in a bootstrap, in my mind.)

  1.  
  2. require_once("Zend/Debug.php"); // next time we’re going to talk about __autoload()
  3.  
  4. /**
  5. * dd is a shorthand function for "dump and die". It takes whatever you give it and
  6. * spits it to d(), then dies.  Useful for debugging.
  7. */
  8.  
  9. function dd($val){
  10. d($val);
  11. die();
  12. }
  13.  
  14. /**
  15. * This is so I don’t have to keep typing Zend_Debug::dump($whatever);
  16. */
  17. function d($val){
  18. Zend_Debug::dump($val);
  19. }

7 Responses to “The most useful function you will ever use in the Zend Framework”

  1. On October 13th, 2007 at 4:42 pm Matt Gardner said:

    Nice man. I’ve taken this a little further by allowing a ‘developer’ flag to ensure any calls that make it to production are at least hidden. Next level would be to post a warning instead so any that remain will be logged.

    if ($config->developer == true){
    $front->throwExceptions(true);
    function d($val) { Zend_Debug::dump($val); }
    function dd($val){ d($val); die(); }
    } else {
    function d($val) { return; }
    function dd($val){ return; }
    }

    Thanks, Matt


  2. On January 3rd, 2008 at 12:06 pm Jeremy Kendall said:

    Nice. I must type Zend_Debug… at least 10 times a day. This is one of those things that you see and think, “Why didn’t I think of that?”

    I make heavy use of the $label param myself, so I modded the functions to include both $label and $echo params, ie:

    d($val, $label=”", $echo=true);

    Works like a charm.


  3. On June 8th, 2010 at 11:49 am Joe Devon said:

    Nice, I’ve done something similar in the past…

    Now I have a debugger & a logger function…it uses firephp to dump vars, arrays and database calls to firebug so I can have the page display as normal, but the debug info is visible.

    A bonus is that it only runs in dev so I can leave the debugger statement in (for example, when I use caching, I always spit out to debugger if it’s a cache hit or miss…) no need to remove that line… and if you want to see this debug data in production you just log it and no one has to know you’re debugging.


  4. On June 23rd, 2010 at 3:32 am Jahufar said:

    Setup Zend Studio + xDebug

    Set a breakpoint where you want and inspect the variables at runtime.

    Way more efficient.

    Just my 2 cents.


  5. On July 14th, 2010 at 2:38 pm Jaybill McCarthy said:

    @Jahufar – Define “efficient”. :) It takes a day and a half to get xdebug working.


  6. On July 23rd, 2010 at 2:40 pm Chris said:

    There’s a handy tool on the xdebug site now that takes just about all of the guesswork out of installing it: http://xdebug.com/find-binary.php


  7. On July 23rd, 2010 at 3:30 pm Jaybill McCarthy said:

    @chris thanks! I will totally check that out!