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.Filed on 22-02-2008, 08:08 under Magic constant, PHP & 13 comments & no trackbacks
Trackback specific URI for this entry
Paul M opines:
published on February 22nd 2008, 10:27:43 amGood addons. I like it.
Reply
Toby responses:
published on February 22nd 2008, 10:42:02 amI’ve been waiting for this for a long, long time. :)
Reply
bapro responses:
published on February 22nd 2008, 12:57:36 pmLittle but nice change.
Reply
Andre Moelle responses:
published on February 22nd 2008, 02:42:58 pmSomething I really missed so far. ;-)
Reply
Stuart Herbert responses:
published on February 23rd 2008, 12:30:44 pmWhat 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__))?
Reply
Lars Strojny reckons:
published on February 23rd 2008, 01:49:09 pmInternally 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.
Reply
Stuart Herbert states:
published on February 23rd 2008, 04:04:09 pmHrm. You’d really want the symlink to be honoured. Shame.
Reply
Lars Strojny responses:
published on February 23rd 2008, 09:11:35 pmI’m not really sure if it would be a good thing to change the behaviour. I mean, what would be the benefit?
Reply
NikoB supposes:
published on September 26th 2008, 09:48:53 pmThis 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).
Reply
BlazS reckons:
published on February 25th 2008, 08:57:53 amisn’t the require_once ‘./baz.php’ the same ?
Reply
Lars Strojny means:
published on February 25th 2008, 09:24:18 amQuestion 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?
Reply
BlazS supposes:
published on February 25th 2008, 09:33:30 amMy mistake :P
Reply
Omardoliamy means:
published on January 20th 2009, 03:52:16 pmI’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
Reply