If I use absolute paths, I can't move the whole directory to a new location. If I use relative paths, I can't move individual files to new locations.
What's the solution here? Do you set up a config file that holds the root path and go from there? Or do you have a rule like: Never move files around?
I've seen in some projects that people use dirname(FILE). What is the point of that, I mean, why not simply leave it out since the dirname is relative anyway (depending on where the file sits)?
Depends on your usage, define a constant to hold application path is one of the common practice
Don't use variable as it might get override somewhere in your application and can break your application
Is even better to combine with symlink (symbolic to create branches)
So, you can symbolic link
/home/testing -> /home/20111202000000 while keeping the stable version,
/home/latest -> /home/20111201000000
With this, you don't have to risk to break your production site while doing some testing/development, and, you can easily shift your development directory
you should use a config file that will be included in each file first line, for example your app look like this
root / App / Plugins
inside your root dir :
app-config.php
now, suppose you have to include a plugin file, so
inside your Plugin dir :
my-plugin.php
now everything below this line can use
ABSPATH
example do you want to load an image
now, the thing is more simple if your app is designed to automatically load some files like
so that everything inside this file or any other file loaded by the
my-plugin.php
file can use theABSPATH
without include each time theapp-config.php
file.with this in mind you can have all the short-hand you want into the
app-config.php
exampleso, now that you have all defined, if you need to move a file, let's say one folder forward example:
root / App / Plugins / Utils
just inlucde
require_once '../../../app-config.php';
obviously i suppose that you are not changing paths each time =) anyway if you need to do so is always more simple to change one file inclusion instead of hundreds.
hope this make sense to you =)
Absolute paths are better from a performance point of view when using an opcode cache or lots of require/include statement (although its only noticeable when you start to include hundreds of files, as might happen when using a framework like Zend/Symfony/etc).
With a relative path the opcode cache and php must work out the files realpath each time before it can work out if it already knows about the file and if it needs to load it again. PHP internally maintains a hashmap of files to file locations which is very quick as long it doesn't have to do the above calculation each time.
I always use absolute paths, but I also start any custom PHP project with a bootstrap file where I define the most commonly used paths as constants, based on values extracted from
$_SERVER
.This is how I define my root paths :
The path
LOCAL_PATH_ROOT
is the document root. The pathHTTP_PATH_ROOT
is the equivalent when accessing the same path via HTTP.At that point, converting any local path to an HTTP path can be done with the following code :
If you want to ensure compatibility with Windows based servers, you'll need to replace the directory seperator with a URL seperator as well :
Here's the full bootstrap code that I'm using for the PHP PowerTools boilerplate :
If you add the above code to your own project, outputting all user constants at this point (which can do with
get_defined_constants(true)
should give a result that looks somewhat like this :It's relative to the include path, anyway. The
dirname( __FILE__ )
(or just__DIR__
in PHP >= 5.3) is there so you can run the file from every location. In case you're using relative paths, the value "." may change. See:berry@berry-pc:~% cat so.php
berry@berry-pc:~% php so.php
berry@berry-pc:~% cd foo
berry@berry-pc:~/foo% php ../so.php
So, it is relative, but it's relative to the current working directory, not to the directory the file is located in. That's why you'll want to use
__DIR__
for this. And by the way; yes, I don't move files around an awful lot. If I do though, I'll have to update every call to that file, although I don't require or include an awful lot anymore, since I'm using an Autoloader.As for the other files I'm referring to (such as template files), I set the path manually, but once. I then refer to
$path . '/filename.php';
, so it's easier to change later.