PHP 10.0 Blog

What if…

dirname(__FILE__)

Posted by Stas on March 21, 2007

This expression - dirname(__FILE__) - is used in a real lot of places. The reason is simple - libraries want to include files relative to library top directory, and do not want to count on include path. And relative include resolution rules in PHP not clear to all, so people prefer to be sure. The downside here is that this expression is dynamic - executed at run-time. Meaning it’s slower and less toolable and also makes a bad habit of putting dynamic things into include (which is not a problem here, since it’s “static dynamic” thing, but still a bad habit).

So why won’t we have constant that would mean dirname(__FILE__)? Something like __FILEDIR__. Would make a lot of code cleaner.

The problem with it of course to make real use in code we should travel back in time to 1996 and add it there :)

18 Responses to “dirname(__FILE__)”

  1. Benjamin Klaile Says:

    If you want to start a poll, I’ll vote for it! :D

    Currently I do not use it anymore, but a lot of older scripts contains this “expression”. Maybe it would be no fault to go back to 1996 … although I would be too young O.o

  2. Evert Says:

    Great idea and I totally agree..

    I personally tend to always use the latest PHP version all over the place (unless its a x.x.0 version) so for me this would be a great feature..

    either that or make dirname a language construct, which would have the same effect ;) but i dont think thats gonna happen..

  3. Benjamin Klaile Says:

    but i dont think thats gonna happen

    Because I don’t necessarily expect any of it to be in PHP 6, 7, 8 and Because I don’t necessarily expect any of it to be in PHP 6, 7, 8 and 9

    :)

  4. Benjamin Klaile Says:

    *oops* .. double quoted. Please forgive :)

  5. Sara Golemon Says:

    This is something that can be easily handled by an opcode optimizer. When a function always returns the same output based on some constant input (e.g. md5(’foo’ ;) ) that entire expression can be reduced to a simple constant itself (’acbd18db4cc2f85cedef654fccc4a4d8′ in this case). Extrapolating an absolute path (which __FILE__ always contains) to the result of the dirname() function is just as trivial and just as portable across versions and hosts.

  6. Carl-Christian Salvesen Says:

    Why not just use this snipplet?


    ini_set('include_path', dirname(__FILE__).':'.ini_get('include_path'));

  7. Stas Says:

    Well, this exactly one because it won’t work on Windows :) But properly fixing the ‘:’ part, still it might be a problem if for example two libraries have files names by the same names - include path could find the wrong one.

  8. fett Says:

    Also: searching a file in include path is much slower that a direct include with an absolute path.

  9. R. Rajesh Jeba Anbiah Says:

    My feature request is worth to mention here http://bugs.php.net/40853

  10. PHPDeveloper.org Says:

    PHP 10.0 Blog: dirname(__FILE__)

  11. developercast.com » Blog Archive » PHP 10.0 Blog: dirname(__FILE__) Says:

    [...] the PHP 10.0 Blog today, Stas has a suggestion that might help out developers that use the combination “dirname(__FILE__)” to get the [...]

  12. Jeremy Johnstone Says:

    An alternative is to do something like the following:

    By doing this, you save the “run-time” cost (which is trivial anyway, but why do things which aren’t needed), it maintains backwards compatibility with PHP4, and requires nothing of anyone else but yourself.

  13. no Says:

    What about the super constant ‘PATH_SEPERATOR’ !?

  14. Rich Buggy Says:

    It’s never too late to include a good idea like this. Eventually PHP 4 and 5 will stop being supported. Let’s get this into 5.2.2 (or 5.3) so that it’s there for 6!! Then people writing new code for PHP 6+ can use it.

  15. Rich Buggy » Blog Archive » dirname(__FILE__) Says:

    [...] was just reading the PHP 10.0 Blog when I saw a post called dirname(__FILE__). It’s a call for a new constant that provides the directory of the current file. Personally I [...]

  16. medyk Says:

    ..and best (most efficient) way to get name of parent directory of directory etc. etc. is not e.g. playing with substr() function but instead nesting dirname calls like: dirname(dirname(__FILE__)) :)

  17. nothing happens Says:

    [...] While the “proper” use of __FILE__ is in conjunction with __LINE__ to report errors (”error occurred at __LINE__ of __FILE__!”), the push toward more modular OO coding style coupled with PHP’s lack of a package system has resulted in the expression “dirname(__FILE__)” popping up all over the place. [...]

  18. Do You PHP はてな Says:

    [PHP][雑記]オレオレ定数「__FILEDIR__」

    「毎回dirname(__FILE__)すんの、面倒くさくね?__FILEDIR__みたいなのがあったら済む話じゃん」といった内容です(多分)。 So why won’t we have constant that would mean dirname(__FILE__)? Something like __FILEDI…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>