note 99752 added to function.debug-backtrace
| From: | phpnoob at osu1 dot php dot net | Date: | Fri, 03 Sep 2010 03:32:48 +0000 |
| Subject: | note 99752 added to function.debug-backtrace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172288@lists.php.net to get a copy of this message | ||
Surprisingly, no one has described one of the best uses of this: dumping a variable and showing the
location. When debugging, especially a big and unfamiliar system, it's a pain remembering where
I added those var dumps. Also, this way there is a separator between multiple dump calls.
<?php
function dump( $var ) {
$result = var_export( $var, true );
$loc = whereCalled();
return "\n<pre>Dump: $loc\n$result</pre>";
}
function whereCalled( $level = 1 ) {
$trace = debug_backtrace();
$file = $trace[$level]['file'];
$line = $trace[$level]['line'];
$object = $trace[$level]['object'];
if (is_object($object)) { $object = get_class($object); }
return "Where called: line $line of $object \n(in $file)";
}
?>
In addition, calling 'whereCalled()' from any function will quickly identify locations
that are doing something unexpected (e.g., updating a property at the wrong time). I'm new to
PHP, but have used the equivalent in Perl for years.
----
Server IP: 69.147.83.197
Probable Submitter: 201.206.250.26
----
Manual Page -- http://www.php.net/manual/en/function.debug-backtrace.php
Edit -- https://master.php.net/note/edit/99752
Del: integrated -- https://master.php.net/note/delete/99752/integrated
Del: useless -- https://master.php.net/note/delete/99752/useless
Del: bad code -- https://master.php.net/note/delete/99752/bad+code
Del: spam -- https://master.php.net/note/delete/99752/spam
Del: non-english -- https://master.php.net/note/delete/99752/non-english
Del: in docs -- https://master.php.net/note/delete/99752/in+docs
Del: other reasons-- https://master.php.net/note/delete/99752
Reject -- https://master.php.net/note/reject/99752
Search -- https://master.php.net/manage/user-notes.php