Adobe Air Adobe Flash CS4 Flex Flash Builder Flash Catalyst

Contact me at : hello[at]gauravjassal[dot]com

Webdevelopment Blog



Observer pattern in PHP part 2
November 9,2009 at 12:18 pm | PHP | 2 Comments

In the second example I am going to use Standard PHP Library (SPL) to notify all attendees about the party location change. SPL has 2 interfaces and a Class for Observer.

  • interface SplObserver
    • Had update($subject) method
  • interface SplSubject
    • Has attach($observer) method
    • Has detach($observer) method
    • Has notify() method
  • class SplObjectStorage

Here is our BirthdayParty class that implements SplSubject interface.

<?php
/**
 *  @name BirthdayParty
 *  @author Gaurav Jassal
 *  @access public
 *  @copyright gauravjassal@gmail.com
 *
 */

class BirthdayParty implements SplSubject {

    protected $party_location = "Oxford Street";

    protected $observers = array();

	/**
	 * Add the observer to the list
	 * @param SplObserver $observer
	 * @return void
	 */
    public function attach(SplObserver $observer) {
        $this->observers[] = $observer;
    }
    /**
     * Remove observer for the list
     * @param SplObserver $observer
     * @return
     */
    public function detach(SplObserver $observer) {
    	// we are not going to remove any observer in this example
    }

	/**
	 * Send notification the observer
	 * @return void
	 */
    public function notify() {
        foreach ($this->observers as $key=>$value) {
            $value->update($this);
        }
    }
	/**
	 * Getter function for party location
	 * @return string
	 */
    public function getPartyLocation() {
        return $this->party_location;
    }
	/**
	 * Setter function for party location
	 * @param string $party_location
	 * @return void
	 */
    public function setPartyLocation($party_location) {
        $this->party_location = $party_location;
		$this->notify();
    }
}
?>

Our second Attendee class that implements SplObserver. When ever there is a change in the party location the update method gets triggered.

<?php

class Attendes implements SplObserver{
	protected $id;

	public function __construct($id)
	{
		$this->id = $id;
	}

	public function update(SplSubject $s)
	{
		echo "<br> " . $this->id . " has been notified about the new location :  " . $s->getPartyLocation();
		//return;
	}
}
?>

Usage

<?php
	error_reporting(E_ALL);
	ini_set("display_errors", 1); 

	require_once('BirthdayParty.php');
	require_once('Attendes.php');
	echo "<h1>Eample 2 </h1>";
	$bp = new BirthdayParty();

	$bp->attach(new Attendes("Gaurav"));
	$bp->attach(new Attendes("Ronnie"));
	$bp->attach(new Attendes("Katie"));
	$bp->attach(new Attendes("Suman"));
	$bp->attach(new Attendes("Devang"));

	$bp->setPartyLocation("King Williams St.");

?>

Results
Eample 2

Gaurav has been notified about the new location : King Williams St.
Ronnie has been notified about the new location : King Williams St.
Katie has been notified about the new location : King Williams St.
Suman has been notified about the new location : King Williams St.
Devang has been notified about the new location : King Williams St.

2 Responses to “Observer pattern in PHP part 2”

  1. julian says:

    Interesting post.

    You have mentioned the SplObjectStorage class but I can’t see where you use it in your example. Shouldn’t it be used for the BirthdayParty::observers property ? (Instead of array.)

  2. @Julian: You are right with SPL example we can use SplObjectStorage class. I checked the example file that you sent me the usage look straight forward.

    I will include you sample code in this post.

    Thanks, Gaurav

Leave a Reply

Recent Blog Entry

In the second example I am going to use Standard PHP Library (SPL) to notify all attendees about the party location change. SPL has 2 interfaces and a Class for Observer.


Read More

The Observer pattern defines a mechanism by which components can opt-in to receive messages when a target object changes its state. Its works similar to Listening to a Change event for a object or Trigger in a database


Read More

Multi touch is a method of interaction with a computer screen or your smartphones like iPhone or Palm Pre. Its allows the user to interact with your device by placing one or more than one finger on the screen.


Read More

Featured Projects

My Blog

Contact Information

© 2008 Gaurav Jassal