Get Code Line and File That's Executing the Current Function in PHP

Get code line and file that's executing the current function in PHP?

You can use debug_backtrace().

http://us3.php.net/manual/en/function.debug-backtrace.php

So, in your log function, you would be able to retrieve the filename and line number from which the log function was called.

I'm using this approach in my logging classes and it has significantly reduced the amount of code required to get meaningful log data. Another benefit would be readability. Magic constants tend to get quite ugly when mixed with strings.

Here's a quick example:

function log($msg)
{
$bt = debug_backtrace();
$caller = array_shift($bt);

// echo $caller['file'];
// echo $caller['line'];

// do your logging stuff here.
}

How to use _LINE_ in PHP to display current line of execution

You have to use :

echo __LINE__; 

with two underscores.

Get the current script file name

Just use the PHP magic constant __FILE__ to get the current filename.

But it seems you want the part without .php. So...

basename(__FILE__, '.php'); 

A more generic file extension remover would look like this...

function chopExtension($filename) {
return pathinfo($filename, PATHINFO_FILENAME);
}

var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"

Using standard string library functions is much quicker, as you'd expect.

function chopExtension($filename) {
return substr($filename, 0, strrpos($filename, '.'));
}

Determine What Line a Function Was Executed From

I suppose a solution could be to use debug_backtrace.

The given example gets a backtrace like this :

array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}

So, should include what you want ;-)


And if you just want to output the trace (not likely), there is also debug_print_backtrace.

Finding out the filename that called my function in PHP

A solution might be to use the debug_backtrace function : in the backtrace, that kind of information should be present.

Or, as Gordon pointed out in a comment, you can also use debug_print_backtrace if you just want to output that information and not work with it.


For instance, with temp.php containing this :

<?php
include 'temp-2.php';
my_function();

and with temp-2.php containing this :

<?php
function my_function() {
var_dump(debug_backtrace());
}


Calling temp.php (i.e. the first script) from my browser gets me this output :

array
0 =>
array
'file' => string '/.../temp/temp.php' (length=46)
'line' => int 5
'function' => string 'my_function' (length=11)
'args' =>
array
empty

In there, I have the "temp.php" filename -- which is the one in which the function has been called.


Of course, you'll have to test a bit more (especially in situations where the function is not in the "first level" included file, but in a file included by another one -- not sure debug_backtrace will help much, there...) ; but this might help you get a first idea...

Function to get currently executing directory name of executing file

To get the directory of the file I use the dirname function like this :

$dir = dirname(__FILE__);

PHP - pass line number automatically as function argument

You can use debug_backtrace inside of Logger::Log to retrieve a call stack, which includes the file and line number of the code that called Logger::Log. That's a sensible thing to include in loggers in general.

How to find line number and file name of error

Your custom error function can capture the file and line as arguments:

function customError($errno, $errstr, $errfile, $errline) {
$e=$errno . ",". $errstr . "," . $errfile . "," . $errline;
...
}

From the Manual:

A callback with the following siganture. NULL may be passed instead, to reset this handler to its default state.

bool handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )



Related Topics



Leave a reply



Submit