PHP hook, building hooks in your application
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!
Spot on with this write-up, I actually believe that this web site needs much more attention. I’ll probably be back again to see more, thanks for the information!
آدرس دانشگاه اراک
16 Sep 25 at 1:59 am
фильмы в хорошем качестве [url=https://www.kinogo-11.top]https://www.kinogo-11.top[/url] .
kinogo_ynMa
16 Sep 25 at 2:00 am
Привет фортовым игрокам КАЗИНО онлайн!
Играй и выигрывай честно с помощью 1win casino зеркало. Здесь доступны все любимые азартные игры. Каждый спин может стать выигрышным. Ты получаешь бонусы и призы. 1win casino зеркало всегда работает стабильно.
Заходите скорее на рабочее 1win casino зеркало – [url=https://t.me/s/onewincasinotoday]1win casino зеркало[/url]
Удачи и легких выйгрышей в 1win casino!
Casinojence
16 Sep 25 at 2:01 am
стоимость согласования перепланировки квартиры [url=turforum.borda.ru/?1-8-0-00003551-000-0-0]стоимость согласования перепланировки квартиры[/url] .
soglasovanie pereplanirovki kvartiri moskva _ryka
16 Sep 25 at 2:01 am
Pretty! This has been an incredibly wonderful article.
Thank you for supplying these details.
best online slots
16 Sep 25 at 2:01 am
согласование перепланировки квартиры москва [url=http://turforum.borda.ru/?1-8-0-00003551-000-0-0]согласование перепланировки квартиры москва [/url] .
soglasovanie pereplanirovki kvartiri moskva _oqka
16 Sep 25 at 2:04 am
Современные наливные полы https://proffi-floor.ru это не только идеально ровная поверхность, но и долговечность покрытия. Если вы планируете ремонт в квартире или офисе, узнайте подробнее об услугах по устройству наливных полов
proffi-floor-441
16 Sep 25 at 2:05 am
Wow, amazing blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your site is magnificent, let alone the
content!
Vortex Bitriver
16 Sep 25 at 2:05 am
Процедура начинается с осмотра и сбора анамнеза. После этого специалист проводит экстренную детоксикацию, снимает симптомы абстинентного синдрома, назначает поддерживающую терапию и даёт рекомендации по дальнейшим шагам. По желанию родственников или самого пациента помощь может быть оказана и в условиях стационара клиники.
Получить больше информации – http://narkologicheskaya-pomoshch-domodedovo6.ru/anonimnaya-narkologicheskaya-pomoshch-v-domodedovo/
Davidcratt
16 Sep 25 at 2:09 am
Hi! I know this is kind of off-topic but I had
to ask. Does managing a well-established blog like yours require a large
amount of work? I’m brand new to blogging but I
do write in my diary daily. I’d like to start a blog so I will be able to share my
own experience and feelings online. Please let me know if you have any kind of recommendations or tips for new aspiring blog owners.
Appreciate it!
Tải App 68WIN
16 Sep 25 at 2:12 am
With havin so much content and articles do you ever run into any problems of plagorism
or copyright infringement? My blog has a lot of completely unique content
I’ve either created myself or outsourced but it looks like a lot of it is
popping it up all over the internet without my authorization. Do you know any techniques to help stop content from being stolen? I’d really appreciate it.
paito sdy
16 Sep 25 at 2:12 am
I like the valuable information you provide in your articles.
I will bookmark your blog and check again here regularly.
I am quite sure I’ll learn a lot of new stuff right here!
Best of luck for the next!
live draw hk
16 Sep 25 at 2:15 am
Помощь родных играет важной ролью в восстановлении после запоя. Обсудите с ними о своих намерениях и попросите о помощи. Группы поддержки и консультации специалистов могут стать полезным ресурсом. Не забывайте о методах предотвращения срывов: рекомендации по трезвости и самопомощь при зависимости от алкоголя помогут избежать соблазна вернуться к прежнему. вывод из запоя
izzapoyavladimirNeT
16 Sep 25 at 2:17 am
3b9wov
📍 📌 Notice: 1.5 BTC expiring. Open account >> https://graph.org/Get-your-BTC-09-04?hs=7255fc1a3bb72291f146f9661674a5a8& 📍
16 Sep 25 at 2:18 am
Выездная бригада прибывает с необходимым оборудованием. Инфузионная терапия длится 60–120 минут; по ходу процедуры контролируются давление, пульс, дыхание и субъективное самочувствие, при необходимости схема корректируется (темп капания, смена растворов, добавление противорвотных или седативных средств). Чаще всего уже к концу первой инфузии снижается тошнота, уходит дрожь и «внутренняя дрожь», нормализуется сон. Врач оставляет пошаговый план на 24–72 часа: питьевой режим, щадящее питание (дробно, без жирного и острого), режим сна, рекомендации по витаминам и гепатопротекции. Если в процессе выявляются тревожные признаки (нестабильная гемодинамика, выраженная аритмия, спутанность сознания), будет предложен перевод в стационар.
Получить дополнительную информацию – [url=https://vyvod-iz-zapoya-reutov7.ru/]vyvod-iz-zapoya-kruglosutochno-reutov[/url]
ClintonHak
16 Sep 25 at 2:19 am
Городской портал Москвы https://moscowfy.ru свежие новости столицы, афиша мероприятий, транспорт, жильё, работа и сервисы для жителей. Полезная информация для москвичей и гостей города на одном сайте.
Michaelbaift
16 Sep 25 at 2:19 am
согласование проекта перепланировки квартиры [url=turforum.borda.ru/?1-8-0-00003551-000-0-0]согласование проекта перепланировки квартиры [/url] .
soglasovanie pereplanirovki kvartiri moskva _pgka
16 Sep 25 at 2:19 am
I am regular reader, how are you everybody? This paragraph posted at this
web site is in fact fastidious.
https://nh88trk.com/
16 Sep 25 at 2:21 am
согласование перепланировки квартиры цена [url=www.dimitrov.forum24.ru/?1-7-0-00000193-000-0-0]согласование перепланировки квартиры цена [/url] .
soglasovanie pereplanirovki kvartiri moskva _wska
16 Sep 25 at 2:22 am
согласование перепланировки квартиры под ключ [url=http://angelladydety.getbb.ru/viewtopic.php?f=42&t=59347&p=109062]согласование перепланировки квартиры под ключ [/url] .
soglasovanie pereplanirovki kvartiri moskva _zwka
16 Sep 25 at 2:25 am
https://myrentacar.site/
https://myrentacar.site/
16 Sep 25 at 2:30 am
согласование перепланировки квартиры москва [url=www.bisound.com/forum/showthread.php?p=2851657]согласование перепланировки квартиры москва [/url] .
soglasovanie pereplanirovki kvartiri moskva _bfka
16 Sep 25 at 2:31 am
нотариус перевод документов бюро технических переводов
byuro-perevodov-411
16 Sep 25 at 2:32 am
Hey there just wanted to give you a quick heads up and
let you know a few of the images aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different
browsers and both show the same outcome.
Model Kantrex 500
16 Sep 25 at 2:37 am
стоимость согласования перепланировки квартиры [url=http://dimitrov.forum24.ru/?1-7-0-00000193-000-0-0]стоимость согласования перепланировки квартиры[/url] .
soglasovanie pereplanirovki kvartiri moskva _wqka
16 Sep 25 at 2:39 am
https://myrentacar.site/
https://myrentacar.site/
16 Sep 25 at 2:39 am
перепланировка в нежилом здании [url=https://pereplanirovka-nezhilogo-pomeshcheniya2.ru/]перепланировка в нежилом здании[/url] .
pereplanirovka nejilogo pomesheniya_upEt
16 Sep 25 at 2:41 am
https://rylanshfz540.mystrikingly.com/
Gestionar una prueba de orina puede ser complicado. Por eso, se desarrollo una alternativa confiable con respaldo internacional.
Su composicion unica combina carbohidratos, lo que prepara tu organismo y oculta temporalmente los rastros de alcaloides. El resultado: una orina con parametros normales, lista para entregar tranquilidad.
Lo mas valioso es su accion rapida en menos de 2 horas. A diferencia de otros productos, no promete resultados permanentes, sino una solucion temporal que responde en el momento justo.
Miles de trabajadores ya han comprobado su discrecion. Testimonios reales mencionan envios en menos de 24 horas.
Si quieres proteger tu futuro, esta formula te ofrece respaldo.
JuniorShido
16 Sep 25 at 2:42 am
согласование проекта перепланировки квартиры [url=www.fanfiction.borda.ru/?1-0-0-00030334-000-0-0]согласование проекта перепланировки квартиры [/url] .
soglasovanie pereplanirovki kvartiri moskva _lmka
16 Sep 25 at 2:42 am
согласование проекта перепланировки квартиры [url=https://www.turforum.borda.ru/?1-8-0-00003551-000-0-0]согласование проекта перепланировки квартиры [/url] .
soglasovanie pereplanirovki kvartiri moskva _pfka
16 Sep 25 at 2:46 am
When someone writes an paragraph he/she retains the image of a user in his/her mind that how a user can be aware
of it. So that’s why this post is outstdanding. Thanks!
Nhà cái 39BET
16 Sep 25 at 2:51 am
согласование перепланировки квартиры москва [url=http://www.turforum.borda.ru/?1-8-0-00003551-000-0-0]согласование перепланировки квартиры москва [/url] .
soglasovanie pereplanirovki kvartiri moskva _eaka
16 Sep 25 at 2:53 am
согласование проекта перепланировки квартиры [url=www.www.bisound.com/forum/showthread.php?p=2851657]согласование проекта перепланировки квартиры [/url] .
soglasovanie pereplanirovki kvartiri moskva _pxka
16 Sep 25 at 2:57 am
Городской портал Москвы https://moscowfy.ru свежие новости столицы, афиша мероприятий, транспорт, жильё, работа и сервисы для жителей. Полезная информация для москвичей и гостей города на одном сайте.
Michaelbaift
16 Sep 25 at 2:58 am
commander de l’stromectol bon marchГ© sans assurance
comment obtenir des comprimГ©s d'stromectol gГ©nГ©rique
16 Sep 25 at 2:59 am
Городской портал Москвы https://moscowfy.ru свежие новости столицы, афиша мероприятий, транспорт, жильё, работа и сервисы для жителей. Полезная информация для москвичей и гостей города на одном сайте.
Michaelbaift
16 Sep 25 at 3:00 am
согласование перепланировки квартиры цена [url=https://fanfiction.borda.ru/?1-0-0-00030334-000-0-0/]согласование перепланировки квартиры цена [/url] .
soglasovanie pereplanirovki kvartiri moskva _dyka
16 Sep 25 at 3:00 am
Городской портал Москвы https://moscowfy.ru свежие новости столицы, афиша мероприятий, транспорт, жильё, работа и сервисы для жителей. Полезная информация для москвичей и гостей города на одном сайте.
Michaelbaift
16 Sep 25 at 3:01 am
Мега ссылка Мега даркнет Мега сайт Мега онион Мега ссылка Mega даркнет Mega сайт Mega онион Mega ссылка Mega darknet Mega onion
RichardPep
16 Sep 25 at 3:03 am
согласование перепланировки в нежилом помещении [url=https://pereplanirovka-nezhilogo-pomeshcheniya3.ru]https://pereplanirovka-nezhilogo-pomeshcheniya3.ru[/url] .
pereplanirovka nejilogo pomesheniya_iasa
16 Sep 25 at 3:03 am
переустройство нежилого помещения [url=http://www.pereplanirovka-nezhilogo-pomeshcheniya.ru]http://www.pereplanirovka-nezhilogo-pomeshcheniya.ru[/url] .
pereplanirovka nejilogo pomesheniya_ocKn
16 Sep 25 at 3:03 am
переустройство нежилого помещения [url=pereplanirovka-nezhilogo-pomeshcheniya1.ru]pereplanirovka-nezhilogo-pomeshcheniya1.ru[/url] .
pereplanirovka nejilogo pomesheniya_ipsi
16 Sep 25 at 3:03 am
стоимость согласования перепланировки квартиры [url=https://turforum.borda.ru/?1-8-0-00003551-000-0-0]стоимость согласования перепланировки квартиры[/url] .
soglasovanie pereplanirovki kvartiri moskva _qhka
16 Sep 25 at 3:04 am
Candy Gold играть
Donaldbow
16 Sep 25 at 3:05 am
перепланировка нежилых помещений [url=https://pereplanirovka-nezhilogo-pomeshcheniya2.ru]перепланировка нежилых помещений[/url] .
pereplanirovka nejilogo pomesheniya_zfEt
16 Sep 25 at 3:15 am
В Краснодаре решение есть — наркологическая клиника. Здесь помогают людям выйти из запоя без страха и осуждения. Всё анонимно, грамотно и с заботой о каждом пациенте.
Исследовать вопрос подробнее – [url=https://vyvod-iz-zapoya-krasnodar16.ru/]нарколог на дом цена город краснодар[/url]
RaymondKet
16 Sep 25 at 3:16 am
стоимость согласования перепланировки квартиры [url=http://angelladydety.getbb.ru/viewtopic.php?f=42&t=59347&p=109062]стоимость согласования перепланировки квартиры[/url] .
soglasovanie pereplanirovki kvartiri moskva _pdka
16 Sep 25 at 3:18 am
согласование проекта перепланировки квартиры [url=www.turforum.borda.ru/?1-8-0-00003551-000-0-0]согласование проекта перепланировки квартиры [/url] .
soglasovanie pereplanirovki kvartiri moskva _yeka
16 Sep 25 at 3:21 am
согласование перепланировки квартиры москва [url=https://www.bisound.com/forum/showthread.php?p=2851657/]согласование перепланировки квартиры москва [/url] .
soglasovanie pereplanirovki kvartiri moskva _dwka
16 Sep 25 at 3:24 am
Super platform! Everything is top-notch, congrats and good luck!
glory casino bangladesh download
glory casino bangladesh download
16 Sep 25 at 3:26 am