Home arrow PHP arrow Page 2 - The Singleton and Factory Patterns in PHP: Building object-oriented forms

When one is better than many: a quick look at the Singleton Pattern - PHP

Experienced PHP programmers know that web development problems are often tackled by using widely known design patterns within the context of an application. This article is the first in a series that will demonstrate how the Singleton and Factory patterns can be implemented in a real-world application.

  1. The Singleton and Factory Patterns in PHP: Building object-oriented forms
  2. When one is better than many: a quick look at the Singleton Pattern
  3. Object-oriented forms: applying the Factory pattern to a real application
  4. The first approximation to object-based forms: building form element classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 31
November 02, 2005

print this article



The first step involved in the development of object-based forms consists of learning a little about the Singleton Pattern, because I'll use it within the form generation process.

Probably, this pattern is one of the easiest ones to understand. To clearly explain its definition, let's describe a scenario which developers are frequently confronted with: one object is needed across an application by other objects for doing their work.

Normally, in this situation, it's best to have a single instance of the required object for accessing within the application, instead of having multiple instances of it. To cite a common case, a single instance of a database connection object is required by different objects for them to have database connectivity in their scope.

That's where the Singleton pattern comes in. Why trouble the application with multiple instances of an object, when a single instance is preferred?

Usually, in PHP a class is turned into a Singleton by defining a static method "getInstance()" or even another indicative name, which when called, returns a single instance of the object. Subsequent calls to the method will always return a handle to the object instance.

To clarify things, let's see an example based on the documentation provided in the PHP manual:

class Singleton{
// $instance holds a single instance of the object private static $instance; // private constructor private function __construct(){} // the getInstance() method returns a single instance of the object public static function getInstance(){ if(!isset(self::$instance)){ $object= __CLASS__; self::$instance=new $object; } return self::$instance; } public function displayMessage(){ echo 'I am a Singleton'; } } $singleton=Singleton::getInstance(); //
return a single instance of the object $singleton->displayMessage();// display message

The above class represents a simplified version of a Singleton. Notice the use of the static method "getInstance()", which when invoked the first time, returns a single instance of the object. As I said before, recurrent calls to the method will always return a handle to the object. 

 As you'll probably agree, this pattern is fairly understandable. Since this is only a brief overview, further and more complex implementation will be left as an additional task. However, for the purposes of the series, the example is indeed very illustrative.

Now that we've demonstrated the functionality of the Singleton pattern, let's move one step toward the creation of object-based forms and explain the specific role of the Factory Pattern within the developing process.

>>> 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: