Home arrow PHP arrow Page 2 - Developing a Caching Class for an MVC-based Framework in PHP 5

Review: the previous HTML helper class - PHP

In this seventh part of the series, I add a cache class to the classes that comprise the MVC-based framework we're building. The new class uses the file system as the underlying backend for caching data, but itís also possible to create one that caches data on shared memory, a SQLite database and so forth. Indeed, numerous possibilities exist.

TABLE OF CONTENTS:
  1. Developing a Caching Class for an MVC-based Framework in PHP 5
  2. Review: the previous HTML helper class
  3. Building a cache class
  4. Determining cache validity
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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