Home arrow PHP arrow 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!

  1. Using the Observer Design Pattern with Static Data in PHP 5
  2. Handling user data via a single static property
  3. Using a static property with the Observer pattern
  4. Putting the data checking system to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
September 17, 2007

print this article



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(){
         return true;
      return false;

// define 'AlphaValidator' class
class AlphaValidator extends Observer{
   public function validate($data){
      if(!preg_match("/^[a-zA-Z ]+$/",$data)){

class NumberValidator extends Observer{
   public function validate($data){

class EmailValidator extends Observer{
   public function validate($data){

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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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