Solved

File, and Line of function call.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

920 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

13 Experts available now in Live!

Get 1:1 Help Now