Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File, and Line of function call.

Posted on 2003-11-13
4
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 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