6

Is it possible to have a function automatically contain the line number and the file that the function was CALLED in,

as if i call __LINE__ or __FILE__ in the function it will use the line and file the function definition is in.

but i dont want to have to pass __LINE__ and __FILE__ into the function every time.

so if i set them as the default params, do they come from the function definition, or where it is being called from?

Hailwood
  • 89,623
  • 107
  • 270
  • 423

4 Answers4

6

Doing what you suggest doesn't seem to work.

You can do it like this, but I'm not sure why you want to do it and that there isn't a better approach to what you are trying to achieve - see Wrikken's answer.

<?php

function test() {
    $backtrace = debug_backtrace();
    $last = $backtrace[0];
    echo "{$last['function']}() called from {$last['file']} line {$last['line']}\r\n"; 
}



test();
Community
  • 1
  • 1
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
5

The only way would be using debug_backtrace(), but as the name says: it is for debugging. Your code should not attach any meaning or functionality in production based on where/when it's called.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • may i ask why this is so bad? – Hailwood Aug 02 '10 at 12:07
  • 1
    (1) debug_backtrace has quite a lot of overhead (2) your functions behavior gets unpredictable for other developers (a decorator or just perhaps proper private/protected/public methods, depending on circumstances, would be far more recognizable solutions) (3) the function breaks out of its self containment / independence, which is about the surest sign there is something wrong with the design. – Wrikken Aug 02 '10 at 12:19
  • 1
    @Hailwood in production environment all inner nitty-gritty details should be logged, not displayed to user. that's what Wrikken about. And for debugging purposes there is a function called `trigger_error` which will luckily add file and line automatically, outputting error message according to current error handling settings - to display or a log file. very handy – Your Common Sense Aug 02 '10 at 12:23
0

It's so late but maybe can be useful, you can use get_called_class(), for the name of class who is called, and do not pass like a parameter insted of CLASS.

-1

if you want to use this information in some kind of error message, there is a function trigger_error() which will raise PHP native error, so, therefore, it will be shown in usual PHP manner - with filename, line number and supplied text.

Most neat feature of this function is behaving according to current error handling settings:

ini_set('display_errors',1);
trigger_error("Horrible bug found!");

will print out directly to screen an error message like this:

Notice: Horrible bug found! in /path/file.php on line 2

very handy for developing
while this code

ini_set('display_errors',0);
ini_set('log_errors',1);
trigger_error("Horrible bug found!");

will be put into error log for the future reference
obligatory for the production

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345