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!
kuvalis.com – The navigation flows well, I was able to find what I needed quickly.
Winford Loeza
19 Oct 25 at 3:42 pm
maskchallengeusa – Clear purpose and bold message, layout supports awareness beautifully.
Goma Mona
19 Oct 25 at 3:43 pm
The Minotaurus presale vesting program is a game-changer for early birds. Extend for bonuses and avoid FOMO on post-TGE pumps. $MTAUR could be the dark horse in blockchain games.
mtaur coin
WilliamPargy
19 Oct 25 at 3:44 pm
kraken vk5
kraken онлайн
JamesDaync
19 Oct 25 at 3:44 pm
Если пациент не может приехать в клинику, в Краснодаре нарколог приедет к нему домой. Помощь оказывает «Детокс» круглосуточно.
Получить дополнительную информацию – [url=https://narkolog-na-dom-krasnodar25.ru/]вызов нарколога на дом краснодар[/url]
JamieOvedy
19 Oct 25 at 3:45 pm
Анонимные методы борьбы с алкоголизмом становится актуальным, так как многие люди стремятся избавиться от алкогольной зависимости без огласки. На сайте vivod-iz-zapoya-vladimir027.ru представлены различные программы реабилитации, включая detox-программы, которые позволяют пройти лечение в комфортной обстановке. Помощь врачей при алкоголизме включает методы кодирования и психотерапию при зависимостях. Группы поддержки в анонимном формате помогают справиться с психологическими трудностями, а поддержка родных и друзей играет важную роль в процессе реабилитации после алкоголизма. Удаленные консультации по алкоголизму и консультации без раскрытия данных обеспечивают доступ к психологической поддержке при запойном состоянии. Конфиденциальное лечение дает возможность людям быть уверенными в конфиденциальности. Важно помнить, что путь к выздоровлению начинается с первого шага.
lechenievladimirNeT
19 Oct 25 at 3:46 pm
yourtradingmentor.cfd – Overall positive first impression, excited to dive deeper into content.
Vernie Faren
19 Oct 25 at 3:50 pm
Heya! I’m at work browsing your blog from my new iphone!
Just wanted to say I love reading your blog and look forward to
all your posts! Keep up the great work! http://sz.80lvl.ru/viewtopic.php?f=9&t=15718
http://sz.80lvl.ru/viewtopic.php?f=9&t=15718
19 Oct 25 at 3:50 pm
Добро пожаловать в удивительный мир природы России!
Между прочим, если вас интересует Изучение ООПТ России: парки, заповедники, водоемы, загляните сюда.
Вот, можете почитать:
[url=https://alloopt.ru]https://alloopt.ru[/url]
Что думаете о красоте природы России? Делитесь мнениями!
fixRow
19 Oct 25 at 3:53 pm
What’s up, after reading this remarkable
piece of writing i am also happy to share my knowledge here with
mates.
bandar togel 4d
19 Oct 25 at 3:56 pm
Пациентам в Самаре предлагается анонимное лечение в стационаре с круглосуточным уходом и безопасными условиями.
Ознакомиться с деталями – [url=https://vyvod-iz-zapoya-v-stacionare-samara25.ru/]быстрый вывод из запоя в стационаре самара[/url]
GilbertCoeby
19 Oct 25 at 3:57 pm
bs2best at Готов ли ты раскрыть тайны, окутывающие тёмную сеть? Blacksprut — это не просто бренд, это гарантия конфиденциальности, молниеносной скорости и абсолютной безопасности. Посети bs2best.at и узнай то, о чём остальные предпочитают не упоминать. Тебе откроются все тайны, тщательно скрываемые от посторонних глаз. Только для избранных, обладающих особым знанием. Не оставляя следов. Без каких-либо компромиссов. Только Blacksprut. Не упусти уникальный шанс оказаться в авангарде — bs2best.at уже ждёт тех, кто стремится к открытиям. Сможешь ли ты осмелиться познать истинное положение вещей?
HermanRhype
19 Oct 25 at 3:58 pm
Автогид https://avtogid.in.ua автоблог с обзорами, автоновостями, советами по ремонту и обслуживанию авто
Arturotew
19 Oct 25 at 3:59 pm
melbet бк [url=www.melbetbonusy.ru]melbet бк[/url] .
melbet_oyOi
19 Oct 25 at 3:59 pm
Hi there just wanted to give you a brief heads up and let you know a few of the images aren’t loading properly.
I’m not sure why but I think its a linking issue.
I’ve tried it in two different internet browsers and both
show the same results.
Roof replacement services
19 Oct 25 at 4:01 pm
1win o‘zbek tilida bonus [url=https://1win5510.ru/]https://1win5510.ru/[/url]
1win_uz_yxsi
19 Oct 25 at 4:02 pm
кракен Москва
кракен android
JamesDaync
19 Oct 25 at 4:02 pm
купить диплом ветеринара [url=http://rudik-diplom9.ru/]купить диплом ветеринара[/url] .
Diplomi_hiei
19 Oct 25 at 4:02 pm
1вин лицензия уз [url=https://1win5510.ru/]1вин лицензия уз[/url]
1win_uz_qmsi
19 Oct 25 at 4:03 pm
I don’t even know how I ended up here, but I thought this post was good.
I don’t know who you are but definitely you are going
to a famous blogger if you aren’t already 😉 Cheers!
christmas light installation
19 Oct 25 at 4:04 pm
Если нужен профессиональный вывод из запоя, обращайтесь в стационар клиники «Детокс» в Сочи. Все процедуры проводятся анонимно и под наблюдением специалистов.
Выяснить больше – [url=https://vyvod-iz-zapoya-sochi24.ru/]вывод из запоя на дому круглосуточно в сочи[/url]
Bryankax
19 Oct 25 at 4:08 pm
купить диплом в елабуге [url=https://www.rudik-diplom9.ru]https://www.rudik-diplom9.ru[/url] .
Diplomi_leei
19 Oct 25 at 4:16 pm
кракен qr код
kraken сайт
JamesDaync
19 Oct 25 at 4:19 pm
Hey there! Someone in my Myspace group shared this site with us so I came to check it out.
I’m definitely loving the information. I’m book-marking
and will be tweeting this to my followers! Excellent blog and superb style and design.
Commercial Lighting Contractor
19 Oct 25 at 4:21 pm
мелбет дают ли фрибет [url=https://www.melbetbonusy.ru]мелбет дают ли фрибет[/url] .
melbet_baOi
19 Oct 25 at 4:22 pm
melbet промокод при регистрации без депозита
промокод мелбет фрибет
19 Oct 25 at 4:23 pm
купить диплом историка [url=https://rudik-diplom13.ru/]купить диплом историка[/url] .
Diplomi_obon
19 Oct 25 at 4:24 pm
Good site you have here.. It’s hard to find good quality writing like yours nowadays.
I seriously appreciate individuals like you!
Take care!!
nibung88
19 Oct 25 at 4:27 pm
Если вы хотите получить дополнительных бонусов от платформы 1xBet, нужно выполнить несколько требований, однако промокоды позволяют получить их быстрее и проще. Сумма бонусов, доступных игрокам через промокоды 1xBet, не всегда велики, но даже небольшой бонус способен существенно повысить игровой потенциал клиента. Активируйте промокод, чтобы получить бонус 100% на первый депозит в году 2026. Промокод 1xBet доступен по этой ссылке — http://www.vlaje.ru/obuv/pages/1xbet_promokod_pri_registracii_na_segodnya_besplatno.html.
Jamesslurn
19 Oct 25 at 4:30 pm
TG @‌LINKS_DEALER | EFFECTIVE SEO LINKS FOR spinbetter.bet
CharlesTHOTH
19 Oct 25 at 4:32 pm
1win uz [url=http://1win5509.ru/]http://1win5509.ru/[/url]
1win_uz_goKt
19 Oct 25 at 4:33 pm
купить свидетельство о заключении брака [url=www.rudik-diplom7.ru/]купить свидетельство о заключении брака[/url] .
Diplomi_sdPl
19 Oct 25 at 4:33 pm
Как победить зависимость в Саратове? Узнайте, где получить помощь и какие методы работают — подробное руководство на Noprost. Получить дополнительную информацию – http://mymoscow.forum24.ru/?1-2-0-00000820-000-0-0-1757525025
Crystaldum
19 Oct 25 at 4:34 pm
купить аттестаты за 9 [url=https://rudik-diplom9.ru/]купить аттестаты за 9[/url] .
Diplomi_cmei
19 Oct 25 at 4:35 pm
сайт бк мелбет [url=www.melbetbonusy.ru]www.melbetbonusy.ru[/url] .
melbet_brOi
19 Oct 25 at 4:36 pm
In fact no matter if someone doesn’t know afterward its up to other people that
they will help, so here it happens.
đăng nhập OK9
19 Oct 25 at 4:37 pm
kraken vpn
кракен ios
JamesDaync
19 Oct 25 at 4:37 pm
Pozycjonowanie stron internetowych (SEO) ewoluuje dynamicznie, napędzane
algorytmami wyszukiwarek, zachowaniami użytkowników
i postępem technologicznym.
SEO pod lokalne wyniki WebMiami80
19 Oct 25 at 4:43 pm
I know this site presents quality based content and
other stuff, is there any other web page which provides these kinds of things in quality?
pancur4d
19 Oct 25 at 4:45 pm
https://rant.li/ocofobpea/muine-kupit-gashish-boshki-marikhuanu
Anthonycam
19 Oct 25 at 4:45 pm
официальный сайт мелбет бонус [url=melbetbonusy.ru]официальный сайт мелбет бонус[/url] .
melbet_kmOi
19 Oct 25 at 4:49 pm
advancedtradingtools.cfd – I appreciate the educational resources, very beneficial for improving skills.
Milo Seraille
19 Oct 25 at 4:51 pm
hello there and thank you for your info – I’ve definitely picked up
anything new from right here. I did however expertise a few technical points using this web site, since I experienced to reload the site many times previous
to I could get it to load properly. I had been wondering if your web hosting is OK?
Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your
quality score if advertising and marketing with Adwords.
Well I’m adding this RSS to my email and can look out for a
lot more of your respective fascinating content.
Make sure you update this again soon.
homepage
19 Oct 25 at 4:53 pm
Generally I do not read article on blogs,
however I wish to say that this write-up very forced me to take a look at and do it!
Your writing taste has been surprised me. Thanks, very nice post.
Also visit my webpage A片
A片
19 Oct 25 at 4:54 pm
Выездная бригада «РостовМед» оснащена всем необходимым для оказания экстренной помощи и проведения полного курса детоксикации на дому. Процесс состоит из нескольких этапов:
Получить дополнительную информацию – https://narkologicheskaya-klinika-rostov13.ru/narkologicheskaya-klinika-narkologicheskij-czentr-v-rostove/
JosephNoirl
19 Oct 25 at 4:54 pm
kraken зеркало
kraken marketplace
JamesDaync
19 Oct 25 at 4:55 pm
1win demo rejim [url=https://1win5510.ru/]1win demo rejim[/url]
1win_uz_kssi
19 Oct 25 at 5:00 pm
купить диплом в армавире [url=http://rudik-diplom13.ru/]купить диплом в армавире[/url] .
Diplomi_umon
19 Oct 25 at 5:01 pm
купить аттестаты за 9 [url=https://rudik-diplom6.ru/]купить аттестаты за 9[/url] .
Diplomi_vzKr
19 Oct 25 at 5:02 pm
Программы вывода из запоя в Самаре включают детоксикацию, медикаментозную поддержку и работу с психотерапевтом.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-v-stacionare-samara25.ru/]нарколог вывод из запоя в стационаре в самаре[/url]
GilbertCoeby
19 Oct 25 at 5:06 pm