Xdebug weird __DIR__ constant

5.1k Views Asked by At

I am writing an PHP CLI app which loads YAML files. When trying to do this within an Xdebug session:

if (file_exists(__DIR__ . '/../../foo/bar')
{
    /* ... */
}

__DIR__ allways is xdebug: which will allways lead to false from file_exists().

Is there any work around?

3

There are 3 best solutions below

1
On

Set $dir = __DIR__; and use if (file_exists($dir . '/../../foo/bar'). It will work like that.

0
On

The thing is that your debugger shows you a wrong value as DIR has already been replaced in your script by the parser.

The whole explanation can be found here:

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

The output you get is not incorrect. FILE is a special constant that gets evaluated at parser time.

1
On

As an alternative replace your __DIR__ constant with dirname(__FILE__) function

if (file_exists(dirname(__FILE__) . '/../../foo/bar')
{
    /* ... */
}