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

后端 未结 5 1070
刺人心
刺人心 2021-02-20 04:23

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

print(__FILE__);

resulted in

5条回答
  •  野性不改
    2021-02-20 05:10

    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
    
    

    even though the script source was:

    // test.php
    
    

    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:

    
    

提交回复
热议问题