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.
blog comments powered by Disqus |
|
|
|
|
|
|
|