Home arrow PHP arrow Page 4 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class

Polishing the Page - PHP

In this third part of the series, I will explain how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects. This will boost the functionality of the factory class by simplifying the process for rendering form elements.

TABLE OF CONTENTS:
  1. The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  2. The Factory pattern in a real application: a quick look at the “formElementFactory” class
  3. A smarter factory: applying Polymorphism within the “formElementFactory” class
  4. Polishing the Page
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
November 16, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Indeed, I can go one step further by polishing the form’s visual presentation, and define a sample class “Page”, which builds the basic structure of a web page. Doing so, we’re able to combine the functionality of this class along with “formElementFactory”, for a quick and dirty implementation. So, the definition for the simple “Page” class is the following:

class Page {
    private $output;
    public function __construct(){
        $this->output='<html><head><title>Default
Page</title></head><body><h1>Default Page</h1></body></html>';
    }
    public function makeHeader($header){
        $this->output=$header;
    }
    public function makeBody($body){
        $this->output.=$body;
    }
    public function makeFooter($footer){
        $this->output.=$footer;
    }
    public function getPage(){
      return $this->output;
    }
}

In simple terms, the above class presents the classic methods for building the header, body and footer sections of a web page, while its constructor sets up a default sample page. Definitely, I won’t stop explaining its logic. Instead, I’ll show an example that uses this class to generate a login form. Here is the sample code:

// instantiate a new Page object
$page=new Page();

// build header page section
$page->makeHeader('<html><head><title>Object-Based web
form</title></head><body>');

// add form tag and build a table header
$body='<form action='.$_SERVER['PHP_SELF'].'
method="post"><table><tr><td>Object-Based web form</td></tr>';

// build a text input box
$body.='<tr><td>User ID '.formElementFactory::createElement
('textinput',array('name'=>'userid','maxlength'=>'20')).'</td></tr>';

// build a password field
$body.='<tr><td>Password '.formElementFactory::createElement
('passwordinput',array('name'=>'passwrd','maxlength'=>'20')).
'</td></tr>';

// build a submit button
$body.='<tr><td>'.formElementFactory::createElement
('submitbutton',array
('name'=>'send','value'=>'Log in')).'</td></tr>';
$body.='</table></form>';

// add the code to the body section
$page->makeBody($body);

// build the footer page section
$page->makeFooter('</body></html>');

// display the page
echo $page->getPage();

The above code uses the “Page” class to generate the structure of a web page, by displaying the header, body, and footer parts. Notice the rough usage of the “formElementFactory” class to obtain the code for the required form elements, by decoupling object instantiation from the general form rendering process.

The only point worth mentioning on the above example is the extensive use of the static “createElement()” method, which is naturally invoked without the need to explicitly instantiate an object from the form element factory class. As you remember, this class has been declared as abstract, therefore any attempt to instantiate an object from it will result in the PHP interpreter triggering a fatal error.

Despite the fact that I’ve hidden behind the structure of the factory class all of the required processing for displaying form elements, I’m still far away from having an efficient rendering mechanism. Notice that the rough use of the “Page” class is merely an example, not suitable for use in a fully functional application.

However, the factory class works quite well, by implementing the Factory pattern, for solving annoying tasks involved in displaying programmatically web form elements. Now, I’ve exposed a common problem that comes up when building real-world applications that can be addressed in an effective way, in this case by applying a design pattern.

Wrapping up

That’s all about it for now. In this third part of the series, I’ve explained how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects to boost the functionality of the factory class, by simplifying the process for rendering form elements.

Now, let’s pay attention to the next part of the series, where I’ll explain the correct application of the Singleton pattern, in order to work with only a single instance of a form factory class. As you’ll see in short, making a class a Singleton is quite easy to do, allowing you to write robust PHP4 programs, by reducing the hassles related to manipulating multiple object instances.



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