patternphpMinor
Converting bootstrap file from procedural code to OOP
Viewed 0 times
filebootstrapproceduralcodeconvertingfromoop
Problem
My bootstrap file contained procedural code, but after stumbling upon this answer I tried to make my bootstrap object oriented.
This is my attempt so far, and I have a feeling that I'm failing at it. I also know that some parts are not really working, because usually with procedural code the changes are made globally and not within a class' scope. I don't know how I can achieve that. Also the methods would have to be performed in order.
Could someone review my bootstrap code and how I can better the logic / usability?
I'm sorry if my code is a little bit overwhelming, but any help is highly appreciated!
Old procedural bootstrap (index.php)
```
# report all php errors
error_reporting(E_ALL);
# OS independent directory separator
define('DS', DIRECTORY_SEPARATOR);
# define site root path
define('ABSPATH', dirname(__FILE__) . DS);
# auto include entire config array
foreach (glob(ABSPATH . 'config' . DS . '*.php') as $configFile) {
require $configFile;
}
# auto include all functions
foreach (glob(ABSPATH . 'functions' . DS . '*.php') as $functionFile) {
require $functionFile;
}
# set include paths to be used for class auto loading
set_include_path(
ABSPATH . 'core' . PATH_SEPARATOR .
ABSPATH . 'controllers' . PATH_SEPARATOR .
ABSPATH . 'models' . PATH_SEPARATOR .
ABSPATH . 'libraries' . PATH_SEPARATOR .
ABSPATH . 'helpers'
);
# enable auto loading of classes with the include paths above
spl_autoload_register(
function ($classname)
{
$classname = ltrim($classname, '\\');
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
$classname = str_replace('\\', '/', $match[1]) . str_replace(['\\', '_'], '/', $match[2]) . '.php';
require_once $classname;
}
);
# create config object for global scope
$config = new Config($config);
# load session ini settings
foreach ($config->get('session') as $setting => $value) {
ini_set($setting, $value);
}
session_name('sid');
session_st
This is my attempt so far, and I have a feeling that I'm failing at it. I also know that some parts are not really working, because usually with procedural code the changes are made globally and not within a class' scope. I don't know how I can achieve that. Also the methods would have to be performed in order.
Could someone review my bootstrap code and how I can better the logic / usability?
I'm sorry if my code is a little bit overwhelming, but any help is highly appreciated!
Old procedural bootstrap (index.php)
```
# report all php errors
error_reporting(E_ALL);
# OS independent directory separator
define('DS', DIRECTORY_SEPARATOR);
# define site root path
define('ABSPATH', dirname(__FILE__) . DS);
# auto include entire config array
foreach (glob(ABSPATH . 'config' . DS . '*.php') as $configFile) {
require $configFile;
}
# auto include all functions
foreach (glob(ABSPATH . 'functions' . DS . '*.php') as $functionFile) {
require $functionFile;
}
# set include paths to be used for class auto loading
set_include_path(
ABSPATH . 'core' . PATH_SEPARATOR .
ABSPATH . 'controllers' . PATH_SEPARATOR .
ABSPATH . 'models' . PATH_SEPARATOR .
ABSPATH . 'libraries' . PATH_SEPARATOR .
ABSPATH . 'helpers'
);
# enable auto loading of classes with the include paths above
spl_autoload_register(
function ($classname)
{
$classname = ltrim($classname, '\\');
preg_match('/^(.+)?([^\\\\]+)$/U', $classname, $match);
$classname = str_replace('\\', '/', $match[1]) . str_replace(['\\', '_'], '/', $match[2]) . '.php';
require_once $classname;
}
);
# create config object for global scope
$config = new Config($config);
# load session ini settings
foreach ($config->get('session') as $setting => $value) {
ini_set($setting, $value);
}
session_name('sid');
session_st
Solution
First off, let me say this: There's no need for your bootstrap to be OO. Your Bootstrap class will become a utility class with no real purpose, which does everything. That is also known as a God Object.
From what I see, you're better off using the procedural one.
Review for OO:
Review for Procedural
Here's an example for a good bootstrap file for an MVC application: How should a Model be structured in MVC?
From what I see, you're better off using the procedural one.
Review for OO:
- Naming convention: Setters should be prefixed with
set:setDirectorySeparator,setRootPath, etc. It's not clear whatdirectorySeparatordoes at a glance.
- Don't use Globals: The entire point of moving an application to OOP is to better manage the dependencies and move away from global variables.
- Don't use Service Locators: Your
$configvariable is a service locator. You will inject it (or worse, global include it) everywhere, it contains all of the configurations and all you need is one or two entries. Don't do that. Give each function/method exactly what it needs.
- Application bootstrapping should not be OO. See above explanation.
Review for Procedural
- Use a class for Autoloading: It makes things a whole lot simpler, and it looks nicer too. I see you're using PSR-0, there are easy and simple solutions out there available.
- Arbitrarily requiring files and adding ini entries is not safe: If you don't sanitize user input properly in any part of your application, this may cause you some severe breakage on the server. Beware!
Here's an example for a good bootstrap file for an MVC application: How should a Model be structured in MVC?
Context
StackExchange Code Review Q#52543, answer score: 8
Revisions (0)
No revisions yet.