DESPERATE FOR HELP Can someone please explain...

I am learning PHP PDO from a book and I have had to create a file error_handler.php
I understand half it up until the static Handler function sets the variable $backtrace = GetBackTrace(2); ( line 21 ).

It looks like it takes the (2) as an argument which maybe means its working with the 3rd entry of the debug_backtrace array?? eg debug_array(2)??? I’m not sure what that is… I was thinking that it could be the ErrorHandler::SetHandler function as and array but if it is I can’t work out anything further.

Could someone please please please explain what is happening in the Get Back Trace function, i’m going crazy looking at. Broken down for the ultra beginner would be much appreciated.

<?php class ErrorHandler { // Private constructor to prevent direct creation of object private function __construct() { } /* Set user error-handler method to ErrorHandler::Handler method */ public static function SetHandler($errTypes = ERROR_TYPES) { return set_error_handler(array ('ErrorHandler', 'Handler'), $errTypes); } // Error handler method public static function Handler($errNo, $errStr, $errFile, $errLine) { /* The first two elements of the backtrace array are irrelevant: - ErrorHandler.GetBacktrace - ErrorHandler.Handler */ $backtrace = ErrorHandler::GetBackTrace(2); // Error message to be displayed, logged, or mailed $error_message = "\nERRNO: $errNo\nTEXT: $errStr" . "\nLOCATION: $errFile, line " . "$errLine, at " . date('F j, Y, g:i a') . "\nShowing backtrace:\n$backtrace\n\n"; // Email the error details, in case SEND_ERROR_MAIL is true if (SEND_ERROR_MAIL == true) error_log($error_message, 1, ADMIN_ERROR_MAIL, "From: " . SENDMAIL_FROM . "\r\nTo: " . ADMIN_ERROR_MAIL); // Log the error, in case LOG_ERRORS is true if (LOG_ERRORS == true) error_log($error_message, 3, LOG_ERRORS_FILE); /* Warnings don't abort execution if IS_WARNING_FATAL is false E_NOTICE and E_USER_NOTICE errors don't abort execution */ if (($errNo == E_WARNING && IS_WARNING_FATAL == false) || ($errNo == E_NOTICE || $errNo == E_USER_NOTICE)) // If the error is nonfatal ... { // Show message only if DEBUGGING is true if (DEBUGGING == true) echo '
' . $error_message . '
'; } else // If error is fatal ... { // Show error message if (DEBUGGING == true) echo '
'. $error_message . '
'; else echo SITE_GENERIC_ERROR_MESSAGE; // Stop processing the request exit(); } } // Builds backtrace message public static function GetBackTrace($irrelevantFirstEntries) { $s = ''; $MAXSTRLEN = 64; $trace_array = debug_backtrace(); for ($i = 0; $i < $irrelevantFirstEntries; $i++) array_shift($trace_array); $tabs = sizeof($trace_array) - 1; foreach ($trace_array as $arr) { $tabs -= 1; if (isset ($arr['class'])) $s .= $arr['class'] . '.'; $args = array (); if (!empty ($arr['args'])) foreach ($arr['args']as $v) { if (is_null($v)) $args[] = 'null'; elseif (is_array($v)) $args[] = 'Array[' . sizeof($v) . ']'; elseif (is_object($v)) $args[] = 'Object: ' . get_class($v); elseif (is_bool($v)) $args[] = $v ? 'true' : 'false'; else { $v = (string)@$v; $str = htmlspecialchars(substr($v, 0, $MAXSTRLEN)); if (strlen($v) > $MAXSTRLEN) $str .= '...'; $args[] = '"' . $str . '"'; } } $s .= $arr['function'] . '(' . implode(', ', $args) . ')'; $line = (isset ($arr['line']) ? $arr['line']: 'unknown'); $file = (isset ($arr['file']) ? $arr['file']: 'unknown'); $s .= sprintf(' # line %4d, file: %s', $line, $file); $s .= "\n"; } return $s; } } ?>

First, this is a double post. Tsk! Tsk! Secondly, code should be placed inside of the PHP tags so we can copy it easier into our tools.

Now, you (2) is used to go thru the total entries starting at that 3rd entry.
( for ($i = 0; $i < $irrelevantFirstEntries; $i++) ) This is used in an array shift command to move the trace array to skip these first two entries. Then, the trace array contains all of the traces except the first two.
(They go away.)

So, think of this, if an array has the values 1,2,3,4,5 in it. This routine shifts it left twice and the results becomes something like this 3,4,5 … Not tested, just thought out…

There you have it, the trace array (“trace_array”) is just being pruned a little to remove the not needed items.

Hope that helps! Please do not double-post!

Hi thanks for getting back to me, i didn’t mean to double post, i saw the notes about highlighting the PHP code so i reposted and didn’t realise I couldn’t delete the old post. Anyway…
The part that i can’t understand is what info is in the [1] [2] and why it needs to be skipped, i echo’d it to the screen but i couldn’t interpret what came out as anything that was relevant and it definitely didn’t look like any of the examples of a back trace that i’ve read up about.

Read the other thread. This is confusing to answer to two threads. (Do NOT reply to this thread, use the other one, please) And, just use the tags in that other thread from now on. Thanks…

Sponsor our Newsletter | Privacy Policy | Terms of Service