Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a PHP project, what patterns exist to store, access and organize helper objects? [closed]

I would avoid the Singleton approach suggested by Flavius. There are numerous reasons to avoid this approach. It violates good OOP principles. The google testing blog has some good articles on the Singleton and how to avoid it:

http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html http://googletesting.blogspot.com/2008/05/tott-using-dependancy-injection-to.html http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html

Alternatives

  1. a service provider

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html

  2. dependency injection

    http://en.wikipedia.org/wiki/Dependency_injection

    and a php explanation:

    http://components.symfony-project.org/dependency-injection/trunk/book/01-Dependency-Injection

This is a good article about these alternatives:

http://martinfowler.com/articles/injection.html

Implementing dependency injection (DI):

  • I believe you should ask what is needed in the constructor for the object to function: new YourObject($dependencyA, $dependencyB);

  • You can provide the needed objects (dependencies) manually ($application = new Application(new MessageHandler()). But you can also use a DI framework (the wikipedia page provides links to PHP DI frameworks).

    Important is that you only pass in what you actually use (call an action on), NOT what you simply pass to other objects because they need it. Here's a recent post from 'uncle Bob' (Robert Martin) discussing manual DI vs using framework.

Some more thoughts on Flavius's solution. I don't want this post to be an anti-post but I think it's important to see why dependency injection is, at least for me, better than globals.

Even though it is not a 'true' Singleton implementation, I still think Flavius got it wrong. Global state is bad. Note that such solutions also use difficult to test static methods.

I know a lot of people do it, approve it and use it. But reading Misko Heverys blog articles (a google testability expert), rereading it and slowly digesting what he says did alter the way I see design a lot.

If you want to be able to test you application, you'll need to adopt a different approach to designing your application. When you do test-first programming, you'll have difficulty with things like this: 'next I want to implement logging in this piece of code; let's write a test first that logs a basic message' and then come up with a test that forces you to write and use a global logger that can't be replaced.

I am still struggling with all the information I got from that blog, and it's not always easy to implement, and I have many questions. But there's no way I can go back to what I did before (yes, global state and Singletons (big S)) after I grasped what Misko Hevery was saying :-)


class Application {
    protected static $_singletonFoo=NULL;

    public static function foo() {
        if(NULL === self::$_singletonFoo) {
            self::$_singletonFoo = new Foo;
        }
        return self::$_singletonFoo;
    }

}

This is the way I'd do it. It creates the object on demand:

Application::foo()->bar();

It's the way I am doing it, it respects OOP principles, it's less code than how you're doing it right now,and the object is created only when the code needs it for the first time.

Note: what I've presented is not even a real singleton pattern. A singleton would allow only one instance of itself by defining the constructor (Foo::__constructor()) as private. It is only a "global" variable available to all "Application" instances. That's why I think its use is valid as it does NOT disregard good OOP principles. Of course, as anything in the world, this "pattern" should not be overused either!

I've seen this being used in many PHP frameworks, Zend Framework and Yii among them. And you should use a framework. I'm not going to tell you which one.

Addendum For the ones among you worrying about TDD, you can still make up some wiring to dependency-inject it. It could look like this:

class Application {
        protected static $_singletonFoo=NULL;
        protected static $_helperName = 'Foo';

        public static function setDefaultHelperName($helperName='Foo') {
                if(is_string($helperName)) {
                        self::$_helperName = $helperName;
                }
                elseif(is_object($helperName)) {
                        self::$_singletonFoo = $helperName;
                }
                else {
                        return FALSE;
                }
                return TRUE;
        }
        public static function foo() {
                if(NULL === self::$_singletonFoo) {
                        self::$_singletonFoo = new self::$_helperName;
                }
                return self::$_singletonFoo;
        }
}

There's enough room for improvement. It's just a PoC, use your imagination.

Why do it like that? Well, most of the time the application won't be unit-tested, it will actually be run, hopefully in a production environment. The strength of PHP is its speed. PHP is NOT and never will be a "clean OOP language", like Java.

Within an application, there is only one Application class and only one instance of each of its helpers, at most (as per lazy loading as above). Sure, singletons are bad, but then again, only if they don't adhere to the real world. In my example, they do.

Stereotyped "rules" like "singletons are bad" are the source of evil, they're for lazy people not willing to think for themselves.

Yeah, I know, the PHP manifesto is BAD, technically speaking. Yet it's a successful language, in its hackish way.

Addendum

One function style:

function app($class) {
    static $refs = array();

    //> Dependency injection in case of unit test
    if (is_object($class)) {
        $refs[get_class($class)] = $class;
        $class = get_class($class);
    }

    if (!isset($refs[$class]))
        $refs[$class] = new $class();

    return $refs[$class];
}

//> usage: app('Logger')->doWhatever();

I like the concept of Dependency Injection:

"Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields. (From Pico Container Website)"

Fabien Potencier wrote a really nice series of articles about Dependency Injection and the need to use them. He also offers a nice and small Dependency Injection Container named Pimple which I really much like to use (more info on github).

As stated above, I don't like the use of Singletons. A good summary on why Singletons aren't good design can be found here in Steve Yegge's blog.


The best approach is to have some kind of a container for those resources. Some of the most common ways to implement this container:

Singleton

Not recommended because it is hard to test and implies a global state. (Singletonitis)

Registry

Eliminates singletonitis, bug I'd not recommend registry too, because it is a kind of singleton too. (Hard to unit test)

Inheritance

Pity, there is no multiple inheritance in PHP, so this limits all to the chain.

Dependency injection

This is a better approach, but a bigger topic.

Traditional

The simplest way of doing this is using constructor or setter injection (pass dependency object using setter or in the class constructor).

Frameworks

You may roll your own dependency injector, or using some of the dependency injection frameworks, eg. Yadif

Application resource

You may initialize each of your resources in the application bootstrap (which acts as a container), and access them anywhere in app accessing the bootstrap object.

This is the approach implemented in Zend Framework 1.x

Resource loader

A kind of a static object which loads (creates) needed resource only when needed. This is a very smart approach. You may see it in action e.g. implementing Symfony's Dependency Injection component

Injection to specific layer

The resources are not always needed anywhere in the application. Sometimes you just need them e.g. in the controllers (MV C ). Then you may inject the resources only there.

The common approach to this is using docblock comments to add injection metadata.

See my approach to this here:

How to use dependency injection in Zend Framework? - Stack Overflow

In the end, I'd like to add a note about very important thing here - caching.
In general, despite the technique you choose, you should think how the resources will be cached. The cache will be the resource itself.

The applications can be very big, and loading all resources upon each request is very expensive. There are many approaches, including this appserver-in-php - Project Hosting on Google Code.


If you want to make objects globally available, the registry pattern could be interesting for you. For inspiration, have a look at Zend Registry.

So also the Registry vs. Singleton question.