Saturday, January 12, 2013

Debug() function for Opencart

Have you wondered if a single function call would be sufficient to output the array in pre-formatted way. I have been working with opencart for some months and I realize how repititive is it to repeat same lines of code to make array look nice. By the way, I am a big fan of CakePHP framework and its powerful debugging technique.

<?php
   echo '<pre>';
   print_r($array);
   echo '</pre>';
?>
Whenever I need to echo any array, I needed to repeat the ‘<pre>’ so that I get readable array.
So, I thought of making a global function debug(), so that it takes array as argument and prints the array in a nice format.
For this, there is a need for making a global function, so that we can access this function whenever we require, like
<?php $this->debug($array); ?>

The debug function was added in the abstract class Controller after render() function at system/engine/controller.php and if you need it for model , system/engine/model.php.

  /*
   * @author Ashok Basnet
   * 2013-1-13
   * Debug function for opencart
   * @params array
   * @return true
   */
  public function debug($array){
    // show calling url : Line line_number
    $bt = debug_backtrace();
    $caller = array_shift($bt);
    echo '<b>'.$caller['file'].' : Line '.$caller['line'].'</b>';
    
    echo '<pre>';
    print_r($array);
    echo '</pre>';
    return true;
  }

 

I don’t think the core modification is a good idea, but if it can make your life easier, then why not… 
Smile Happy Coding Smile

0 comments:

Post a Comment