【问题标题】:PHP Xdebug Overrides __Dir__ and __File__ constantPHP Xdebug 覆盖 __Dir__ 和 __File__ 常量
【发布时间】:2021-12-27 09:42:49
【问题描述】:

在使用 VS 代码中的“Listen for XDebug”调试 PHP 脚本时(Felix Becker 扩展的 PHP 调试),我发现应该返回当前运行脚本的目录和文件名的 __DIR__ and __File__ 是被 Xdebug 覆盖:

__DIR__ contains
"xdebug:"
__FILE__ contains
"xdebug://debug-eval"

但是,如果我将值分配给局部变量,它会按预期工作。

$dir = __DIR__; // $dir holds the correct path, instead of "xdebug:"

这里遇到了这个问题Xdebug weird __DIR__ constant

但是帖子很旧,并且缺少对意外行为的足智多谋的解释。

感谢您的帮助。

【问题讨论】:

    标签: php xdebug


    【解决方案1】:

    您得到的输出不正确。 __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__;");'
    

    也输出:

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

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

    __FILE__ 在 PHP 源代码中按预期工作,可以用这个 sn-p 证明:

    <?php $far = __FILE__; // now evaluate $far in your IDE ?>
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 2011-02-14
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2023-03-15
      • 2013-02-17
      相关资源
      最近更新 更多