Implementing the Singleton Pattern in PHP 5 - Page 2

Develop a Singleton class in Front of Other Classes

A Singleton class can act as a point access for a set of common classes to provide a single instance for each class. Normally, each class can have more than one instance if it is instantiated directly, but it has only one instance if it is instantiated indirectly through the Singleton class. To accomplish this task, the Singleton class will store an array of instances, and each time a new instance is requested, it checks the array for it first. If the requested instance is not found in the array, then a new instance is created and stored in the array for further requests. This class is listed below:

Develop a Singleton Class for Database Connection

A classic task done with Singleton classes is providing resources access through a single instance. For example, a database connection is exactly the type of task that can be done through a Singleton class: you should have one instance communicate with a database because it helps you maintain only one connection instance instead of multiple ones, which might otherwise be resource intensive.

Based on the Singleton template described in the first section of this article, you can write a Singleton class for obtaining database connections, like this:

The output of this test depends on the database you use. Therefore, no sample output is provided here.

What Is the Risk of Using the Singleton Pattern?

Well, as the saying goes, "too much of anything is never good!" This rule is applicable to the Singleton pattern also, because too much of it will definitely generate some performance issues. So, be careful not to abuse it. Another concern is unit testing, because you will test single objects.

Because these are known risks, you can easily fix them in your applications, but there is one more risk that depends only on programmers and is directly related to their programming expertise and experience. Usually, when inexperienced developers use patterns (not necessarily this one), it leads to performance issues instead of a powerful and robust applications. Therefore, be careful how you integrate design patterns in your applications. Sometimes, assistance from a software architect may be necessary to achieve your desired results.

Conclusion

In this article you saw how to implement the Singleton pattern using PHP 5 capabilities. In future articles, I plan to show the other three patterns from this Gang of Four.

About the Author

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies: XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests. to e-mail her.