This seems to be the focus of many PHP developers, so let's get to it well. Most PHP applications put their code in include '../../library/someclass.php.class' . This is not very good for anyone, because it is very easy to break, and no one likes to do work with the janitor when you need to code. It is also like building a house of cards and strengthening ties, fearing any changes. So good, maybe we could just create a constant and use the full path?
define('PATH', '/home/me/webroot/Application'); include(PATH . '/Library/someclass.php.class');
Well, thatโs very good, but erm, what if we deploy on windows? Also, will we define a path at each script entry point? Not very DRY if you ask me. Plus, moving deployments will be a huge pain. Clearly, while we are closer, this is not a very good improvement.
Fortunately, PHP provides several magic functions that can help us immediately.
So, let's just say that you have one entry point for your application, or at least a common header file. We can quickly grasp our deployment root if we know where our header file is associated with the code root. IE, in /home/me/webroot/Application/Init/set_paths.php
define('PATH_SITE', realpath(dirname(__FILE__) . '/../../'));
Amazing, this is our document root. It is OS independent and fairly easy to adapt if you change the place of set_paths.php . Now we can talk about some other places in our application, simply because the constants are convenient:
define('PATH_APPLICATION', realpath(PATH_SITE . "/Application")); define('PATH_LIBRARY', realpath(PATH_SITE . "/Application/Library")); define('PATH_CONFIG', realpath(PATH_SITE . "/Config")); define('PATH_WRITE', realpath(PATH_SITE . "/Volatile"));
This is very good and good, but in reality it is not much better than our previous solution. Enter the PHP enable path. Adding the corresponding constants to our path, we will not define them every time. The order of the paths in the path of inclusion is actually very important for speed, so we are working hard to get them in order of use.
$paths['inc'] = array_flip(explode(PATH_SEPARATOR, get_include_path())); unset($paths['inc']['.']); $paths['inc'] = array_flip($paths['inc']); // The first item on the path the external // libs that get used all the time, // then the application path, then the // site path, and any php configured items. // The current directory should be last. $paths = array_merge(array(PATH_LIBRARY, PATH_APPLICATION, PATH_SITE), $paths['inc'], array(".")); set_include_path(implode(PATH_SEPARATOR, $paths));
Now all critical places in our application are on the way, and you can include it in your content, regardless of where you decide to store your libraries, settings, etc.
include('someclass.php.class');
Step further
If you are working with a fairly well-developed OOP application, we can go a little further. If you subscribe to one file, one class, then the PEAR naming convention makes life very simple.
PEAR naming conventions dictate a 1: 1 ratio between the file system and the class. For example, the class Foo_Bar_Baz will be found in the file "Foo / Bar / Baz.php" on your include_path. a source
Once you have a predictable mapping of files to classes, you can implement spl_autoload_register and you can replace
include('someclass.php.class'); new SomeClass();
Just
new SomeClass();
And ask PHP to handle this for you.