我如何获得PHP魔术常数__FILE__与Eclipse和PDT配合使用 [英] How can i get the PHP magic constant __FILE__ work with Eclipse and PDT

查看:147
本文介绍了我如何获得PHP魔术常数__FILE__与Eclipse和PDT配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,当我用XDebug调试一些PHP文件(在Ubuntu上的Eclipse下)时,我遇到了一个奇怪的行为:

 打印(__ FILE__); 

导致

 code>xdebug eval

GEE!



所以这个魔术常数似乎可以使用。



任何人都知道修复或可行的解决方法?如何调试调试器?
(硬编码一个路径是一个PITA!)

解决方案

您输出的输出并不正确。 __ FILE __ 是一个在分析器时间进行评估的特殊常量。当PHP脚本被编译时,它会真的读取这样的东西:

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

即使脚本来源是:

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

这意味着在解析之后,没有这样的常量 __ FILE __ ,因为它已经被替换了。



这意味着如果你在一个IDE中,通过DBGp的 eval 命令 eval - __FILE __ 它可以给你 __ FILE __ 任何文件名。相反,它使用当前上下文的文件名,它是 xdebug eval 或更高版本的 xdebug:// debug-eval



从本质上讲,这与以下操作相同:

  php -r'eval(__ FILE__;);'

哪个也输出:

 命令行代码(1):eval()'d代码

Xdebug寻找这种格式,并将其更改为 xdebug:// debug-eval ,以便它可以实际调试评估代码。



__ FILE __ 按照PHP源代码中的预期工作,可以通过此代码段证明:

 <?php $ far = __FILE__; //现在评估$在你的IDE?> 


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

解决方案

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

这篇关于我如何获得PHP魔术常数__FILE__与Eclipse和PDT配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆