Source for file Debug.php
Documentation is available at Debug.php
* Factory class for renderer of Debug class
* @see Debug/Renderer/*.php
require_once 'Debug/Renderer.php';
* Possible version of class Debug
define('PHP_DEBUG_VERSION_STANDALONE', 0);
define('PHP_DEBUG_VERSION_PEAR', 1);
define('PHP_DEBUG_VERSION_DEFAULT', PHP_DEBUG_VERSION_STANDALONE);
define('PHP_DEBUG_VERSION', PHP_DEBUG_VERSION_STANDALONE);
define('PHP_DEBUG_RELEASE', 'V2.0.0');
* These are constant for dump() and DumpObj() functions.
* - PHP_DEBUG_DUMP_DISP : Tell the function to display the debug info.
* - PHP_DEBUG_DUMP_STR : Tell the fonction to return the debug info as a
* - PHP_DEBUG_DUMP_VARNAME : Default name of Array - DBG_ARR_OBJNAME : Default
define('PHP_DEBUG_DUMP_DISP', 1);
define('PHP_DEBUG_DUMP_STR', 2);
define('PHP_DEBUG_DUMP_VARNAME', 'Variable');
* These are constants to define Super array environment variables
define('PHP_DEBUG_GLOBAL_GET', 0);
define('PHP_DEBUG_GLOBAL_POST', 1);
define('PHP_DEBUG_GLOBAL_FILES', 2);
define('PHP_DEBUG_GLOBAL_COOKIE', 3);
define('PHP_DEBUG_GLOBAL_REQUEST', 4);
define('PHP_DEBUG_GLOBAL_SESSION', 5);
define('PHP_DEBUG_GLOBAL_GLOBALS', 6);
* These are constant for addDebug functions, they set the behaviour where
* the function should add the debug information in first or in last position
define('PHP_DEBUG_POSITIONLAST', 0);
define('PHP_DEBUG_POSITIONFIRST', 1);
* Default configuration options
* @since V2.0.0 - 16 apr 2006
'DEBUG_render_mode' => 'HTML_Table', // Render mode
'DEBUG_restrict_access' => true, // Restrict or not the access
'DEBUG_allowed_ip' => array('127.0.0.1'),// Authorized IP to view the debug when restrcit_access is true
'DEBUG_allow_url_access' => false, // Allow to access the debug with a special parameter in the url
'DEBUG_url_key' => 'debug', // Key for url instant access
'DEBUG_url_pass' => 'true', // Password for url instant access
'DEBUG_enable_watch' => false, // Enable the watch function
'DEBUG_replace_errorhandler' => true, // Replace or no the PHP errorhandler
'DEBUG_lang' => 'EN' // Language
* Default static options for static functions
* @since V2.0.0 - 16 apr 2006
static $staticOptions = array(
'DEBUG_dump_method' => 'print_r', // print_r or var_dump
'DEBUG_pear_var_dump_method' => 'Var_Dump::display' // Var_Dump display funtion
* Functions from this class that must be excluded in order to have the
* correct backtrace information
* @see Debug_Line::setTraceback()
* @since V2.0.0 - 13 apr 2006
static $excludedBackTraceFunctions = array(
* Correspondance between super array constant and variable name
* @since V2.0.0 - 18 apr 2006
static $globalEnvConstantsCorresp = array(
PHP_DEBUG_GLOBAL_GET => '_GET',
PHP_DEBUG_GLOBAL_POST => '_POST',
PHP_DEBUG_GLOBAL_FILES => '_FILES',
PHP_DEBUG_GLOBAL_COOKIE => '_COOKIE',
PHP_DEBUG_GLOBAL_REQUEST=> '_REQUEST',
PHP_DEBUG_GLOBAL_SESSION=> '_SESSION',
PHP_DEBUG_GLOBAL_GLOBALS=> 'GLOBALS'
* Default configuration options
* @since V2.0.0 - 13 apr 2006
* This is the array where the debug lines are collected.
* @since V2.0.0 - 11 apr 2006
* This is the array containing all the required/included files of the
* @since V2.0.0 - 17 apr 2006
* @see render(), PHP_DEBUGLINE_TEMPLATES
* This is the array containing all the watched variables
* @since V2.0.0 - 16 apr 2006
* @since V2.0.0 - 11 apr 2006
* @since V2.0.0 - 11 apr 2006
* Number of queries executed during script
* @since V2.0.0 - 19 apr 2006
* PHP_Debug class constructor
* - the execution start time
* - the error and watch call back functions
* @param array $options Array containing options to affect to Debug
* @since V2.0.0 - 11 apr 2006
* Add a debug information
* @param string $info The main debug information
* (may be empty for some debug line types)
* @param integer $type Type of the Debug_Line
public function addDebug($info, $type = PHP_DEBUGLINE_STD, $position = PHP_DEBUG_POSITIONLAST)
// Additional process for some types
* Add a debug info before all the existing other debug lines
* It is an alias for addDebug($info, PHP_DEBUG_POSITIONLAST)
* This is an alias for the addDebug function
* @since V2.0.0 - 20 apr 2006
public function add($info, $type = PHP_DEBUGLINE_STD)
* This is an alias for the addDebug function when wanting to add a query
* @see addDebug(), PHP_DEBUGLINE_QUERY
* @since V2.0.0 - 21 Apr 2006
public function query($qry)
* This is an alias for the addDebug function when wanting to add an
* @see addDebug(), PHP_DEBUGLINE_APPERROR
* @since V2.0.0 - 21 Apr 2006
public function error($info)
* Set the callback fucntion to process the watches, enabled depending of
* the options flag 'DEBUG_enable_watch'
* @since V2.0.0 - 16 apr 2006
* @see options, watches, watchesCallback()
if ($this->options['DEBUG_enable_watch'] == true) {
$watchMethod = array($this, 'watchesCallback');
* Set the callback function to process replace the php error handler,
* enabled depending of the options flag 'DEBUG_replace_errorhandler'
* @since V2.0.0 - 16 apr 2006
* @see options, errorHandlerCallback()
if ($this->options['DEBUG_replace_errorhandler'] == true) {
* Callback function for php error handling
* Warning : the only PHP error codes that are processed by this user
* handler are : E_WARNING, E_NOTICE, E_USER_ERROR
* For the other error codes the standart php handler will be used
* @since V2.0.0 - 17 apr 2006
* @see options, setErrorHandler()
// We already have line & file with setBackTrace function
for ($index = 0; $index < $popNumber; $index++ ) {
if ($details[0] != E_STRICT)
* Add a variable to the watchlist. Watched variables must be in a declare
* (ticks=n) block so that every n ticks the watched variables are checked
* for changes. If any changes were made, the new value of the variable is
* @param string $variableName Variable to watch
* @since V2.0.0 - 17 apr 2006
public function watch($variableName)
if ($this->options['DEBUG_enable_watch'] == true) {
if (isset ($GLOBALS[$variableName])) {
$this->watches[$variableName] = $GLOBALS[$variableName];
$this->watches[$variableName] = null;
print ('<br />The <b>Watch()</b> function is disabled please set the option "DEBUG_enable_watch" to "true" to be able to use this feature<br />');
* Watch callback function, process watches and add changes to the debug
* @since V2.0.0 - 17 apr 2006
// Check if there are variables to watch
foreach ($this->watches as $variableName => $variableValue) {
if ($GLOBALS[$variableName] !== $this->watches[$variableName]) {
$this->watches[$variableName] = $GLOBALS[$variableName];
* Get global process time
* @return float Execution process time of the script
* @since V2.0.0 - 21 Apr 2006
* Get database related process time
* @return float Execection process time of the script for all
* database specific tasks
* @see PHP_DEBUGLINE_QUERY, PHP_DEBUGLINE_QUERYREL
* @since V2.0.0 - 21 Apr 2006
$properties = $lvalue->getProperties();
if (!empty($properties['endTime'])) {
$queryTime = $queryTime + $this->getElapsedTime($properties['startTime'], $properties['endTime']);
* PHP_Debug default output function, first we finish the processes and
* then a render object is created and its render method is invoked
* The renderer used is set with the options, all the possible renderer
* are in the directory Debug/Renderer/*.php
* (not the files ending by '_Config.php')
* @since V2.0.0 - 13 apr 2006
// Render output if we are allowed to
// Create render object and invoke its render function
// Get required files here to have event all Debug classes
* Alias for the render function
* @since V2.0.0 - 17 apr 2006
* @since V2.0.1 - 17 apr 2006
* Restrict access to a list of IP
* @param array $ip Array with IP to allow access
* @see $options, isAllowed()
$this->options['DEBUG_allowed_ip'] = $ip;
* Test if the client is allowed to access the debug information
* There are several possibilities :
* - 'DEBUG_restrict_access' flag is set to false
* - 'DEBUG_restrict_access' flag is set to true and client IP is the
* allowed ip in the options 'DEBUG_allowed_ip'
* - Access by url is allowed with flag 'DEBUG_allow_url_access' then
* the client must enter the good key and password in the url
* @since V2.0.0 - 20 apr 2006
* @see $options, restrictAcess()
if ($this->options['DEBUG_restrict_access'] == true) {
// Check if client IP is among the allowed ones
if (in_array($_SERVER['REMOTE_ADDR'], $this->options['DEBUG_allowed_ip'])) {
// Check if instant access is allowed and test key and password
elseif ($this->options['DEBUG_allow_url_access'] == true) {
$key = $this->options['DEBUG_url_key'];
if (!empty($_GET[$key])) {
if ($_GET[$key] == $this->options['DEBUG_url_pass']) {
// Access is not restricted
* Return microtime from a timestamp
* @param $time Timestamp to retrieve micro time
* @return numeric Microtime of timestamp param
* @since V1.1.0 - 14 Nov 2003
list ($usec, $sec) = explode(' ', $time);
return ((float) $usec + (float) $sec);
* Alias for getMicroTime(microtime()
* @since V2.0.0 - 19 apr 2006
* Get elapsed time between 2 timestamp
* @param float $timeStart Start time
* @param float $timeEnd End time
* @return float Numeric difference between the two times
* ref in format 00.0000 sec
return round($timeEnd - $timeStart, 4);
* Set the endtime for a Debug_Line in order to monitor the performance
* @see Debug_Line::endTime
* @since V2.0.0 - 19 apr 2006
* Display the content of any kind of variable
* - Mode PHP_DEBUG_DUMP_ARR_DISP display the array
* - Mode PHP_DEBUG_DUMP_ARR_STR return the infos as a string
* @param mixed $var Variable to dump
* @param string $varname Name of the variable
* @param integer $mode Mode of function
* @return mixed Nothing or string depending on the mode
* @todo I don't know if it is a good practice to have static properties
* for static functions, to check
* @since V2.0.0 - 25 Apr 2006
static function dumpVar($var, $varName = PHP_DEBUG_DUMP_VARNAME, $mode = PHP_DEBUG_DUMP_DISP)
$dumpMethod = self::$staticOptions['DEBUG_pear_var_dump_method'];
$dumpMethod = self::$staticOptions['DEBUG_dump_method'];
$dbgBuffer = "<pre><b>dump of '$varName'</b> :". CR. $dbgBuffer. '</pre>';
* This a method to dump the content of any variable and add the result in
* @param mixed $var Variable to dump
* @param string $varname Name of the variable
* @since V2.0.0 - 25 Apr 2006
public function dump($obj, $varName = STR_N)
* Set the main action of PHP script
* @param string $action Name of the main action of the file
* @since V2.0.0 - 25 Apr 2006
* @see PHP_DEBUGLINE_CURRENTFILE
* @param string $optionsIdx Name of the option to get
* @since V2.0.0 - 13 apr 2006
* Return the style sheet of the HTML_TABLE debug object
* @return string The stylesheet
return $this->options['HTML_TABLE_stylesheet'];
* Getter of requiredFiles property
* @return array Array with the included/required files
* @since V2.0.0 - 13 apr 2006
* Getter of debugString property
* @since V2.0.0 - 13 apr 2006
* Getter of queryCount property
* @since @since V2.0.0 - 21 Apr 2006
* Debug default output function, simply uses the static dump fonction
* @since V2.0.0 - 11 apr 2006
* @since V2.0.0 - 11 apr 2006
* - PHP_DEBUGLINE_ANY : All available types (for search mode)
* - PHP_DEBUGLINE_STD : Standart debug
* - PHP_DEBUGLINE_QUERY : Query debug
* - PHP_DEBUGLINE_REL : Database related debug
* - PHP_DEBUGLINE_ENV : Environment debug ($GLOBALS...)
* - PHP_DEBUGLINE_APPERROR : Custom application error
* - PHP_DEBUGLINE_CREDITS : Credits information
* - PHP_DEBUGLINE_SEARCH : Search mode in debug
* - PHP_DEBUGLINE_DUMP : Dump any kind of variable
* - PHP_DEBUGLINE_PROCESSPERF : Performance analysys
* - PHP_DEBUGLINE_TEMPLATES : Included templates of the calling script
* - PHP_DEBUGLINE_PAGEACTION : Store main page action
* - PHP_DEBUGLINE_SQLPARSE : SQL Parse error
* - PHP_DEBUGLINE_WATCH : A variable to watch
* - PHP_DEBUGLINE_PHPERROR : A debug generated by the custom error handler
* @todo Currentfile is deprecated, numbers to resequence
define('PHP_DEBUGLINE_ANY', 0);
define('PHP_DEBUGLINE_STD', 1);
define('PHP_DEBUGLINE_QUERY', 2);
define('PHP_DEBUGLINE_QUERYREL', 3);
define('PHP_DEBUGLINE_ENV', 4);
define('PHP_DEBUGLINE_APPERROR', 5);
define('PHP_DEBUGLINE_CREDITS', 6);
define('PHP_DEBUGLINE_SEARCH', 7);
define('PHP_DEBUGLINE_DUMP', 8);
define('PHP_DEBUGLINE_PROCESSPERF', 9);
define('PHP_DEBUGLINE_TEMPLATES', 10);
define('PHP_DEBUGLINE_PAGEACTION', 11);
define('PHP_DEBUGLINE_SQLPARSE', 12);
define('PHP_DEBUGLINE_WATCH', 13);
define('PHP_DEBUGLINE_PHPERROR', 14);
define('PHP_DEBUGLINE_DEFAULT', PHP_DEBUGLINE_STD);
* Labels for debugline types
static $debugLineLabels = array(
PHP_DEBUGLINE_ANY => 'ALL',
PHP_DEBUGLINE_STD => 'Standart',
PHP_DEBUGLINE_QUERY => 'Query',
PHP_DEBUGLINE_QUERYREL => 'Database related',
PHP_DEBUGLINE_ENV => 'Environment',
PHP_DEBUGLINE_APPERROR => 'Application error',
PHP_DEBUGLINE_CREDITS => 'Credits',
PHP_DEBUGLINE_SEARCH => 'Search',
PHP_DEBUGLINE_DUMP => 'Variable dump',
PHP_DEBUGLINE_PROCESSPERF => 'Performance analysis',
PHP_DEBUGLINE_TEMPLATES => 'Included files',
PHP_DEBUGLINE_PAGEACTION => 'Page main action',
PHP_DEBUGLINE_SQLPARSE => 'SQL parse error',
PHP_DEBUGLINE_WATCH => 'Watch',
PHP_DEBUGLINE_PHPERROR => 'PHP error'
* Properties that stores the non formatted debug information
* @since V2.0.0 - 11 apr 2006
* Type of the debug information
* @since V2.0.0 - 11 apr 2006
* @see Debug_Line constants
* @since V2.0.0 - 11 apr 2006
* @since V2.0.0 - 11 apr 2006
* Class from witch the debug was called
* @since V2.0.0 - 13 apr 2006
* Function from wich the debug was called
* @since V2.0.0 - 11 apr 2006
* Exection time for debug info
* @since V2.0.0 - 16 apr 2006
* Exection end time for debug info
* @see Debug::stopTimer(), setEndTime()
* @since V2.0.0 - 16 apr 2006
* PHP_DebugLine class constructor
* - the start time of the debug info
* - the traceback information
* @since V2.0.0 - 11 apr 2006
function __construct($info, $type = PHP_DEBUGLINE_DEFAULT)
* Fills properties of debug line with backtrace informations
* @since @since V2.0.0 - 15 apr 2006
// Get max id of 'add' debug functions
foreach($callStack as $lkey => $lvalue) {
if (in_array($callStack[$lkey]['function'], Debug::$excludedBackTraceFunctions) == true) {
$this->file = !empty($callStack[$idx] ['file']) ? $callStack[$idx]['file'] : '';
$this->line = !empty($callStack[$idx] ['line']) ? $callStack[$idx]['line'] : '';
$this->function = !empty($callStack[$idx+ 1]['function']) ? $callStack[$idx+ 1]['function'] : '';
$this->class = !empty($callStack[$idx+ 1]['class']) ? $callStack[$idx+ 1]['class'] : '';
* Getter of all properties of Debug_Line object
* @return array Array containg all the properties of the debugline
* @since V2.0.0 - 21 apr 2006
* @since V2.0.0 - 19 apr 2006
* Debug_Line default output function
* @since V2.0.0 - 11 apr 2006
return '<pre>'. Debug::dumpVar($this, __CLASS__ , PHP_DEBUG_DUMP_ARR_STR). '</pre>';
* Debug_Line class destructor
* @since V2.0.0 - 11 apr 2006
|