Home arrow PHP arrow Page 3 - Using Singletons with Factory Methods in PHP 5

Coding a Singleton method - PHP

In this second installment of a six-part series on implementing the factory pattern in PHP 5, I explain how to build an improved version of a factory class that returns Singletons of itself to client code.

TABLE OF CONTENTS:
  1. Using Singletons with Factory Methods in PHP 5
  2. Review: building a basic factory class with PHP 5
  3. Coding a Singleton method
  4. The enhanced factory class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 01, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Definitely, one of the simplest way to implement the Singleton pattern inside the example form element factory class that you saw before is to add a static method, usually called “getInstance()” (or something similar) that takes care of returning to client code only one instance of it. Period.

To demonstrate in detail how this static method can be coded in a few simple steps, below I reintroduced the definition of the factory class, which this time includes the pertinent “getInstance()” method. Take a look at it, please:

class FormElementFactory

{

private static $instance = NULL;

 

// get Singleton instance of form element factory class

public static function getInstance()

{

if (self::$instance === NULL)

{

self::$instance = new FormElementFactory;

}

return self::$instance;

}

public function factory($formElement, $attributes = array())

{

if ($formElement === 'textbox' or $formElement === 'textarea')

{

return new $formElement($attributes);

}

}

}

That wasn’t rocket science, wasn’t it? As you can see, now the “FormElementFactory” class implements the Singleton pattern through its “getInstance()” method, which internally uses a $instance static property, to return only a single instance of the class.

While I’m still far from defining a truly effective factory method, it’s fair to say that the incorporation of the previous “getInstance()” Singleton is a considerable improvement for avoiding issues related to multiple instances.

Well, at this stage I’m pretty sure that you understand the purpose of coding the static “getInstance()” method. However, the missing part here is the lack of an example that shows how to use this method in conjunction with the already familiar “factory()” to create some form element objects.

In the last segment of this tutorial I’m going to code that example, so you can see in action the improved version of the sample “FormElementFactory” class.

Please click on the link that appears below and read the final section. 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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