Introduction
One of the real challenges in building any type of framework, core or application is making it possible for the developers to hook into the business logic at specific points. Since PHP is not event based, nor it works with interrupts you have to come up an alternative.
The test case
Lets assume we are the main developers of a webshop framework. Programmers can use our framework to build complete webshops. Programmers can manage the orders that are placed on the webshop with the order class. The order class is part of our framework and we don’t want it to be extended by any programmer. However we don’t want to limit to programmers in their possibilities to hook into the orders process.
For example programmers should be able to send an email to the webshopowner if an order changes from one specific delivery status to another. This functionality is not part of the default behavior in our framework and is custom for the progammers webshop implementation.
Like said before, PHP doesn’t provide interrupts or real events so we need to come up with another way to implement hooks into our application. Lets take a look at the observer pattern.
Implementing the Observer pattern
The observer pattern is a design-pattern that describes a way for objects to be notified to specific state-changes in objects of the application.
For the first implementation we can use SPL. The SPL provides in two simple objects:
SPLSubject
- attach (new observer to attach)
- detach (existing observer to detach)
- notify (notify all observers)
SPLObserver
- update (Called from the subject (i.e. when it’s value has changed).
iOrderRef = $iOrderRef;
// Get order information from the database or an other resources
$this->iStatus = Order::STATUS_SHIPPED;
}
/**
* Attach an observer
*
* @param SplObserver $oObserver
* @return void
*/
public function attach(SplObserver $oObserver)
{
$sHash = spl_object_hash($oObserver);
if (isset($this->aObservers[$sHash])) {
throw new Exception('Observer is already attached');
}
$this->aObservers[$sHash] = $oObserver;
}
/**
* Detach observer
*
* @param SplObserver $oObserver
* @return void
*/
public function detach(SplObserver $oObserver)
{
$sHash = spl_object_hash($oObserver);
if (!isset($this->aObservers[$sHash])) {
throw new Exception('Observer not attached');
}
unset($this->aObservers[$sHash]);
}
/**
* Notify the attached observers
*
* @param string $sEvent, name of the event
* @param mixed $mData, optional data that is not directly available for the observers
* @return void
*/
public function notify()
{
foreach ($this->aObservers as $oObserver) {
try {
$oObserver->update($this);
} catch(Exception $e) {
}
}
}
/**
* Add an order
*
* @param array $aOrder
* @return void
*/
public function delete()
{
$this->notify();
}
/**
* Return the order reference number
*
* @return int
*/
public function getRef()
{
return $this->iOrderRef;
}
/**
* Return the current order status
*
* @return int
*/
public function getStatus()
{
return $this->iStatus;
}
/**
* Update the order status
*/
public function updateStatus($iStatus)
{
$this->notify();
// ...
$this->iStatus = $iStatus;
// ...
$this->notify();
}
}
/**
* Order status handler, observer that sends an email to secretary
* if the status of an order changes from shipped to delivered, so the
* secratary can make a phone call to our customer to ask for his opinion about the service
*
* @package Shop
*/
class OrderStatusHandler implements SplObserver
{
/**
* Previous orderstatus
* @var int
*/
protected $iPreviousOrderStatus;
/**
* Current orderstatus
* @var int
*/
protected $iCurrentOrderStatus;
/**
* Update, called by the observable object order
*
* @param Observable_Interface $oSubject
* @param string $sEvent
* @param mixed $mData
* @return void
*/
public function update(SplSubject $oSubject)
{
if(!$oSubject instanceof Order) {
return;
}
if(is_null($this->iPreviousOrderStatus)) {
$this->iPreviousOrderStatus = $oSubject->getStatus();
} else {
$this->iCurrentOrderStatus = $oSubject->getStatus();
if($this->iPreviousOrderStatus === Order::STATUS_SHIPPED && $this->iCurrentOrderStatus === Order::STATUS_DELIVERED) {
$sSubject = sprintf('Order number %d is shipped', $oSubject->getRef());
//mail('secratary@example.com', 'Order number %d is shipped', 'Text');
echo 'Mail sended to the secratary to help her remember to call our customer for a survey.';
}
}
}
}
$oOrder = new Order(26012011);
$oOrder->attach(new OrderStatusHandler());
$oOrder->updateStatus(Order::STATUS_DELIVERED);
$oOrder->delete();
?>
There are several problems with the implementation above. To most important disadvantage is that we have only one update method in our observer. In this update method we don’t know when and why we are getting notified, just that something happened. We should keep track of everything that happens in the subject. (Or use debug_backtrace… just joking, don’t even think about using it that way ever!).
Taking it a step further, events
Lets take a look at the next example, we will extend the Observer implementation with some an additional parameter for the eventname that occured.
Finishing up, optional data
iOrderRef = $iOrderRef;
// Get order information from the database or something else...
$this->iStatus = Order::STATUS_SHIPPED;
}
/**
* Attach an observer
*
* @param Observer_Interface $oObserver
* @return void
*/
public function attachObserver(Observer_Interface $oObserver)
{
$sHash = spl_object_hash($oObserver);
if (isset($this->aObservers[$sHash])) {
throw new Exception('Observer is already attached');
}
$this->aObservers[$sHash] = $oObserver;
}
/**
* Detach observer
*
* @param Observer_Interface $oObserver
* @return void
*/
public function detachObserver(Observer_Interface $oObserver)
{
$sHash = spl_object_hash($oObserver);
if (!isset($this->aObservers[$sHash])) {
throw new Exception('Observer not attached');
}
unset($this->aObservers[$sHash]);
}
/**
* Notify the attached observers
*
* @param string $sEvent, name of the event
* @param mixed $mData, optional data that is not directly available for the observers
* @return void
*/
public function notifyObserver($sEvent, $mData=null)
{
foreach ($this->aObservers as $oObserver) {
try {
$oObserver->update($this, $sEvent, $mData);
} catch(Exception $e) {
}
}
}
/**
* Add an order
*
* @param array $aOrder
* @return void
*/
public function add($aOrder = array())
{
$this->notifyObserver('onAdd');
}
/**
* Return the order reference number
*
* @return int
*/
public function getRef()
{
return $this->iOrderRef;
}
/**
* Return the current order status
*
* @return int
*/
public function getStatus()
{
return $this->iStatus;
}
/**
* Update the order status
*/
public function updateStatus($iStatus)
{
$this->notifyObserver('onBeforeUpdateStatus');
// ...
$this->iStatus = $iStatus;
// ...
$this->notifyObserver('onAfterUpdateStatus');
}
}
/**
* Order status handler, observer that sends an email to secretary
* if the status of an order changes from shipped to delivered, so the
* secratary can make a phone call to our customer to ask for his opinion about the service
*
* @package Shop
*/
class OrderStatusHandler implements Observer_Interface
{
protected $iPreviousOrderStatus;
protected $iCurrentOrderStatus;
/**
* Update, called by the observable object order
*
* @param Observable_Interface $oObservable
* @param string $sEvent
* @param mixed $mData
* @return void
*/
public function update(Observable_Interface $oObservable, $sEvent, $mData=null)
{
if(!$oObservable instanceof Order) {
return;
}
switch($sEvent) {
case 'onBeforeUpdateStatus':
$this->iPreviousOrderStatus = $oObservable->getStatus();
return;
case 'onAfterUpdateStatus':
$this->iCurrentOrderStatus = $oObservable->getStatus();
if($this->iPreviousOrderStatus === Order::STATUS_SHIPPED && $this->iCurrentOrderStatus === Order::STATUS_DELIVERED) {
$sSubject = sprintf('Order number %d is shipped', $oObservable->getRef());
//mail('secratary@example.com', 'Order number %d is shipped', 'Text');
echo 'Mail sended to the secratary to help her remember to call our customer for a survey.';
}
}
}
}
$oOrder = new Order(26012011);
$oOrder->attachObserver(new OrderStatusHandler());
$oOrder->updateStatus(Order::STATUS_DELIVERED);
$oOrder->add();
?>
Now we are able to take action on different events that occur.
Disadvantages
Although this implementation works quite well there are some drawbacks. One of those drawbacks is that we need to dispatch an event in our framework, if we don’t programmers can’t hook into our application. Triggering events everywhere give us a small performance penalty however I do think this way of working gives the programmers a nice way to hook into your application on those spots that you want them to hook in.
Just for the record
Notice that this code is just an example and can still use some improvements, for example: each observer is initialized even it will maybe never be notified, therefore I suggest to make use of lazy in some cases for loading the objects. There are other systems to hook into an application, more to follow!
lucky jet стратегия
Hi there, all the time i used to check web site posts
here in the early hours in the morning, since i enjoy to learn more and more.
tonicgreens I was hesitant in the beginning, yet TonicGreens surpassed my expectations.
The mix of herbs and vitamins actually makes a difference.
I’ve noticed less colds and much better total health.
It’s absolutely worth trying if you wish to sustain your immune
system naturally.
прогнозы на теннис го спорт prognoz-na-segodnya-na-sport1.ru .
ставки на хоккей прогнозы от профессионалов ставки на хоккей прогнозы от профессионалов .
прогнозы на спорт бесплатно от профессионалов на сегодня http://www.prognoz-na-segodnya-na-sport2.ru .
Absolutely! I think this is a great point, and I read a similar discussion on https://communistleague.org/ that explained this idea in even more depth.
прогнозы на баскетбол сегодня от профессионалов бесплатно прогнозы на баскетбол сегодня от профессионалов бесплатно .
лакиджет
Very rapidly this web page will be famous amid all blog viewers, due to it’s good articles
1win букмекерская скачать приложение https://www.1win1144.ru
Капельница при похмелье — это проверенный способ для ликвидации неприятных ощущений интоксикации после употребления алкоголя. При состоянии похмелья организм переживает нагрузки от недостатка жидкости и нехватки важных веществ, что способствует головной боли. Капельная терапия включает в себя вливание специальных растворов и возвращение нормального состояния организма. Медицинская помощь при похмелье может включать в себя лекарства для восстановления, которые помогают организму вернуться к норме. Капельницы часто содержат минералы, ингредиенты, способствующие очищению, которые способствуют детоксикации. Поддержка организма таким образом позволяет существенно облегчить состояние и способствовать быстрому облегчению. Если вам нужна капельница можно записаться на процедуру или вызвать врача на дом через сайт narkolog-tula005.ru. Помните, что оперативное вмешательство поможет избежать серьезных последствий.
купить диплом как [url=arus-diplom9.ru]купить диплом как[/url] .
провайдеры интернета в красноярске по адресу
domashij-internet-krasnoyarsk004.ru
интернет провайдеры красноярск по адресу
Hi, i believe that i saw you visited my web site so i got here to go back the desire?.I am
attempting to in finding things to improve my website!I suppose its adequate to use a few of your ideas!!
aviator 1 win https://1win1145.ru
прогноз на спорт на сегодня от профессионалов прогноз на спорт на сегодня от профессионалов .
Hi! Would you mind if I share your blog with my zynga group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thank you
Feel free to surf to my web page zorroescu01
спортивный прогноз на сегодня http://prognoz-na-segodnya-na-sport.ru .
mostbet depozit bonusu http://mostbet4045.ru
прогнозы на спорт бесплатно от профессионалов на сегодня https://www.prognoz-na-segodnya-na-sport2.ru .
прогноз на спорт на сегодня от профессионалов прогноз на спорт на сегодня от профессионалов .
аттестат ссср купить [url=www.arus-diplom10.ru]аттестат ссср купить[/url] .
Hmm is anyone else encountering problems with the images on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any feedback would be greatly appreciated.
Запой серьезная проблема, требующая немедленного вмешательства. В Туле услуги нарколога, выезжающего на дом пользуются высоким спросом, особенно когда речь идет о помощи при запое. Нарколог на дом может обеспечить необходимую медицинскую помощь при запое, включая процедуры детоксикации и безопасного вывода из запоя.Не рекомендуется откладывать, пока ситуация станет критической. Скорая помощь может быть вызвана в любой момент для оказания первой помощи и выявления необходимости в дальнейшем лечении. Лечение алкоголизма и алкогольной зависимости это процесс, который требует профессиональной помощи, и анонимность лечения играет важную роль для многих клиентов. Нарколог на дом срочно Тула Обращение к наркологу на дому в Туле — это возможность получить срочную помощь, не покидая домашние стены. Услуги нарколога включают в себя консультации так и проведение необходимых процедур по детоксикации. Обратитесь за помощью к профессионалам, чтобы вернуть контроль над своей жизнью.
Hi mates, how is all, and what you wish for to say concerning this piece
of writing, in my view its truly remarkable designed for
me.
This post will assist the internet viewers for creating new web site or
even a weblog from start to end.
Hi to every body, it’s my first go to see of this weblog;
this webpage contains amazing and actually good data designed for readers.
Сервис по подбору психолога Психотерапевт онлайн 891 оценок
Hi there, I enjoy reading through your post. I like to
write a little comment to support you.
I really like what you guys tend to be up too. This kind of clever work and coverage!
Keep up the great works guys I’ve incorporated you guys to my own blogroll.
купить диплом экономиста arus-diplom1.ru .
sportbets sportbets16.ru .
sportbets http://www.sportbets16.ru/ .
1аин вход [url=https://1win1144.ru]https://1win1144.ru[/url]
sportbets http://www.sportbets16.ru .
лучшие прогнозы на хоккей на сегодня http://kompyuternye-prognozy-na-futbol1.ru/ .
прогнозы на футбол от экспертов прогнозы на футбол от экспертов .
1 win ставки на спорт онлайн http://1win1142.ru
футбол завтра прогнозы на матчи http://kompyuternye-prognozy-na-futbol1.ru .
I know this if off topic but I’m looking into starting my own weblog
and was wondering what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet smart so I’m not 100% certain. Any recommendations
or advice would be greatly appreciated. Cheers
Мы изготавливаем дипломы психологов, юристов, экономистов и любых других профессий по приятным тарифам. Важно, чтобы документы были доступными для большого количества граждан. Приобрести диплом об образовании sntnika.forumex.ru/viewtopic.php?f=26&t=1363
mostbet hesabı bərpa etmək http://www.mostbet4042.ru
1вин авиатор 1вин авиатор
купить диплом о среднем образовании купить диплом о среднем образовании .
888starz скачать apk 888starz скачать apk .
купить диплом гознак купить диплом гознак .
Мы готовы предложить дипломы психологов, юристов, экономистов и любых других профессий по приятным ценам. Мы готовы предложить документы техникумов, расположенных в любом регионе Российской Федерации. Дипломы и аттестаты делаются на “правильной” бумаге самого высшего качества. Это дает возможности делать настоящие дипломы, которые невозможно отличить от оригиналов. orikdok-1v-gorode-ufa-2.ru
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you!
By the way, how can we communicate?
Мы готовы предложить дипломы психологов, юристов, экономистов и любых других профессий по доступным ценам. Мы готовы предложить документы ВУЗов, расположенных в любом регионе Российской Федерации. Дипломы и аттестаты печатаются на “правильной” бумаге самого высокого качества. Это позволяет делать настоящие дипломы, не отличимые от оригинала. orikdok-5v-gorode-samara-63.ru