Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: min(three, four)?
(Example: nine)

The Note You're Voting On

Anonymous
12 years ago
<?php
class Observable implements SplSubject
{
private
$storage;

function
__construct()
{
$this->storage = new SplObjectStorage();
}

function
attach(SplObserver $observer)
{
$this->storage->attach($observer);
}

function
detach(SplObserver $observer)
{
$this->storage->detach($observer);
}

function
notify()
{
foreach (
$this->storage as $obj) {
$obj->update($this);
}
}
//...
}

abstract class
Observer implements SplObserver
{
private
$observable;

function
__construct(Observable $observable)
{
$this->observable = $observable;
$observable->attach($this);
}

function
update(SplSubject $subject)
{
if (
$subject === $this->observable) {
$this->doUpdate($subject);
}
}

abstract function
doUpdate(Observable $observable);
}

class
ConcreteObserver extends Observer
{
function
doUpdate(Observable $observable)
{
//...
}
}

$observable = new Observable();
new
ConcreteObserver($observable);

<< Back to user notes page

To Top