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

The enhanced factory class in action - 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
 

As I promised in the previous section, below I coded a final example that demonstrates how to use the improved version of the “FormElementFactory” class to create a couple of web form element objects. Here’s the example in question:

// create instance of FormElementFactory class using its 'getInstance()' method

$formFactory = FormElementFactory::getInstance();

// create some form elements objects

$texBox = $formFactory->factory('textbox');

$textArea = $formFactory->factory('textarea');

// display form elements

echo $texBox->render() . '<br />' . $textArea->render();

Definitely, the above code fragment looks much better than many of its predecessors. The static call to the “getInstance()” method of the factory class returns Singletons of it, which assures that only one instance of the class will be available across the entire script. The rest of the example remains the same, implying that the “factory()” method is again called dynamically, to spawn a couple of web form element objects.

While this example shows how to build a factory method that uses single instances of its originating class, it has plenty of room for improvement. If you’re anything like me, then it’s possible that you hear inside your head a little voice that whispers over and over again: "why not simply declare this factory method static?" Well, your inner voice is right! But, this process will be covered in detail in the next part of the series.

Meanwhile, feel free to tweak all of the code samples shown in this tutorial, so you can quickly start building your own factory methods with PHP 5.

Final thoughts

That’s all for the moment. In this second installment of the series, I explained how to build an improved version of a factory class that returned Singletons of itself to client code. While this approach permits you to avoid creating multiple instances of the class, the truth is that it can be implemented in a much more efficient manner.

If you closely analyze the definition of the class, then you’ll realize that it’s extremely easily to avoid its instantiation by simply declaring its “factory()” method static. So, in the next article I’m going to explore this solution in depth, in this way going one step further in building factory methods with PHP 5.

Don’t miss the upcoming tutorial!



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