A template class is a base class with some special abilities. First, it concretely implements one or more algorithms; second, it can determine which subclasses should use these algorithms. This article is the first part of a two-part series that introduces you to the basic concepts of this design pattern.
Working with the object-oriented paradigm in PHP -- and of course, other programming languages -- implies having at least a basic understanding of its key concepts. A developer using this paradigm knows the intrinsic definition of a class, its properties and methods, and logically possesses a decent knowledge of the so-called big pillars of OOP, such as inheritance and polymorphism.
Naturally, if all of these important concepts are familiar to you, I should assume that youíve been building your own PHP classes for a while, and that very often you find yourself creating a class API that hides all the complexity required for performing a number of predefined tasks.
However, there are times when a specific object-based PHP application needs to execute extremely complex processes. In most cases, these processes are encapsulated inside one or more methods of a class (or sometimes in many of them).
While this situation happens very frequently, itís rather unusual to create classes that implement specific algorithms on top of the corresponding hierarchy. Most of the time, base classes only define the basic functionality of a group of objects, and the respective sub classes are responsible for implementing the business logic required to achieve specific tasks.
So, in which cases does the above situation occur? Well, at times itís necessary to define a base class that concretely implements one or more algorithms. Additionally, this class may need to be capable of determining which subclasses should use these algorithms. In pattern-based jargon, this class is called a ďtemplateĒ and not surprisingly, it determines the programming model imposed by the homonymous pattern.
Indeed, template classes can be very interesting within the vast area of pattern-oriented programming. Over the course of this series, Iíll demonstrate how to implement this rather unusual design pattern by accompanying the corresponding theory with copious hands-on examples.
Hopefully this experience will be instructive. Let's not waste more time in preliminaries; click on the link below to begin learning more about building template classes with PHP 5. Letís get started!