Listing 4-3 (Continued) } function print_banner() { if ($this->banner_printed == TRUE) { return 0; } $out = “<br><br><font color=’$this->myTextColor’>” . “<strong>Debugger started for $this->prefix</strong>” . “</font><br><hr>”; if ($this->buffer == TRUE ){ $this->buffer_str .= $out; } else { echo $out; $this->banner_printed = TRUE; } return 1; } function write($msg) { $out = sprintf(“<font color=’%s’>%03d </font>” . “<font color=%s>%s</font><br>\n”, $this->myTextColor, $this->line++, $this->color, $msg); if ($this->buffer == TRUE) { $this->buffer_str .= $out; } else { echo $out; } } 86 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 86 function debug_array($hash = null) { while(list($k, $v) = each($hash)) { $this->write(“$k = $v”); } } function set_buffer() { $this->buffer = TRUE; } function reset_buffer() { $this->buffer = FALSE; $this->buffer_str = null; } function flush_buffer() { $this->buffer = FALSE; $this->print_banner(); echo $this->buffer_str; } } ?> The debugger class has the following methods: ◆ Debugger(): This is the constructor function for the debugger class (class.Debugger.php). This function initializes the color, prefix, line, and buffer_str, banner_printed member variables. The color is used to display the debug information in a given color. The prefix variable is used to prefix each debug message displayed, which allows for easier identifi- cation of messages. The line variable is initialized to zero, which is automatically incremented to help locate debug information quickly. The buffer_str variable is used to store buffered debug information. The banner_printed variable, which Chapter 4: Architecture of an Intranet Application 87 07 549669 ch04.qxd 4/4/03 9:24 AM Page 87 controls the banner printing, is set to FALSE. The debugger can be invoked in an application called test_debugger1.php as follows: <?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed framewirk directory in // a different directory than // %DocumentRoot%/framework, change the setting below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set( ‘include_path’, ‘:’ . $APP_FRAMEWORK_DIR . ‘:’ . ini_get(‘include_path’)); // Now load our Debugger class from application framework require_once(‘class.Debugger.php’); $myDebugger = new Debugger(array( ‘color’ => ‘red’, ‘prefix’ => ‘MAIN’, ‘buffer’ => FALSE) ); // Define an array of fruits $fruits = array(‘apple’, ‘orange’, ‘banana’); // Show the array contents $myDebugger->debug_array($fruits); ?> In this example, a new Debugger object called $myDebugger is created, which will print debug messages in red color and use ‘MAIN’ as the prefix for each message. The buffering of debug messages is disabled as well. ◆ print_banner(): This function prints a banner message as follows: Debugger started for PREFIX The PREFIX is set when the object is created. 88 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 88 ◆ write(): This function displays a debug message using the chosen color and automatically prints the debug message line number. If debug buffer- ing is on, then the message is written to the buffer (buffer_str). ◆ debug_array(): This function allows you to debug an associative array. It prints the contents of the associative array parameter using the write() method. ◆ set_buffer(): This function sets the buffering of debug messages. ◆ reset_buffer(): This function resets the buffering of debug messages. ◆ flush_buffer(): This function prints the buffer content along with the debug banner. Now let’s look at how an application called test_debugger2.php can use this debugging facility: <?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed framewirk directory in // a different directory than // %DocumentRoot%/framework, change the setting below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set( ‘include_path’, ‘:’ . $APP_FRAMEWORK_DIR . ‘:’ . ini_get(‘include_path’)); // Now load our Debugger class from application framework require_once(‘class.Debugger.php’); // Create a variable $name = ‘M. J. Kabir’; $myDebugger = new Debugger(array( ‘color’ => ‘blue’, ‘prefix’ => ‘MAIN’, ‘buffer’ => 0) ); Chapter 4: Architecture of an Intranet Application 89 07 549669 ch04.qxd 4/4/03 9:24 AM Page 89 // Write the variable out using debugger write() method $myDebugger->write(“Name = $name”); ?> This will print a message such as the following: <font color=’red’>000 </font> <font color=blue>Name = M. J. Kabir</font><br> Buffering debug messages enables you to print all debug messages together, which is often very beneficial in identifying a flow sequence. For example, here an application called test_debugger3.php buffers debugging information and prints the information when the buffer is flushed using flush_buffer() method found in the Debugger class. <?php // Turn on all error reporting error_reporting(E_ALL); // If you have installed framewirk directory in // a different directory than // %DocumentRoot%/framework, change the setting below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set( ‘include_path’, ‘:’ . $APP_FRAMEWORK_DIR . ‘:’ . ini_get(‘include_path’)); // Now load our Debugger class from application framework require_once(‘class.Debugger.php’); // Create a variable $name = ‘M. J. Kabir’; $email = ‘kabir@evoknow.com’; $myDebugger = new Debugger(array( ‘color’ => ‘blue’, ‘prefix’ => ‘MAIN’, ‘buffer’ => TRUE) ); $myDebugger->write(“Name = $name”); $myDebugger->write(“Email = $email”); echo “This will print before debug messages.\n\n”; $myDebugger->flush_buffer(); ?> 90 Part II: Developing Intranet Solutions 07 549669 ch04.qxd 4/4/03 9:24 AM Page 90 . below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set(. below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set(. below. $APP_FRAMEWORK_DIR=$_SERVER[‘DOCUMENT_ROOT’] . ‘/framework’; // Insert the path in the PHP include_path so that PHP // looks for our PEAR, PHPLIB and application framework // classes in these directories ini_set(