Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the PHP magic constant __FILE__ work with Eclipse and PDT

Lately when i was debugging some PHP file with XDebug (under Eclipse on Ubuntu) i came across a strange behaviour:

print(__FILE__);

resulted in

"xdebug eval"

GEE!

So this magic constant seems not to work with this.

Anyone know a fix or a viable workaround? How to debug the debugger? (Hardcoding a path is a PITA!)

like image 974
geek-merlin Avatar asked Feb 07 '11 17:02

geek-merlin


People also ask

What is __ FILE __ in PHP?

__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.

What are the magic constants in PHP?

Magic constants are the predefined constants in PHP which get changed on the basis of their use. They start with double underscore (__) and ends with double underscore. They are similar to other predefined constants but as they change their values with the context, they are called magic constants.

What is __ LINE __ PHP?

Name. Description. __LINE__ The current line number of the file.


1 Answers

The output you get is not incorrect. __FILE__ is a special constant that gets evaluated at parser time. When the PHP script gets compiled, it would really read something like this:

// test.php
<?php
    "test.php";
?>

even though the script source was:

// test.php
<?php
    __FILE__;
?>

This means that after parsing, there is no such "constant" __FILE__ at all, as it has already been replaced.

This means that if you do in an IDE, through DBGp's eval command eval -- __FILE__ it can not give you the __FILE__ with any filename. Instead, it uses the filename for the current context which is xdebug eval or in later versions, xdebug://debug-eval.

In essence, it's the same as doing this:

php -r 'eval("__FILE__;");'

Which also outputs:

Command line code(1) : eval()'d code

Xdebug looks for this sort of format, and changes it to xdebug://debug-eval so that it can actually debug into eval'ed code.

__FILE__ works as expected in PHP source code, as can be proven with this snippet:

<?php $far = __FILE__; // now evaluate $far in your IDE ?>
like image 106
Derick Avatar answered Oct 19 '22 23:10

Derick