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

纵饮孤独 提交于 2019-12-05 17:42:09

问题


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!)


回答1:


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 ?>



回答2:


Despite this is quite old topic, but it is still actual. That's why I'm going to copy-paste my answer from the similar topic here as well.

This is XDEBUG issue, which can be fixed by downgrading (or upgrading in case you work with very old PHP) of XDEBUG version to 2.6.1. I accidentally faced this issue and wasn't able to find an answer. Reinstall of server, PHP, PHPStorm, different versions of XDEBUG didn't help. For MacOS one can use

pecl install xdebug-2.6.1

In case you already have newer version you can use command

pecl uninstall xdebug

Answers in this article helped me a lot after several hours of investigation.




回答3:


Not an answer, but you probably could use __DIR__ in php 5.3.
UPD. Found that it often contains not what you expect it to.




回答4:


I know it is an old question. I solved by assigning it to a variable, then it works fine!

$file = __FILE__;
include dirname($file) . '/../whateverfile.php';



回答5:


Create a breakpoint on line print(__FILE__); and analyse which variables are available to you.



来源:https://stackoverflow.com/questions/4924676/how-can-i-get-the-php-magic-constant-file-work-with-eclipse-and-pdt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!