PHP Singleton Pattern – OOP

Introduction

Commonly when we think of OOP, things like extends, inheritence, visibility and polymorphism are terms that almost always come to mind (or at least should…). We, as developers, are constantly looking for the best approach: what makes my class a reusable object? How can I apply more abstraction? Does my hierarchy make sense? Is this object scalable? etc…

Typically, these are very short-minded and simple things developers of all levels think of when their only language for development has been PHP. While “short-minded” may seem a bit harsh, one must realize that there is much more to Object Oriented Programming than what we’ve grown up learning through PHP.  PHP,  while better at OOP as of the 5.x series, still does not have all the available OOP options needed to allow us to code  in a true OOP environment.    I truly believe that this is one reason why developers, like myself, never hear about “patterns” or realize just how important they are are until we feel we’ve mastered all there is to know about development with PHP.

Why?  Well, let’s face it –  As we get better, we become a little more cocky about our approaches and disregard other solutions as unnecessary overhead or a waste of time.

With that being said, the first section of this article will be on misconceptions.   It’s a really short section that covers some common misconceptions developers have of PHP.  If you feel this section isn’t for you, skip on down to the section “What is a Singleton Pattern?”.

Misconceptions

After you work with a language for over a decade, one tends to believe that they’ve covered and exhausted every topic until it’s been beaten to death.  This is unfortunate yet very common.   After working with any language this long its rightfully a belief we obtain not out of ego, but sheer experience.   It’s also extremely true for those of us who, again, have only really developed with a single language (such as PHP) as our main language.

With that in mind, some fundamental misconceptions must be brought to light that we have of PHP with OOP in mind.

  • Misconception #1 – PHP is a true OOP language

This is an unfortunate misconception PHP developers have everywhere.  While PHP has supported OOP since PHP 3.x and gotten a lot better with the 5.x version (5.3x has given us almost 100%), it is still not a 100% or true OOP language.  Simply put, it’s more of a language that supports OOP with a definitive implementation.  One should also realize that out of all the languages available which may use OOP designs better, they are still not considered to be a true OOP language.

  • Misconception #2 – OOP Patterns Are A Waste of Time/Effort

WRONG!   The majority who say this are either unable to grasp the very reasoning of patterns in the first place, or believe that it’s something that should only be used in advanced programming languages (C++, Java, etc).  The truth is patterns can help your code, make it more scalable, and ultimately improve performance..but only if used properly.  Does that sound a bit hypocritical?  On the surface sure, but realize that this is true for literally everything in PHP.  One example would be my article about use of Recursive Methods: If you use the “improper” but perfectly valid method instead of recursion, you would actually decrease performance — even though the code was valid!

  • Misconception #3 – Creating Multiple Instances of an Object is Perfectly Fine

It’s very easy to think about OOP in this manner.   It just makes sense right?  What’s wrong with creating a new instance of an object whenever we want to use this object?  Why not just keep unsetting the object after we use it, and re-instantiate it when we need it?  WHO CARES?  This is a very dangerous misconception.  Why these sorts of things matter is every time an object is created, the entire object is stored in memory.  This is especially true since PHP passes by reference in OOP land (which means it keeps values and representations of your object/values/variables/methods/etc in a block of memory rather than creating a “copy” (as was true with PHP 4.x and below unless you use the infamous & operator).  It also will slow down performance since that object must be rebuilt from the ground up every time you create it.  This is true since it must reset any/all local scope properties, variables, definitions in order to be considered a true new instance.

With this small list of  misconceptions out of the way, let’s get into the meat of our discussion.

The Singleton Pattern

When one creates a class, it stems (or at least should be) from needing a way to reuse a “set” of methods in multiple applications.  One of the most common classes a developer or team of developers build is a Database Handler (dbh) object (we will use this as our main example throughout the rest of this article).

A very common thing we can see through the use of our object is it is instantiated (ie: $dbh = new myDBH();).  This is used throughout all of our applications each time we need a database connection opened to our MySQL server from application to application.

So say that we have 5 different applications that can be loaded independently on separate pages or simultaneously on the same page :

  • user management system – constantly keeps track of our user(s) that are logged into our system
  • navigation system –  loads sections based on the page we are on
  • page manager – loads the page content we are wanting to load
  • advertisement application – controls what ads are displayed when/where
  • schedule application – keeps track of what content is allowed to be displayed on each page.

At this point, you may be saying “TELL ME WHAT THE HELL IS WRONG WITH THIS ALREADY!!”.  Ok…chill out psycho and I’ll explain.

Assume that each one of these applications has a line in it that defines a variable, $dbh,  that holds an instance of an object, myDBH.  Each time that our object is instantiated, a new instance is created for that object which includes:

  • a new connection to MySQL (5 connections per user!!!)
  • a new block of memory taken up to hold all the changes of this object (5 blocks per user!!!)
  • all the classes that belong in the hierarchy (5 times these are also loaded per user!!!)

I think you get the picture already.  Myself, I never really considered why this was such a bad deal as I never knew a different way existed to keep this sort of thing from happening.  Singleton to the rescue!

What is a Singleton Pattern?

A “singleton pattern” is nothing more than a way to maintain one instance and state of an object at any given time.  This means that instead of creating our object 5 different times, we instead make a call that says “wait, this object has already been instantiated.  here, let me return to you the current active instance instead”.  Immediately, you should realize a point (which will be covered shortly) that the singleton pattern is not always the best solution.

Wikipedia describes the singleton pattern in the following verbage:

In computer engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five). Some consider it ananti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.

In C++ it also serves to isolate from the unpredictability of the order of dynamic initialization, returning control to the programmer.

But why not create the instance of our object inside of a file that is loaded via include_once?  I guess that depends on the developer, team, and how they think about flow and “cleanliness” of their code base.  To me, including a file simply to have one line of code that creates an instance is, well, a waste of a file and line of code.  I also personally believe that this is considered “sloppy code”.  But, I will stop myself there before this starts sounding like a rant or that I’m standing on a soap box and preaching my own personal view.

We also must keep in mind that we will need to check to make sure that $dbh was defined before we use it. Otherwise, we need to create the object. We also must keep track of our variable names and make sure $dbh isn’t being defined as something elsewhere ($dbh instanceof myDBH for example).  Otherwise, we run the risk of ruining another tool’s implementation of that variable name (note that this wouldn’t be possible if the company standard has been defined to make $dbh a variable that can only hold an instance of myDBH…but each company is different ;) ).

At the end of the day, I would challenge you to at least understand how we can use the singleton approach to “improve” this sort of thinking and then — decide for yourself.

Creating a Singleton Pattern

Creating a singleton object is quite simple;  so simple it can even be applied to existing objects with minimal code rewrites!

To better explain this approach, let’s consider that the following code is our myDBH class:

<?php
class myDBH {
  protected $db_info = array("host" => "localhost", "dbname" => "myDB", "username" => "myDB_user", "password" => "myDB_password");
  public $dbh;
 
  public function __construct() {
    $this->dbh = new PDO("mysql:host={$this->db_info['host']};dbname={$this->db_info['db_name']}", $this->db_info['username'], $this->db_info['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  }
 
  public function fetch_rows($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetchAll();
  }
 
  public function fetch_row($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetch();
  }
}
?>

Disclaimer: The above class is simple by design. It’s not a production-ready class, but merely something quick I want to use for our example.

Usage of this class would be something like this (assuming we are using autoload obviously…)

<?php
$dbh = new myDBH();
$rst = $dbh->fetch_row("Select * from `mytable`");
// do some stuff...
?>

The above (as explained earlier) will create a new object every time we instantiate it, and create a new MySQL connection.

Now we want to implement a singleton solution. 2 questions come to mind:

  1. Do we want to maintain backwards compatibility, allowing us to update other scripts that may be using myDBH class at will?
  2. Do we just make the change and let any code that’s not updated break?

Those are questions you and your team should answer. If the answer to the first question to 1) is yes, then your construct will need to remain as it was prior to moving to the singleton pattern solution.   Realize that doing so would be considered more of a hybrid solution in that it allows us to have a singleton-like object, while still allowing the object to be instantiated normally.

So let’s look at both solutions.

Pure Singleton

<?php
class myDBH {
  private $db_info = array("host" => "localhost", "dbname" => "myDB", "username" => "myDB_user", "password" => "myDB_password");
  private $dbh;
 
  public static $instance = NULL;
 
  private function __construct(array $db_info = null) {
    if(isset($db_info)) {
      foreach($db_info as $key_name => $key_value) {
        if(!in_array($key_name, array("host", "db_name", "username", "password") || empty($key_value))) {
          throw new Exception("Invalid key passed!");
        }
        $this->db_info = $db_info;
      }
    }
    $this->dbh = new PDO("mysql:host={$this->db_info['host']};dbname={$this->db_info['db_name']}", $this->db_info['username'], $this->db_info['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  }
 
  public static function getInstance() {
    if(!isset(self::$instance)) {
      self::$instance = new myDBH();
    }
    return self::$instance;
  }
 
  public function fetch_rows($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetchAll();
  }
 
  public function fetch_row($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetch();
  }
}
?>

Our new usage would look like this:

// Option 1
$dbh = myDBH::getInstance();
$rst = $dbh->fetch_row("Select * from `mytable`");
 
// Option 2 (Method Chaining Example)
$rst = myDBH::getInstance()->fetch_row("Select * from `mytable`");

Explanation of Our Changes

The first thing to note is the change in visibility of some the following:

  • The constructor changed from public to private (this prevents anyone from instantiating this object without explicitly calling getInstance)
  • The $dbh variable changed from public to private (this prevents any child from overriding the $dbh variable with anything other than the singleton’s definition)
  • The db_info array changed from public to private (same reasoning as $dbh)

The second thing to note is the 2 new additions:

  • public static $instance – holds the actual instantiated object of our myDBH class
  • public static getInstance() – This method first checks to determine if $instance is null. If it is, it will attempt to create an instance of myDBH and store it inside this variable. Finally, we return self::$instance (this is also done when self::$instance is not null!).

With the pure solution, we obtain an instance of our object via the static method getInstance instead of instantiating it normally via new object(). getInstance() will either create a new instance of our myDBH object (if has never been called before) or retrieve the existing instance (if  getInstance() has already been called) of our object!

Hybrid Singleton

<?php
class myDBH {
  private $db_info = array("host" => "localhost", "dbname" => "myDB", "username" => "myDB_user", "password" => "myDB_password");
  private $dbh;
 
  public static $instance = NULL;
 
  public function __construct(array $db_info = null) {
    if(isset($db_info)) {
      foreach($db_info as $key_name => $key_value) {
        if(!in_array($key_name, array("host", "db_name", "username", "password") || empty($key_value))) {
          throw new Exception("Invalid key passed!");
        }
        $this->db_info = $db_info;
      }
    }
    $this->dbh = new PDO("mysql:host={$this->db_info['host']};dbname={$this->db_info['db_name']}", $this->db_info['username'], $this->db_info['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  }
 
  public static function getInstance() {
    if(!isset(self::$instance)) {
      self::$instance = new myDBH();
    }
    return self::$instance;
  }
 
  public function fetch_rows($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetchAll();
  }
 
  public function fetch_row($sql) {
    $rst = $this->dbh->query($sql);
    return $rst->fetch();
  }
}
?>

Our usage with the hybrid approach gives us three usages:

// Optioon 1 (Normal)
$dbh = new myDBH();
$rst = $dbh->fetch_row("Select * from `mytable`");
 
// Option 2 (Singleton)
$dbh = myDBH::getInstance();
$rst = $dbh->fetch_row("Select * from `mytable`");
 
// Option 3 (Singleton with Method Chaining Example)
$rst = myDBH::getInstance()->fetch_row("Select * from `mytable`");

Explanation of Our Changes

The first thing to note is the visibility differences with a hybrid solution:

  • The constructor remains public instead of private (this allows us to call our constructor normally and by getInstance())
  • The $dbh variable remains private (this, again, prevents ending children from overriding our dbh variable)
  • The db_info array changed from public to private (same reasoning as $dbh)

Our 2 new methods still remain:

  • public static $instance – holds the actual instantiated object of our myDBH class
  • public static getInstance() – This method first checks to determine if $instance is null. If it is, it will attempt to create an instance of myDBH and store it inside this variable. Finally, we return self::$instance (this is also done when self::$instance is not null!).

With the hybrid solution, we can use the class via getInstance or by calling it normally. However, this approach should be used only with the idea that deprecated implementations of our myDBH class exist only temporarily until we can upgrade all applications to use the singleton approach. All future implemenations should be using the singleton approach. Once all applications have been successfully upgraded to use the Pure Singleton usage, the class should also be updated to reflect this by changing the visibility of the object’s properties (explained above in “Pure Singleton”).

At this point, we now have what is called a “singleton object”.  We should also have a firm understanding of what a singleton really is.

Conclusion

I hope by now, you have a good, concrete understanding of what a “singleton pattern” is, and how it can be created.  Realize that a singleton is not always the best solution, nor is it the only pattern available for OOP.

Singleton’s should be created when an object is used over and over with no affect to applications that use it when the object’s environment changes.  To determine this, just ask yourself the following question: “If I create a variable (let’s call it $obj) to hold an instance of my class, and then I use that same variable with a totally different application on the same page , will anything break?”.  If the answer is no, then you may be a good candidate for a singleton pattern.

Singleton’s should not be used when you have a class that is state-dependent.  What this means is, if your object can hold values that are specific to that instance, then you should not use a singleton approach.  If this is not very clear, please ask and I’ll see if I can reword this or give some examples.

Finally, in order to encourage the idea of searching out other alternatives, I would recommend visiting Wikipedia’s Design Patterns.  These patterns are usually explained with languages other than PHP, but this is a good thing!   OOP is OOP – regardless as to what language it is used in.  The syntax may be different but the general concept will remain.  The point is to encourage searching out how OOP is being used, thereby reducing redundant discovery into these areas.

Good luck!

Resources

Special Thanks

A special thanks goes out to Chris Cember for spending the time to give me some really helpful pointers to making this a more structured article.  Also, thanks to William Wade for his suggestions (along with Chris’) to see examples of the hybrid approach.

14 thoughts on “PHP Singleton Pattern – OOP

  1. Mike Fleisher

    Well done! The singleton pattern is often mysterious to “newbs”, but after you get the hang of it it turns out to be incredibly useful in Object Oriented Programming.

    We’re professional PHP developers as well. Great that you find the time to post such useful articles for the community.

    Reply
  2. Matt

    Great tutorial! I’m really glad I came across this one. I’m trying to shift my mode of thinking towards OOP and this really helped!

    Reply
  3. Steve Buzonas

    The Singleton pattern is one of my favorite to use in PHP.

    I disagree with the subject choice for the example. In the multiple examples you had listed user management system, navigation system, page manager, advertisement application, schedule application; your advertisement database may be hosted in a 3rd party marketing corp’s server, your users may share a global database throughout all of your sites, and your page manager may use a different table based on current state. This defeats the effort put forth in creating a Singleton pattern for your DB class.

    More suitable usage for the Singleton pattern would be scenarios where your systems do not depend on the existence of the Singleton, or when there is and always will be exactly only one possible implementation of an object. Loggers falling into the first category, and users or sessions falling into the second are more suitable uses for the Singleton pattern in PHP.

    Reply
  4. Jonathon Hibbard Post author

    Steve, Thanks for the input!

    In situations where you have multiple possible database connections, you could still use the singleton solution but it would absolutely be different in situations like the one that you’ve presented.

    In situations where you have the possibility of multiple database connections, a singleton solution is still possible.

    Realize, there are MULTIPLE other possibilities. The example I’m going to give (I do not believe it is the best) is only an example – it is not the only way…

    The example strategy would be to have an array private to MyDBH that tracks all instances, and update MyDBH’s getInstnace method to accept the name of the db connection you want to create. In order to do this, either you’d have to define every database conection’s credentials in the MyDBH class, create a separate class that the MyDBH extends for storing DB configuration, create a configuration file that can be included to obtain an instances’s credentials, or create an object that stores this sort of information.

    This example just creates a dbconnection factory because I’m trying to keep this short and sweet (and also show the factory pattern at the same time):
    < ?php
    // using the "i" prefix to indicate its an interface...
    interface iDbConnection {
    public function getDbInfo();
    // anything else...
    }

    // Could make this abstract too obviously....
    class DbConnectionFactory {
    // just an example for factory as well...
    public static function getInstance($db_connection) {
    if(class_exists($db_connection) && method_exists()) {
    $dbConnection = new $db_connection;
    if(!($dbConnection instanceof iDbConnection)) {
    throw new Exception("Invalid DB Connection Requested!");
    }
    // do whatever
    return $dbConnection->getDbInfo();
    }
    }
    }

    class AdvertisementDbConnection implements iDbConnection {
    private $db_info = array(“host” => “localhost”, “dbname” => “myAdvertisementDB”, “username” => “myAdvertisementDB_user”, “password” => “myAdvertisementDB_password”);

    // whatever else

    public function getDbInfo() {
    return $this->db_info;
    }
    }

    class MyDBH {
    // changed this to an array…
    private static $instances = array();

    // …….
    //
    // same logic/methods/properties from above…
    //
    // …….

    public static function getInstance($db_connection = ‘default’) {
    // keep it all consistent…
    $db_connection = strtolower($db_connection);
    if(!isset(self::$instances[$db_connection])) {
    $db_info = (strtolower($db_connection) == ‘default’ ? null : DbConnectionFactory::getInstance($db_connection));
    self::$instances[$db_connection] = new MyDBH($db_info);
    }
    return self::$instances[$db_connection];
    }
    }

    // same ole, same ole..
    $dbh = MyDBH::getInstance();

    // get advertisement
    $dbh = MyDBH::getInstance(‘AdvertisementDbConnection’);

    While crude, this shows a basic example.. You could improve this in many, many different ways. Up to you how to do this.

    Also, I’d like to point out that multiple user’s on a page does not affect the singleton since PHP creates a new thread per user that accesses your page. In other words, the logic that checks for “current state” to display a page, and will still use everything you would normally expect.

    Note: I didn’t actually test the above so feel free to make improvements where needed!

    Reply
  5. A Boy Named Su

    Not bad, I’m on a phone so will keep this brief and apologize if somebody has already mentioned this in a comment, but the beauty of design patterns, like the singleton, is often marred by the static calls people use with them.

    This puts them in such a similar category to global variables.

    The solution would be so much more elegant if used in conjunction with a factory object.

    Reply
  6. Michael Newton

    I wonder if anyone’s still watching this post?
    I can certainly see the value in design patterns such as these, but have a hard time wrapping my head around how to effectively use them in a web-based environment, as opposed to a desktop one. Essentially each HTTP request is a different run of the program, so where is the value in, for example, using a singleton database connector as opposed to just opening the database connection at an early stage in page load, and then reusing that variable?

    Reply
    1. Jhourlad Estrella

      Hi Michael, in OOP we have a concept called “encapsulation” and as the term OOP itself implies we seldom work with variables having literal values but primarily with objects. We usually avoid $variable = “value” on the global scope, instead we implement encapsulation such as $object->getValue(). I can’t really expound on it more. I am paid by the hour :) I suggest you do your own research. the truth is out there XDXDXD

      Reply
  7. David Zentgraf

    I’ll agree with the boy named su: While the premise of the article is correct (don’t re-instantiate your objects every time you use them), the conclusion falls short. Making a static method call to get an object to then interact with the object is very half-baked OOP. You may as well just keep using that static method call the whole way through.

    The problem is that static method calls create code coupling, which throws you right back into procedural territory. OOP applied properly has the advantage of making your code extremely modular, extensible and testable. You’re undermining that with static calls. See http://kunststube.net/static/ for an in-depth discussion about that. The full answer is Dependency Injection.

    Reply
  8. Jonathon Hibbard Post author

    That’s the beauty of OOP in general in that there are many, many ways to skin a cat. I guess I was just trying to give a brief example of a Singleton and hopefully open the eyes to PHP developers who may not realize that, well, there is much more to OOP than < ?php class iCreatedAClass {} ?> :)

    Well written and great link though David!

    Reply
  9. Domenic Fiore

    Hey Jonathon,

    Great article, it opened my eyes a bit more to the Singleton pattern, and design patterns in general. Still need to do some more research, but I appreciate your efforts. I thought I’d let you know that your link to the design patterns in the PHP manual is broken. I don’t think this is your fault, I can’t seem to find the page you’re trying to link to in English. Here’s the German version, though :P http://php.net/manual/de/language.oop5.patterns.php

    Reply
  10. Jonathon Hibbard Post author

    Hey Domenic,

    Thanks man. Looks like it is down completely too. The dutch version seems to work (http://php.net/manual/de/language.oop5.patterns.php) but the english version of the same link gives a nice fat 404 fnf (http://www.php.net/manual/en/language.oop5.patterns.php) heh.

    If you’re using Google Chrome, it will translate the dutch version for you at least. There are better resources , though, out there.

    Here is perhaps the best resource on the subjective I’ve seen : http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/

    Granted, the topic is for Javascript Design patterns – but it is an awesome resource for getting to know the schematics of OOP design patterns (I know, I know…javascript and OOP doesn’t exactly sound like it mixes – but it does and can).

    Thanks again!

    Reply
  11. Phil

    I’ve read so much about how ‘Singletons are evil’ that I started getting paranoid about an app framework that I’d written for a site that relies upon a Singleton core object to maintain a single database and the core site rendering code. By using getInstance anywhere in the site’s code I am always able to access the core object, its site object and the database connection. As my application does not make multiple database queries concurrently and the only persistent data is the user class, I’ve found this is a really intuitive way to work.

    The earlier version of the app using dependency injection for the core objects but I found this to be an exceptionally cumbersome way to work and whilst I understand the unit testing implications of using my new Singleton structure, I’ve found it to be a pleasure to work with and extend and I feel the code is much more readable as a result.

    The biggest problem that I’ve encountered is that the IDEs I’ve tried using seem incapable of parsing a Singleton object chain ( e.g. $core->site->mySiteFunction(); ) so code hints don’t work.

    Like you, I acknowledge that the way I’ve built the app isn’t the only way it could have been written or the best way but it’s a way that works for the specific project.

    Reply
  12. Jonathon Hibbard Post author

    Well put Phil. I think we’ve both read the same articles relating to this pattern. It took me a long time to realize that the negative feeling towards the pattern stemmed from the fact that the singleton pattern represented an object that sole purpose in life was to do what most of the gray-beards cringe about: allocating space without an “any time soon” approach to release itself.

    The singleton pattern kind of makes memory-management extremists foam at the mouth because of this and, in the process. To me, if I was planning on creating a singleton just to create one – I would understand this sort of thing. However, a singleton can always be deallocated from memory, and it should be once the developer’s dependancies on the object to begin with are fully met.

    Long story short – as long as you are responsible about how you manage an object (no matter what pattern you apply to the methodology) and use the right pattern for the right problem – I say go for it. I never saw a negative impact from using a singleton, but then again that’s one of the great benefits of being in PHP where memory management / garbage collection is handled for free for you.

    Thanks Phil!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>