Home arrow PHP arrow Page 3 - Creating a Static Web Form Helper with Restrictive Constructors

Building a static web form helper class with a private constructor - PHP

Welcome to the final installment of a series that shows you how to use restrictive constructors in PHP 5. With a decent variety of code samples, this series recreates for you a few common scenarios where the implementation of both protected and private constructors can greatly improve the behavior of different classes in the context of a given application.

TABLE OF CONTENTS:
  1. Creating a Static Web Form Helper with Restrictive Constructors
  2. Review: using a private constructor in a Singleton class
  3. Building a static web form helper class with a private constructor
  4. Using the previous Form helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 23, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the segment that you just read, thereís another scenario where a private constructor can be truly useful -- when building a class that must be used out of the object context, or expressed in other words, as a solely static class.

To demonstrate this concept, in the lines to come Iím going to code a presentational helper, which will be tasked with rendering different controls of an HTML form. Since there wonít be a real need to create instances of it, its originating class will have to be used only statically.

How will this be accomplished? Yes, you guessed right! By declaring its constructor private and its discrete methods static, the helper will be turned into a static class.

Now, take a look at the helperís definition, which is as follows:

<?php

 

 

class Form

{

  

    // private constructor

    private function __construct(){}

   

   // render <form> opening tag

   public static function open(array $attributes = array())

   { 

       $html = '<form';

       if (!empty($attributes))

       {

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

          {

              if (in_array($attribute, array('action', 'method', 'id', 'class', 'enctype')) and !empty($value))

              {

                 // assign default value to 'method' attribute

                 if ($attribute === 'method' and ($value !== 'post' or $value !== 'get'))

                 {

                     $value = 'post';

                 }

                 $html .= ' ' . $attribute . '="' . $value . '"';

              }

          }

       }

       return $html . '>';

   }

  

   // render <input> tag

   public static function input(array $attributes = array())

   { 

       $html = '<input';

       if (!empty($attributes))

       {

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

          {

              if (in_array($attribute, array('type', 'id', 'class', 'name', 'value')) and !empty($value))

              {

                 $html .= ' ' . $attribute . '="' . $value . '"';

              }

          }

       }

       return $html . '>';

   }

  

   // render <textarea> tag

   public static function textarea(array $attributes = array())

   { 

       $html = '<textarea';

       $content = '';

       if (!empty($attributes))

       {

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

          {

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

              {

                 if ($attribute === 'value')

                 {

                     $content = $value;

                     continue;

                 }

                 $html .= ' ' . $attribute . '="' . $value . '"';

              }

          }

       }

       return $html . '>' . $content . '</textarea>';

   }

  

   // render </form> closing tag

   public static function close()

   {

       return '</form>';

   }

}// End Form class

Aside from implementing a few simple static methods that allow you to render common elements of a web form, such as input boxes, submit buttons and text areas, the previous ďFormĒ class declares its constructor private. In doing so, the class can only be used out of the instance context.

Having demonstrated yet another concrete use of a restrictive constructor in PHP 5 classes, itís time to give the previous helper a try, so you can see how it functions. Therefore, in the final section of this tutorial Iím going to set up an example for you, which will show the earlier ďFormĒ class in action.

Now, jump forward and read the next segment. Itís only one click away.



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