Introduction This is the final part of the series “The Singleton and Factory Patterns in PHP.” IN the previous article, I showed how the “formElementFactory” class may be turned into a Singleton, useful for working with a single object instance, when a program must be executed within a PHP 4 programming platform. Also, I highlighted the advantages of using the Factory pattern to simplify tasks related to building regular web forms, by using an object-oriented approach, instead of coding them through the traditional way. Of course, the major benefit of utilizing the power of the object oriented paradigm is that once all of the form classes have been defined, generating forms within a web page is a matter of instantiating the required form objects through a factory class, and then deciding the best layout for the elements. Stepping back to the previous part of the series, I’ve gone so far as to implement the above mentioned patterns on building object-based forms, without paying strong attention to the control of form element layout. Certainly, I wouldn’t be fully exploiting the advantages inherent in application design and code portability described throughout the series, if visual presentation is not included. So, keeping in mind that the layout of form elements plays a relevant role within the overall development process, in this last part of the series, I’ll encapsulate the logic to generate web forms, by defining a form generator class, which will implement the form element factory together with all of the required form element classes. With the formalities out of the way, let’s get started. Object-based form generation: defining a form generator class A natural place to start building object-based forms within a web page is by defining the form generator class itself. As one would expect, this class will hide the internal processing involved in instantiating form objects and expose some methods aimed specifically at giving the form a proper visual presentation. Here is what it looks like: class formGenerator{ Now that I’ve listed the source code forming the brand new “formGenerator” class, let’s break down the code to understand each relevant method. As you can see, the class exposes the most common data members associated with a regular web form, such as “$name”, “$action”, and “$method” attributes, along with other core properties that are used to properly render form elements. The constructor accepts a single “elements” associative array, which will be passed internally to the static method “createElement()”, in order to instantiate form objects and return their corresponding (X)HTML markup. Certainly, after the initialization tasks are performed by the constructor, the most important method within the class is “createForm()”, since it’s responsible for generating the code that renders each form element defined from outside the class. Its source code is listed below: // create form code Basically, the above method builds the complete form’s code by applying the following logic: first, the opening <form> tag together with its properties are created, and second, the generation of form elements is carried out by the “formElementFactory” class. As you remember, this class was defined as abstract, so the call to its method “createElement()” is performed outside its object context. The line below shows how the markup for each element is appended to the general output: $this->output.=$this- Notice that aside from adding the element’s code, two additional properties, “elementHeader” and “elementFooter” are appended to the overall form’s output. The reason for adding these properties is simply to provide the class with the ability to pre-append and post-append additional HTML tags to the form element itself, allowing it to include dynamically presentational markup during the form generation process. If this sounds rather confusing, don’t be concerned. The function of each property will become clear when I set up a functional example. The next step in explaining how the form generator works is to analyze the rest of class methods. So, let’s jump to the few next lines to learn more about them.
blog comments powered by Disqus |
|
|
|
|
|
|
|