Is it a good practice to bootstrap your PHP application. I found two ways to bootstrap my PHP application. Need some suggestions which is a better way.
First.
Define a constant for the folder structures
$controllerPath = 'controller';
define('CONTROLLER', str_replace('\\', '/', realpath($controllerPath)).'/');
//usage
require_once CONTROLLER . 'somecontroller.php';
Second
Using ini_set set the include path to the application root
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$includePath = ini_get('include_path');
ini_set('include_path', '.'.PATH_SEPARATOR.$rootPath.PATH_SEPARATOR.$includePath);
//usage
require_once 'controller/somecontroller.php';
Please tell me which is a better way.
In case of a high-load application which would be the best method ??
Use ini_set to set it to the directory -above- your application. That why you can use the literal strings in your require statements. In addition, it makes it easier to use reuse code
require 'coolapp/class/Model.php'
require 'coolapp/display/Router.php'
require 'spinoff/display/JsView.php'
// etc
Its similar to the idea in java of having fully qualified imports of com.whatever.app.more, or how in python all of an apps imports should be absolute with respect to that app.
Re: High load application
Unless you are loading many thousand files, the time it takes to include files probably isn't a bottle neck. But, if it was the case you have a couple options. One is APC, which caches the results of include in memory. Another is to load everything from a single file, similar to how javascript files are concatenated into one for better performance (coincidentally, APC has a function that gives you this information). APC is really easy to setup and is completely transparent, for a boost of ~50% better performance.
Better use absolute paths than letting PHP find the file in one of the given include paths.
So I tend for the first way using a constant that holds the absolute path to the application root.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With