Solved

File, and Line of function call.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 look for a specific file type in a local or remote server directory using PHP.

813 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

17 Experts available now in Live!

Get 1:1 Help Now