Code snippets for symfony 1.x

Navigation

Debug an object or variable with additional informations

This function allows you to dump the value of a variable or an object.If available the function tells the file, line, function, class where it was called.

1st parameter

The object/variable to debug

2nd parameter

The name that you want to give to the debuged object (var by default)

3nd parameter

Allows you can stop the process or not. If the process is not stopped the debug message is available in the sf debug bar.

/**
   * Function that dumps an array or an object in the SF debug infos or
   * on s"\n"een/console
   *
   * @param $var mixed variable to dump
   * @param $die boolean Tells the function to stop the process or not
   *
   * @author Vernet Loic
   * @since  24 sept. 2006
   */
  public static function dump($var, $name = 'var', $die = false)
  {
      ob_start();
      print('<br/><pre>'. $name . ' :<br/>');
      print_r($var);
      print('</pre></br>');
      $buffer = ob_get_contents();
      ob_end_clean();
 
      $backtrace = debug_backtrace();
      $dieMsg  = '<pre><b>var dump by goTools:dump()</b>'. "\n";
      $dieMsg .= isset($backtrace[0]['file']) ?     '» file     : <b>'.
      $backtrace[0]['file'] .'</b>'. "\n" : '';
      $dieMsg .= isset($backtrace[0]['line']) ?     '» line     : <b>'.
      $backtrace[0]['line'] .'</b>'. "\n" : '';
      $dieMsg .= isset($backtrace[1]['class']) ?    '» class    : <b>'.
      $backtrace[1]['class'] .'</b>'. "\n" : '';
      $dieMsg .= isset($backtrace[1]['function']) ? '» function : <b>'.
      $backtrace[1]['function'] .'</b>'. "\n" : '';
      $dieMsg .= '</pre>';
 
      print($buffer);
 
      if ($die == true) {
          die($dieMsg);
      } else {
          print($dieMsg);
          sfWebDebug::getInstance()->logShortMessage($buffer);
      }
  }
 
by Loïc Vernet on 2007-12-20, tagged debug 

Comments on this snippet

gravatar icon
#1 JeremY B on 2007-12-26 at 11:37

Don't forget to define "CR" constant or replace it by "\n" or something else ;-)

gravatar icon
#2 Loïc Vernet on 2008-01-01 at 02:55

That's write, it was a class constant. I have replaced it.

gravatar icon
#3 Nicolas Chambrier on 2008-01-03 at 11:05

Note that installing xdebug (on your dev server) and call var_dump() has the same effect + nice colorization :)

gravatar icon
#4 Loïc Vernet on 2008-01-04 at 11:24

Note that installing windows on your computer could destroy it. + nice fireworks ;)