If you frequently use an object-oriented approach to build your own PHP applications, you know that creating object graphs can be pretty challenging. After all, providing objects with their dependencies is not as easy as it seems, even in the simplest use case. Fortunately, things aren't hopeless when it comes to joining sets of interconnected objects; in the last few years, several approaches implemented for a long time in more mature languages (especially Java) have found their way into PHP. The first of these approaches is dependency injection (DI), which is now widely utilized by well-trusted frameworks and libraries, such as the Zend Framework, Symfony and Doctrine (to name a few). However, there's another method that you can use to give objects the collaborators they require to work properly. Yes, as you may have already guessed, in this case I'm talking about service locators. These are nothing but simple mediators whose responsibility it is to provide the objects with the corresponding dependencies, either statically or dynamically. Since the most effective way to demonstrate the functionality of service locators is by example, in earlier tutorials of this series I developed some approachable ones that showed how to use a service locator in the development of a basic registry library. In the first case, the locator was consumed statically. This approach is more difficult to test. In the second case, the locator was injected through the constructor of a client class, thus demonstrating that DI and service locators aren't mutually exclusive methods. They can live together in sweet harmony as well. While utilizing a service locator in the implementation of a basic registry is all well and good, at least for didactic purposes, I'd like to show you how to use this approach in a more realistic case. In this and upcoming articles in the series, I'm going to create a simple, yet functional blog application, which will use a locator to create its object graphs. If this proposal doesn't whet your appetite for knowledge, I'm going to astonish you with three different versions of the blog. The first one will rely purely on dependency injection, the second one will make use of a static locator, and the last one will combine the two previous approaches, by employing an injected locator. Now it's time to leave the dull theory behind and start learning how to use a service locator in the construction of a blogging program. Let's get going! Lazy-loading source classes: creating a simple autoloader Since the construction of the blog application mentioned in the introduction will make use of a bunch of classes, the first step is to define an autoloader. This autoloader will lazy-load the pertinent classes (and eventually a couple of interfaces) without having to appeal to multiple PHP requires. With that said, the autoloader looks like this: (Autoloader.php) <?php class Autoloader
(AutoloaderException.php) <?php class AutoloaderException extends Exception{} Well, if you've read some of my other articles published lately here at the Developer Shed network, you'll find the implementation of the above autoloader very familiar. In simple terms, this class is a Singleton that includes name spaced classes and interfaces on demand by using the PHP SPL stack. That is all it does. Now that you've grasped how this autoloader functions, it's time to start building the source classes that will make up the domain layer of this sample blog program. The first of these classes will be an abstract parent, and its definition will be shown in the upcoming section. To see how this abstract class looks, jump ahead and read the lines to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|