Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Using PEAR::PHP_Debug

Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Smarty Pro

Joined: 16 May 2007
Posts: 124
Location: Italy

PostPosted: Sat May 19, 2007 4:52 pm    Post subject: Using PEAR::PHP_Debug Reply with quote


PHP_Debug is a handy PEAR library for debugging business layer and process flow in PHP. The PEAR::PHP_Debug page can be found at and the external package homepage, complete with examples, is

It comes with a nifty floating div bar - for in page information about what happened during the execution cycle and can give you access to variables, etc and I wanted to include the Smarty vars in the output - rather than use the popup smarty debug console.

To use PHP_Debug in your application:

1) Install the PEAR::PHP_Debug lib with pear install PHP_Debug. It's currently in beta - so you'll need to set your PEAR config to install betas if you haven't already - you can do this with:

pear config-set preferred_state beta

to install PHP_Debug

pear install PHP_Debug

2) Follow the instructions on the PHP_Debug website to copy the templates, scripts and images to the correct place on your site.

3) Add the following code to your extended Smarty class:


   var $_PHPDebugHandler = null;
   function &PHPDebugObj(&$obj=null) {
      if (empty($obj)) {
         return $obj;
      } else {
         // Not empty so set it
         $this->_PHPDebugHandler =& $obj;
   // Assigns the Smarty debug vars (discovered by looking at debug.tpl and then function.assign_debug_info.php)
   // to the PHPDebug object
   // You'll find the information under logs and messages on the PHP_Debug div bar
   function PHPDebugAssignSmartyVars() {
      if (!empty($this->_PHPDebugHandler)) {
         $this->_PHPDebugHandler->dump($this->_tpl_vars,"Smarty Assigned Vars");
         if (@is_array($smarty->_config[0])) {
            $config_vars = $smarty->_config[0];
            $this->_PHPDebugHandler->dump(array_keys($config_vars),"Smarty Config Keys");
            $this->_PHPDebugHandler->dump(array_values($config_vars),"Smarty Config Vals");
         $included_templates = $this->_smarty_debug_info;
         $this->_PHPDebugHandler->dump(array_keys($assigned_vars),"Smarty Debug Keys");
         $this->_PHPDebugHandler->dump(array_values($assigned_vars),"Smarty Debug Vals");         
         $this->_PHPDebugHandler->dump($included_templates,"Smarty Debug Tpls");         
   function display($resource_name, $cache_id = null, $compile_id = null) {

This does the guts of letting PHP_Debug know about the Smarty Debug vars.

4) You'll need to define the config array for PHP_Debug somewhere where you defined your constants or global config info. The config array I use is - but you'll need to change the paths to suit your environment:

$phpDebugOptions =
      'render_type'          => 'HTML',    // Renderer type
      'render_mode'          => 'Div',     // Renderer mode
      'restrict_access'      => false,     // Restrict access of debug
      'allow_url_access'     => true,      // Allow url access
      'url_key'              => 'key',     // Url key
      'url_pass'             => 'nounou',  // Url pass
      'enable_watch'         => true,     // Enable wath of vars
      'replace_errorhandler' => true,      // Replace the php error handler
      'enable_w3c_validator' => false,   // Validate the output
      // Renderer specific
      'HTML_DIV_view_source_script_name' => 'PHP_Debug_ShowSource.php',
      'HTML_DIV_remove_templates_pattern' => false,
      'HTML_DIV_images_path' => 'templates/default/css/images',
      'HTML_DIV_css_path' => 'templates/default/css',
      'HTML_DIV_js_path' => 'javascripts',

5) We're almost done - stick with me! Smile Now we need to include the PHP_Debug library and create an instance of the PHP_Debug class - (my global smarty var is called $thisPage):

$Dbg = new PHP_Debug($phpDebugOptions);

I include the above in my include/common.php.

6) Now you just need to modify your template for the extra bits needed by PHP_Debug. We need an entry in the header template within the <HEAD></HEAD> tags:

      <script type="text/javascript" src="{$phpDebugOptions.HTML_DIV_js_path}/html_div.js"></script>
      <link rel="stylesheet" type="text/css" media="screen" href="{$phpDebugOptions.HTML_DIV_css_path}/html_div.css" />

That's it. If all is well - the next page you load will have the debug panel in the top right of the browser page.

Notice that in stage 5 we assigned the config array to Smarty using $thisPage->assign - this allows smarty to pass the info to PHP_Debug and will ensure your paths are set correctly when the header template is parsed.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP