Building a Model Class for the HMVC Design Pattern
(Page 1 of 4 )
In this fifth part of a nine-part series, I will add to our example framework a generic model class, which will allow us to implement a fully-working HMVC schema in future applications. The method of this class that fetches all of the rows from a specified MySQL table also caches this data. If this approach doesn’t fit your requirements, feel free to modify the method and tweak it at will.
Learning the key concepts that surround the implementation of the Hierarchical Model-View-Controller design pattern (a long and elegant name for the HMVC acronym) can be of great help when developing modular and more scalable web applications in PHP 5. This pattern allows you to easily split programs into smaller, loosely-coupled MVC modules, commonly known as MVC triads.
As with many other topics that fall under the heading of PHP-based web development, there are many approaches one can follow to learn the basics of the HMVC paradigm. In this series of articles you’ll find a guide that will show you how to use the pattern by progressively developing a simple framework made up of a number of basic, yet functional classes.
And now that I've mentioned the term “classes,” it’s time to review the topics that were covered in the last tutorial. In that part of the series I added to this example framework a couple of classes aimed at filtering input data and helping in the generation of HTML forms. This latter was a simple helper that permitted you to easily render typical form elements, including input controls and text areas.
While coding both a form helper and an input sanitization class contributes to expanding the current functionality of the framework, it’s also important to create a model class. This will help demonstrate the actual power that stands behind the HMVC pattern.
Therefore, in the next few lines I’m going to built a pretty generic model. It will allow you to perform CRUD operations against any selected MySQL database table, as in this particular case, this RDBMS will be the default storage mechanism used by the framework.
Now, let’s start coding the model class. It’s going to be an educational experience, trust me!