Just in case you haven't read the previous tutorial, where I finished building the HTML form helper class, I listed its full source code below. Essentially, the file containing the definition of this basic form helper was created like this: (Form.php) <?php class Form { private static $instance = NULL;
// get Singleton instance of Form class public static function getInstance() { if (self::$instance === NULL) { self::$instance = new self; } return self::$instance; }
// render <form> opening tag public static function open(array $attributes) { $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) { $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) { $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 It’s easy to see the simplicity of the internal functioning of this HTML form helper class. Basically, the class is comprised of a few static methods that can be used for rendering different elements of a web form, such as its opening and closing tags, and different input controls and text areas. Under normal conditions, the helper’s methods will be called out of the object context since they’ve been declared static. Even so, I decided to code an additional method that returns a Singleton instance of it, in case you want to add some non-static methods on your own. At this stage you should understand how the previous helper class does its thing, so it's time to add more classes to this sample framework. In the following section I’m going to build a basic cache class that will enable the framework to cache database result sets and HTML output via the file system. Want to learn the full details regarding the development of this cache class? Click on the link below and read the next segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|