Here's an example of my bootstrap loader:
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', realpath(getcwd() . '/../application'));
}
/**
* Add the APPLICATION_PATH and the library dir to the include_path
*/
set_include_path(get_include_path() . PATH_SEPARATOR . APPLICATION_PATH . PATH_SEPARATOR . realpath(APPLICATION_PATH . '/../library'));
/**
* Load the file loader to setup the class autoloader
*/
include_once 'Loader.php';
if (!class_exists('Loader')) {
die('Could not load class loader.');
}
spl_autoload_register('Loader::autoload');
I'd personally go with the second way. I've learned to love the include_path: there is probably some performance hit from looking in lots of directories but I doubt it would be significant. It also prevents errors from forgetting to include the path constant.
On a side note I put my controllers, etc. in /application/
, and have some libraries in /library/
. This all goes above the web root. It fully prevents users from accessing these files, which you have to otherwise take precautions against if you have everything under the document root. If your host supports this (some shared hosts don't) take advantage of it!
Update
In case of a high-load application is it good to go with the second method ??
In my opinion if you were to keep an eye on what's in your include_path
(for example, on my Windows dev machine I have all sorts in there that I don't need: SQL Server, Ruby etc.) and were to strip out anything not needed then the second method would be fine.
Another thing you could do is dump the include_path
at the end of the script and hard code it into your php.ini file.
Really, though. I don't think this is going to be a bottleneck in your system performance. Use what's easier for you.
Do you have performance issues on a site you're running or is this precautionary stuff? I can see why you'd want to preoptimise (I have to stop myself from doing it) but seriously. Deal with those issues when they arise. When you're in the lucky position of having a popular site deal with it then. At the end of the day, it's not a nightmare if you have to replace a few require
s.