Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function name($param, $line = __LINE__, $file = __FILE__) {};

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?

like image 891
Hailwood Avatar asked Aug 02 '10 11:08

Hailwood


2 Answers

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();
like image 196
Tom Haigh Avatar answered Nov 06 '22 23:11

Tom Haigh


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.

like image 41
Wrikken Avatar answered Nov 06 '22 22:11

Wrikken