HomePHP Using the Observer Design Pattern with Static Data in PHP 5
Using the Observer Design Pattern with Static Data in PHP 5
In the vast terrain of object-oriented programming with PHP 5, working with static data members is an approach that provides developers with the capacity for building classes that are callable from outside the object context. It also lets them define properties that are shared by all the instances of those classes. If you're interested in learning how to put this powerful feature to work for you, then you should start reading this series of articles now!
Welcome to the last tutorial of the series "Handling Static Data with PHP 5." By using a step-by-step guide, this series walks you through working with static properties and methods in PHP 5, and also shows you how to combine this static data with some of the most popular design patterns, such as "Factory" and "Observer."
In the previous article, I went through the development of a basic example to demonstrate how the definition of a unique static property within a given class can facilitate the enchaining of different objects that belong to the class.
This rather primitive example showed how a bunch of user-related objects could increment the value of a specified static property each time a new one was properly instantiated. It demonstrated the principle that stands behind defining a class property as static. It should also be noticed that this basic example can be used as a starting point to develop more complex PHP applications that also use static data, since the core concepts remain the same, regardless of the size of the project being developed.
However, as I said before, defining a simple static property within a given PHP class to chain its different instances might seem extremely basic, not to mention that it's hard to find a concrete application of this approach in the real world. Thus, keeping in mind this idea, in this last installment of the series I'll show you how to use a static property, along with the programmatic model dictated by the observer pattern, to implement an expandable data validation system. This task might have an immediate application in a real situation.
Thus, having introduced the subject of this final part of the series, let's move on together and see how to build the aforementioned data validation mechanism using a unique static property. Let's get started!