PHP
  Home arrow PHP arrow An Introduction to the Observer Pattern in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

An Introduction to the Observer Pattern in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2006-07-17


    Table of Contents:
  • An Introduction to the Observer Pattern in PHP
  • Building a practical example
  • Adding a basic error logger
  • Applying the Observer design pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    An Introduction to the Observer Pattern in PHP
    ( Page 1 of 4 )

    If you have reached the point in your programming life where you are using design patterns, you will want to read this article. The first of a three-part series, it covers the Observer pattern, which can be just the thing for situations where objects need to send information to a centralized mechanism.

    Introduction

    Design patterns can be hard to grasp sometimes. They're a fundamental part of advanced software development, and possibly this fact makes them even more intimidating to those developers starting to taste their real power. However, many things in software programming aren't as difficult as they seem at first glance, and that applies to learning design patterns as well.

    True to form, over the development of numerous PHP applications, quite possibly you've been using some kind of design pattern, either because you needed to solve a specific problem by applying a well-known, proven solution, or simply due to your good coding habits. In either case, the good news is that you can become much more familiar with design patterns, as they can meet your programming requirements better than you may have initially thought.

    Beyond the bunch of tongue-twisting buzzwords that "Singleton," "Factory" or "Iterator" can be, the truth is that one -- or all of them -- have already been part of your productive developer life.

    Certainly, among the most popular design patterns, there's one that I think is especially interesting, particularly if you're used to writing a lot of object-oriented Web applications. In this case I'm speaking of the Observer pattern. Indeed, this pattern can be extremely useful in situations where a certain number of objects (or components) must have a clearly-delimited area of responsibility inside an application, and at the same time, need to send information to a centralized mechanism about all their changes occurring at runtime.

    This is very convenient in many situations. It is especially useful when it comes to working with Web-based user interfaces and system loggers. In these cases there are different independent objects that need to reflect any eventual changes by notifying a core module, which will take a course of action in accordance with the relevance of these modifications.

    In the examples that I mentioned before, the core module "observes" the distinct objects and decides what course of action must be taken, in response to the different changes reflected by the observed objects. In all the cases, the objects in question are only limited to performing their specific tasks, and are not responsible for making decisions beyond their scope. This facilitates their decoupling from the rest of the application.

    Okay, after reading this simple introduction, I hope you'll be wondering how all this boring theory can be translated into functional PHP code. That's what I'm going to do in the course of this article, by introducing some friendly examples of how to implement the Observer pattern in PHP applications.

    Are you ready? Let's get started.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT