Solved

File, and Line of function call.

Posted on 2003-11-13
4
259 Views
Last Modified: 2008-02-26
Hi

When I define a function is there any of getting the Line, number and file of the script that called the function? I want to do this to improve error handling in my PHP functions.

Lets say for example we have the following without the line numbers:

###############################
1. <?php
2.
3. my_function();
4.
5. my_function();
6.
7. function my_function(){
8. // have something here that returns 3 for it being called on Line 3, and 5 for it being called on line 5.
9. }
###############################

I know I could call the function as my_function(__LINE__, __FILE__), and define the function with

function my_function($line, $file)..., but I'd perfer not to have to change the function call syntax.

for an example of what I mean, run the following script.

##############################
<?php

error_reporting(E_ALL & ~E_NOTICE);

mysql_fetch_assoc("");

?>
##############################

I tried putting __LINE__, and __FILE__ into the function defination like

function my_function($line = __LINE__, $file = __FILE__), I tried triggering an error of type E_WARNING, but it simply output the filename, and line number of the line where trigger_error was called.

I also tried hacking the zend engine, and using ZE function get_compiled_lineno(or something like that), and get_compiled_filename(or something like that - can't remember exact name).

Any other suggestions?
0
Comment
Question by:aolXFT
4 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9742443
no way i can think of, definitely not using standard php code. i think you'd need to really edit something integral to php to get this to work. i'd just cut your losses and change the function, besides it'll be more portable then

loz
0
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9745744
It's not possible in normal PHP; maybe you could write some fancy PHP, but that would probably cost you a lot of time and code.

You can also adapt the function to also return error numbers and process those just after the function was called.
0
 
LVL 14

Accepted Solution

by:
ThG earned 150 total points
ID: 9749985
Yes, there is a way to do what you want, at least in PHP 4.3.x (I don't know about older versions, I don't support them).

Output is:
Function my_function() called by from file error_handler.php at line 14
Function my_function() called by from file error_handler.php at line 16

<?php
// error_handler.php - ThG at Experts-Exchange.com - 20031114
// http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20797344.html

function my_function() {
  debug_backtrace();

  $bt = debug_backtrace();
  $xbt = $bt[0];
  print "Function my_function() called by from file " . basename($xbt['file']) .
        " at line " . $xbt['line'] . "\n";
}

my_function();

my_function();
0
 
LVL 6

Author Comment

by:aolXFT
ID: 9750134
Portability isn't really an issue. This is for debugging on my own machine.

debug_backtrace() seems just what I'm looking for. I can't believe I couldn't find it in the manual. I suppose it might be because I don't have the latest version of the manual.

I think I have the latest php version. If not > 4.3, then I'll just upgrade.

Thanks ThG
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now