Home arrow PHP arrow Page 4 - Using the Observer Design Pattern with Static Data in PHP 5

Putting the data checking system to work - 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!

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the section that you just read, below I coded several hands-on examples that illustrate how the data validation system that you learned earlier works to check some basic input data.

Here are the mentioned examples, along with their corresponding outputs:

try{
   // create observer object
   $observer=new Observer;

   // create validator objects
   $alphaVal=new AlphaValidator;
   $numVal=new NumberValidator;
   $emailVal=new EmailValidator;

   // validate some basic input data
   $alphaVal->validate('This is a valid input string');
   $numVal->validate('This is not a number');
   $emailVal->validate('user@domain.com');
   if(!Observer::checkData()){
      echo 'Inputted data is not valid';
   }
   else{
      echo 'Inputted data is valid';
   }

   // displays the following:
   // Inputted data is not valid
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

//*******************************************
try{
   // create observer object
   $observer=new Observer;

   // create validator objects
   $alphaVal=new AlphaValidator;
   $numVal=new NumberValidator;
   $emailVal=new EmailValidator;

   // validate some basic input data
   $alphaVal->validate('This is a valid input string');
   $numVal->validate(1);
   $emailVal->validate('user@domain');
   if(!Observer::checkData()){
      echo 'Inputted data is not valid';
   }
   else{
      echo 'Inputted data is valid';
   }

   // displays the following:
   // Inputted data is not valid
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

//*******************************************
try{
   // create observer object
   $observer=new Observer;

   // create validator objects
   $alphaVal=new AlphaValidator;
   $numVal=new NumberValidator;
   $emailVal=new EmailValidator;

   // validate some basic input data
   $alphaVal->validate('1 is a number');
   $numVal->validate(1234);
   $emailVal->validate('user@domain.com');
   if(!Observer::checkData()){
      echo 'Inputted data is not valid';
   }
   else{
      echo 'Inputted data is valid';
   }

   // displays the following:
   // Inputted data is not valid
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

//*******************************************
try{
   // create observer object
   $observer=new Observer;

   // create validator objects
   $alphaVal=new AlphaValidator;
   $numVal=new NumberValidator;
   $emailVal=new EmailValidator;

   // validate some basic input data
   $alphaVal->validate('S');
   $numVal->validate(1234);
   $emailVal->validate('user@domain.com');
   if(!Observer::checkData()){
      echo 'Inputted data is not valid';
   }
   else{
      echo 'Inputted data is valid';
   }

   // displays the following:
   // Inputted data is valid
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Weren't all the above code samples easy to follow? I'm sure they were. As you can see, the previous data checking mechanism uses the programmatic model imposed by the observer pattern, along with a single static property to validate different types of inputted data. Considering that coding the prior classes may take only a few minutes, and the system's capacity to be expanded upon, you'll have to agree with me that using static properties and methods with PHP 5 brings many benefits. 

Final thoughts

Sadly, we've come to the end of this series. Hopefully, after reading all these tutorials, you'll be better prepared to start using static data with your own PHP-driven applications.

See you in the next PHP tutorial!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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