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

Review: building a basic factory class with PHP 5 - 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

In the introduction, I mentioned that the factory class created in the preceding tutorial had a pretty annoying down side, since it was necessary to first create an instance of it to invoke its factory method. Below Iíve listed for you the source code of this class, along with an example that shows how to use it for creating some web form element objects.

First, here are the corresponding classes that render HTML input boxes and text areas. Take a look at them:

// define TextBox class

class TextBox

{

private $name = 'mytextbox';

private $id = 'textbox';

private $class = 'textbox';

private $maxlength = 20;

 

// constructor

public function __construct($attributes = array())

{

if (empty($attributes) === FALSE)

{

foreach ($attributes as $attribute => $value)

{

if (in_array($attribute, array('name', 'id', 'class', 'maxlength')) and empty($value) === FALSE)

{

$this->$attribute = $value;

}

}

}

}

 

// renders input text box element

public function render()

 {

return '<input type="text" name="' . $this->name . '" id="' . $this->id . '" class="' . $this->class . '" maxlength="' . $this->maxlength . '" />';

}

}

 

 

// define TextArea class

class TextArea

{

private $name = 'mytextarea';

private $id = 'textarea';

private $class = 'textarea';

private $rows = 10;

private $cols = 20;

 

// constructor

public function __construct($attributes = array())

{

if (empty($attributes) === FALSE)

{

foreach ($attributes as $attribute => $value)

{

if (in_array($attribute, array('name', 'id', 'class', 'rows', 'cols')) and empty($value) === FALSE)

{

$this->$attribute = $value;

}

}

}

}

 

// renders textarea element

public function render()

{

return '<textarea name="' . $this->name . '" id="' . $this->id . '" class="' . $this->class . '" rows="' . $this->rows . '" cols="' . $this->cols . '"></textarea>';

}

}

As seen above, the logic implemented by the ďTextBoxĒ and ďTextAreaĒ classes is quite simple to grasp; in both cases, they accept an array of attributes, which are used by their ďrender()Ē method to display the corresponding HTML elements of a web form. The workings of those classes are that simple, really. 

Now, itís time to show the definition of the factory class, which as I explained before, is responsible for creating both input text and text area objects for further manipulation. 

Hereís how this class was conceived originally:

// define FormElementFactory class

class FormElementFactory

{

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

{

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

{

return new $formElement($attributes);

}

}

}

Obviously, the above class is a wrapper for its factory method, which is the actual workhorse, since itís tasked with returning to client code the aforementioned web form element objects. As I pointed out earlier, though, while the factory method does its thing as expected, itís necessary (at least in theory) to create an instance of its originating class to use it.

The following script clearly represents this situation:

$formFactory = new FormElementFactory();

// create some form elements objects

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

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

// display form elements

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

Unquestionably, the form element factory does a decent job when it comes to creating input box and text area objects, but as seen above, this process requires spawning an instance of it first, which is pretty inefficient. Of course, itís valid to say that the classís factory method could have been called out of the object scope, that is statically, but this would mean getting ahead of myself.

For the moment, this factory method hasnít been declared static, so itís necessary to find a partial solution that permits us to create only a single instance of the previous factory class. But how can this be achieved? Well, I used the term ďsingleĒ on purpose, so you can pick up the solution very quickly!

Yes, in this case Iím talking about adding to the factory class a method that returns Singletons of it, thus letting us avoid dealing with multiple instances of the class. Sounds pretty interesting, right?

Thus, in the next section Iím going to show you how to incorporate this brand new method inside the previous factory class. Now, click on the link below and keep reading.



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