Recently, I updated my structure in preparation for the second version of our company CMS. I unzipped a huge number of things that I did static to replace them with ordinary objects. In doing so, I created tremendous flexibility that was used to rely on how I went about and cracked kernel files. Now I use only static constructs, when the only alternative is global functions that are associated only with basic low-level functionality.
I am going to show a few lines of my bootstrap.php file (all my requests are sent through this file, but you can achieve the same result by including it at the beginning of each file) to show you what I mean. This is a pretty impressive version of what you probably use in your situation, but hopefully this idea is useful. (All of this is slightly modified.)
//bootstrap.php ... // CONSTRUCT APPLICATION { $Database = new Databases\Mysql( Constant::get('DATABASE_HOST'), Constant::get('DATABASE_USER'), Constant::get('DATABASE_PASSWORD'), Constant::get('DATABASE_SCHEMA') ); $Registry = new Collections\Registry; $Loader = new Loaders\Base; $Debugger = new Debuggers\Dummy; // Debuggers\Console to log debugging info to JavaScript console $Application = new Applications\Base($Database, $Registry, $Loader, $Debugger); } ...
As you can see, I have every opportunity to create my application object, which I can provide as an argument in the constructor for other objects to give them access to these "global" needs.
The database object is self explanatory. The registry object acts as a container for the object that I can get elsewhere in the application. The bootloader acts as a utility for loading other resources, such as template files. And the debugger should handle the debug output.
I can, for example, change the database class that I create, and, voila, I have a connection to the SQLite database. I can change the debugger class (as noted), and now all my debugging information will be written to my JavaScript console.
Ok, back to this problem. How do you give other objects access to all of this? You just pass it in an argument to the constructor.
// still bootstrap.php ... // DISPATCH APPLICATION { $Router = new Routers\Http($Application); $Router->routeUri($_SERVER['REQUEST_URI']); } ...
Not only that, but my Router (or any other object that I build with it) is also more flexible. Now I can just instantiate the application object in different ways, and my Router will behave differently accordingly.