Home arrow PHP arrow Developing a Simple Blog Application in PHP

Developing a Simple Blog Application in PHP

In this third part of the series, I develop the domain layer of a sample blog application. In this case, for the sake of simplicity, the domain layer will be able to fetch, save, and delete blog post entries from the underlying storage layer.

  1. Developing a Simple Blog Application in PHP
  2. Defining generic entities for the domain layer
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 16, 2011

print this article



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: 



class Autoloader
    private static $_instance;
     * Get the Singleton instance of the autoloader
    public static function getInstance()
        if (self::$_instance === null) {
            self::$_instance = new self;
        return self::$_instance;
     * Reset the instance of the autoloader
    public static function resetInstance()
        self::$_instance = null;
     * Class constructor
    private function __construct()
        spl_autoload_register(array(__CLASS__, 'load'));
     * Prevent to clone the instance of the autoloader
    private function __clone(){}
     * Load a given class or interface
    public static function load($class)
        $file = str_replace('', '/', $class) . '.php';
        if (!file_exists($file)) {
            throw new AutoloaderException('The file ' . $file . ' containing the requested class or interface ' . $class . ' was not found.');
        require $file;
        if (!class_exists($class, false) && !interface_exists($class, false)) {
            throw new AutoloaderException('The requested class or interface ' . $class . ' was not found.');




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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: