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 ![]()
March 21, 2007 at 10:14 am
If you want to start a poll, I’ll vote for it!
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
March 21, 2007 at 10:22 am
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..
March 21, 2007 at 11:44 am
Because I don’t necessarily expect any of it to be in PHP 6, 7, 8 and
March 21, 2007 at 11:44 am
*oops* .. double quoted. Please forgive
March 21, 2007 at 1:17 pm
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.
March 21, 2007 at 1:25 pm
Why not just use this snipplet?
ini_set('include_path', dirname(__FILE__).':'.ini_get('include_path'));
March 21, 2007 at 2:08 pm
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.
March 21, 2007 at 3:32 pm
Also: searching a file in include path is much slower that a direct include with an absolute path.
March 22, 2007 at 12:33 am
My feature request is worth to mention here http://bugs.php.net/40853
March 22, 2007 at 4:09 am
PHP 10.0 Blog: dirname(__FILE__)
March 22, 2007 at 5:35 am
[...] the PHP 10.0 Blog today, Stas has a suggestion that might help out developers that use the combination “dirname(__FILE__)” to get the [...]
March 22, 2007 at 7:32 am
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.
March 22, 2007 at 2:29 pm
What about the super constant ‘PATH_SEPERATOR’ !?
March 24, 2007 at 2:57 am
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.
March 24, 2007 at 3:02 am
[...] 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 [...]
March 25, 2007 at 3:29 pm
..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__))
April 18, 2007 at 10:18 am
[...] 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. [...]
July 23, 2007 at 4:30 pm
[PHP][雑記]オレオレ定数「__FILEDIR__」
「毎回dirname(__FILE__)すんの、面倒くさくね?__FILEDIR__みたいなのがあったら済む話じゃん」といった内容です(多分)。 So why won’t we have constant that would mean dirname(__FILE__)? Something like __FILEDI…