Home arrow PHP arrow Page 3 - Implementing Factory Methods in PHP 5

Defining factory methods - PHP

If you’ve ever developed desktop applications using mature object-oriented languages like C++ and Java, then it’s possible that you’ve found yourself saying a few funny phrases such as “I need to implement a factory method within this class,” or in the worst case: “this class needs to implement a factory method, but I don’t have a single clue about how to do that.” Surprisingly, you can implement these factory methods pretty quickly and easily in PHP 5, as this six-part series will show you.

TABLE OF CONTENTS:
  1. Implementing Factory Methods in PHP 5
  2. The simplest object constructor
  3. Defining factory methods
  4. Factoring web form element objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate how to implement a simple factory method in PHP 5, I’m going to define a basic class, which will be charged with returning to client code a few basic web form element objects. For this concrete example, these objects will be capable of generating all the HTML markup required for rendering input text boxes and text areas.

So, here are the definitions of the classes that are the blueprints for the mentioned web form element objects:

// 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 I mentioned before, the two sample classes shown above simply construct HTML input boxes and text areas, via their respective “render()” methods. On the other hand, their constructors act like simple setters for the attributes that will be appended to these web form elements.

Now that you've grasped how the previous classes function, it’s time to define the class responsible for creating form element objects. As you’ll see in a moment, this brand new class implements a method called “factory.” Its definition looks like this:

// define FormElementFactory class

class FormElementFactory

{

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

{

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

{

return new $formElement($attributes);

}

}

}

Certainly, there’s not much to be said about the above “FormElementFactory” class, except that it uses its factory method to create only two specific form element objects, text boxes and text areas. However, this is a great breakthrough, since it’s a simple example that shows how to define a factory method in PHP 5.

If you ever though this was a difficult process, then you should take a deep breath and relax, because in reality it’s simple. But, if you’re like me, then you'll want to see how the previous factory class can be used to display an input box and a text area on the browser.

This procedure will wrap up this introductory tutorial of the series. To learn more about it, go ahead and read the last segment.



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