HomePHP Page 3 - Using the Observer Design Pattern with Static Data in PHP 5
Using a static property with the Observer pattern - PHP
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!
According to the concepts expressed in the previous section, my purpose here is simply to illustrate how a static property can be used to tackle a real world problem. I'm going to build a basic data validation mechanism for verifying common input types, like alphabetic and numeric data, as well as email addresses.
Nonetheless, and here is where things get really interesting, the core logic of the system described above will be implemented via an observer class. As you might guess, this class will use a static property to control the validity of all the inputted data.
Now that I have explained how this validation system will work, please take a look at the definition of the following classes, which are responsible for the correct implementation of the system in question:
// define 'Observer' class class Observer{ static public $errors=0; static public function checkData(){ if(!self::$errors){ return true; } return false; } }
// define 'AlphaValidator' class class AlphaValidator extends Observer{ public function validate($data){ if(!preg_match("/^[a-zA-Z ]+$/",$data)){ self::$errors++; } } }
class NumberValidator extends Observer{ public function validate($data){ if(!is_numeric($data)){ self::$errors++; } } }
class EmailValidator extends Observer{ public function validate($data){ if(!preg_match("/^.+@.+..+$/",$data)){ self::$errors++; } } }
As show above, I coded some basic PHP classes, which in conjunction implement the so-called observer design pattern. Logically, in this particular case, the functionality provided by the pattern is utilized to validate basic types of input data. This process is performed by a bunch of specific data checking classes.
However, you should notice that on top of this validation system, there's an observer class that uses a static property called "$errors" to determine whether any failures occurred when validating specific types of data. Of course, since this property is also shared by all the validation classes, its value is incremented each time a specific input data is considered invalid. In this case it "notifies the observer" that an error was triggered when validating a certain input. Pretty simple to grasp, right?
All right, having explained the programming logic that drives the previous data checking system, it's time to move on and code an illustrative example. In this final example, all of the above classes will be put to work in conjunction, in this manner showing the actual functionality of the system.
As you might guess, this practical demonstration will be done in the following section, thus click on the below link and keep reading.