Static call versus Singleton call in PHP

The difference between Singleton and Static design patterns in PHP.



In the past several months I’ve been working with a rather large application built with symfony. I noticed that symfony makes heavy use of the Singleton pattern (other frameworks, like Zend do that too); everywhere in the code there were pieces like this one:

// ...
// ...

I know that in more than half of the situations, you can write your code using plain static classes, with some initialize() method, as an alternative to writing singletons. For example, this is a dummy Singleton:

class DummySingleton {
    private function __construct(){}
    private function __clone(){}
    public static function getInstance(){
        if(self::$__instance == NULL) self::$__instance = new DummySingleton;
        return self::$__instance;
    public function foo(){
        echo 'I am DummySingleton::foo()';

Now this is a completely useless class, but it suits our purpose of illustrating the Singleton. Notice the amount of code needed by the Singleton pattern. Except the…

View original post 652 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s