Contact me at : hello[at]gauravjassal[dot]com
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.
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.
Previously i posted a very useful class to categorize multi-dimensional array. I wrote another one that is just an alternative for array_unique function.
This extension notifies you with latest London underground tube updates. You can click on the browser button and check the status of any tube line.By default the extension updates the status in every 5 mins.
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.
© 2008
Gaurav Jassal
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.)
@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