Exploring PHP and object design
Before we look at specific solutions, we can explore various helpful aspects of PHP and also look at the pattern known as Singleton.
Autoloading
We want to automate the loading of code and PHP provided a tool to do exactly this with the launch of PHP5. It was the __autoload
function. When PHP came across a class it did not know about, it called __autoload
, passing the class name as parameter. It was up to the user to code a suitable body for the function.
This was a good start, and in the first edition it was the solution to the problem. However, PHP has moved on and improved the handling of autoloading. The obvious problem with __autoload
was that there could only be one such function. Someone writing, say, a library of PHP code for some particular purpose will not know about all the possible uses for the library, but may well want to use autoloading. So there really needs to be a system that supports multiple spheres of autoloading. PHP adapted to this need...