Home arrow PHP arrow Using Singletons with Factory Methods in PHP 5

Using Singletons with Factory Methods in PHP 5

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.

  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



While this may sound like a clichť, the truth is that building factory methods in PHP 5 is a straightforward process that only requires an intermediate background in working with objects and classes. After all, a factory method is merely a regular class method that returns, when possible, one or more objects to client code -- or expressed in other terms, itís a concrete implementation of the Factory design pattern.

In reality, learning the basics of factory methods is one of those things that every PHP developer must tackle sooner or later. They're used very frequently in the development of object-oriented web applications, which makes it practically impossible not to deal directly with them at some point.

Of course, if you already went through the introductory part of this series, then you have a pretty clear idea of how to create factory methods using PHP 5. In that tutorial I demonstrated how to create a simple factory class which could spawn a couple of web form element objects for rendering on screen a few basic HTML input boxes and text areas.

However, this example factory class has a serious down side. In its current version itís necessary to create an instance of it to dynamically call its factory method, and therefore spawn the mentioned web form element objects. Actually, this is considered a bad thing in terms of programming habits, because in this particular case the goal of the factory class is to build other objects, other than its own..so whatís the point in dealing with instances of it? Thatís pretty useless.

To address only partially the issue described above, in this second chapter of the series Iím going to introduce some subtle changes into the definition of this factory class, so it can return Singletons of it to client code. This at least will make sure that only one instance of it will be created during the execution of a given script.

Now, letís and see how to translate the previous concepts to fully-functional PHP 5 code. Letís go!

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