Solved

File, and Line of function call.

Posted on 2003-11-13
4
262 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

830 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