New magic constant in PHP 5.3 13

In PHP 5.3 there will be another magic constant __DIR__. Until 5.3 a typical pattern to include files was to do something like this:

[geshi lang=php]The extra dirname()-call will be gratuitous:

[geshi lang=php]__DIR__ always references the directory which contains the current file. In case of /var/www/host/app/foo.php the __DIR__ will reference /var/www/host/app.
To allow this, the internal function php_dirname() has been moved in the Zend Engine and is now called zend_dirname(). Nevertheless an alias still exists.

Filed on 22-02-2008, 08:08 under , & 13 comments & no trackbacks


Trackback specific URI for this entry

No Trackbacks


  1. Paul M opines:
    published on February 22nd 2008, 10:27:43 am *

    Good addons. I like it.


  2. Toby responses:
    published on February 22nd 2008, 10:42:02 am *

    I’ve been waiting for this for a long, long time. :)


  3. bapro responses:
    published on February 22nd 2008, 12:57:36 pm *

    Little but nice change.


  4. Andre Moelle responses:
    published on February 22nd 2008, 02:42:58 pm *

    Something I really missed so far. ;-)


  5. Stuart Herbert means:
    published on February 23rd 2008, 12:30:44 pm *

    What will the value of DIR be if the path to the current file contains a symlink? Will DIR contain the smylink, or will it be more like realpath(dirname(__FILE__))?


  6. Lars Strojny reckons:
    published on February 23rd 2008, 01:49:09 pm *

    Internally it is implemented exactly as dirname(__FILE__). If /tmp/bar is a symlink to /tmp/foo and /tmp/foo/baz.php does something with the constant the value will be /tmp/foo whether it is executed as /tmp/foo/baz.php or /tmp/bar/baz.php.


  7. Stuart Herbert says:
    published on February 23rd 2008, 04:04:09 pm *

    Hrm. You’d really want the symlink to be honoured. Shame.


  8. Lars Strojny responses:
    published on February 23rd 2008, 09:11:35 pm *

    I’m not really sure if it would be a good thing to change the behaviour. I mean, what would be the benefit?


  9. NikoB supposes:
    published on September 26th 2008, 09:48:53 pm *

    This is very useful for some people. We need to run the same code from different symlinks to the same directory and this is not possible because symlinks are resolved. There is no way to tell if the script was called from a symlink, and which one. This is especially true in included files, where $_SERVER[‘SCRIPT_FILENAME’] returns the path to the calling script (and not the included one, obviously, since httpd doesn’t call the include directly, it’s PHP’s job).


  10. BlazS answers:
    published on February 25th 2008, 08:57:53 am *

    isn’t the require_once ‘./baz.php’ the same ?


  11. Lars Strojny means:
    published on February 25th 2008, 09:24:18 am *

    Question of the day: which file does "./baz.php" reference if "bar.php", the file which contains that directive, is included by "foo.php" one directory above?


  12. BlazS reckons:
    published on February 25th 2008, 09:33:30 am *

    My mistake :P


  13. Omardoliamy means:
    published on January 20th 2009, 03:52:16 pm *

    I’ve recently read it in PHP.net docs :).
    But I can’t install the latest php :)
    I’m using now LAMPP it’s using php-5.2.6
    Anyway it’s good thing to use in future


Add a Comment & let me know what you think