1 | <?php |
||
2 | |||
3 | namespace MVC; |
||
4 | |||
5 | class Exception extends \Exception |
||
6 | { |
||
7 | // Redefine the exception so message isn't optional |
||
8 | public function __construct($message, $code = 0, Exception $previous = null) |
||
9 | { |
||
10 | // some code |
||
11 | // make sure everything is assigned properly |
||
12 | parent::__construct($this->generateCallTrace(), $code, $previous); |
||
13 | } |
||
14 | |||
15 | // custom string representation of object |
||
16 | public function __toString() |
||
17 | { |
||
18 | return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
||
19 | } |
||
20 | |||
21 | public function generateCallTrace() |
||
22 | { |
||
23 | $e = new \Exception(); |
||
24 | $trace = explode("\n", $e->getTraceAsString()); |
||
25 | // reverse array to make steps line up chronologically |
||
26 | $trace = array_reverse($trace); |
||
27 | array_shift($trace); // remove {main} |
||
28 | array_pop($trace); // remove call to this method |
||
29 | $length = count($trace); |
||
30 | $result = []; |
||
31 | |||
32 | for ($i = 0; $i < $length; ++$i) { |
||
33 | $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering |
||
34 | } |
||
35 | |||
36 | return nl2br("\t" . implode("\n\t", $result) . "\n\n" . $this->debug_string_backtrace()); |
||
37 | } |
||
38 | |||
39 | public function debug_string_backtrace() |
||
40 | { |
||
41 | if (ob_get_level()) { |
||
42 | ob_get_clean(); |
||
43 | } |
||
44 | ob_start(); |
||
45 | |||
46 | echo "debug_print_backtrace:\n\n "; |
||
47 | debug_print_backtrace(); |
||
48 | echo "\n\n"; |
||
49 | if ($this->is_admin() || isset($_REQUEST['dbg'])) { |
||
0 ignored issues
–
show
|
|||
50 | echo "debug_backtrace (administrator only): \n\n "; |
||
51 | echo '<pre>'; |
||
52 | var_dump(debug_backtrace()); |
||
0 ignored issues
–
show
|
|||
53 | echo '</pre>'; |
||
54 | } |
||
55 | |||
56 | $trace = ob_get_contents(); |
||
57 | ob_end_clean(); |
||
58 | |||
59 | // Remove first item from backtrace as it's this function which |
||
60 | // is redundant. |
||
61 | //$trace = preg_replace('/^#0\s+' . __FUNCTION__ . "[^\n]*\n/", '', $trace, 1); |
||
62 | |||
63 | // Renumber backtrace items. |
||
64 | //$trace = preg_replace('/^#(\d+)/me', '\'#\' . ($1 - 1)', $trace); |
||
65 | |||
66 | return $trace; |
||
67 | } |
||
68 | |||
69 | public function is_admin() |
||
70 | { |
||
71 | return is_admin(); |
||
72 | } |
||
73 | } |
||
74 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: