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!
займ онлайн без отказа https://zaimy-59.ru
zaim online 987
23 Oct 25 at 9:26 am
Такие острова, как Гавайи, Исландия и
Галапагосские острова, образовались в результате вулканической активности.
казино вулкан
23 Oct 25 at 9:29 am
1xbet giri? [url=http://www.1xbet-giris-5.com]1xbet giri?[/url] .
1xbet giris_kuSa
23 Oct 25 at 9:30 am
купить диплом в чайковском [url=www.rudik-diplom9.ru]купить диплом в чайковском[/url] .
Diplomi_utei
23 Oct 25 at 9:33 am
1xbet yeni giri? adresi [url=1xbet-giris-3.com]1xbet-giris-3.com[/url] .
1xbet giris_dvMi
23 Oct 25 at 9:36 am
1xbet giri? g?ncel [url=http://1xbet-giris-6.com]1xbet giri? g?ncel[/url] .
1xbet giris_pcsl
23 Oct 25 at 9:38 am
вывод из запоя цена
vivod-iz-zapoya-smolensk024.ru
экстренный вывод из запоя
lecheniesmolenskNeT
23 Oct 25 at 9:39 am
1win бонусный счет [url=www.1win5518.ru]www.1win5518.ru[/url]
1win_kg_ymkl
23 Oct 25 at 9:40 am
1x giri? [url=http://www.1xbet-giris-2.com]http://www.1xbet-giris-2.com[/url] .
1xbet giris_lpPt
23 Oct 25 at 9:40 am
If some one wishes expert view concerning blogging afterward i recommend him/her to
pay a quick visit this website, Keep up the pleasant job.
ww88.fan
23 Oct 25 at 9:41 am
Если вы ищете информацию о бонусах при регистрации, ознакомьтесь с наши рекомендации о видах вознаграждений; в одном из разделов материала естественно упомянут https://bergkompressor.ru/news/artcles/?1xbet_promokod_pri_registracii_bonus_5.html для получения приветственного бонуса. Редакция объясняем, как вводить данные при регистрации и какие условия нужно выполнить для отыгрыша.
Anthonykit
23 Oct 25 at 9:41 am
В таких случаях медлить нельзя — своевременный вызов нарколога может спасти жизнь и предотвратить тяжёлую инвалидизацию.
Узнать больше – [url=https://vyvod-iz-zapoya-orekhovo-zuevo4.ru/]vyvod iz zapoya na domu orekhovo zuevo[/url]
Marvinwaf
23 Oct 25 at 9:43 am
https://bluepeakmeds.shop/# Blue Peak Meds
LanceHek
23 Oct 25 at 9:43 am
1xbwt giri? [url=https://1xbet-giris-6.com/]https://1xbet-giris-6.com/[/url] .
1xbet giris_plsl
23 Oct 25 at 9:48 am
I’m truly enjoying the design and layout of your site.
It’s a very easy on the eyes which makes it much more enjoyable
for me to come here and visit more often. Did you hire out a developer to create your theme?
Superb work!
https://topprowellness.com/
Mitos Lokal
23 Oct 25 at 9:49 am
Unlock curated shopping ɑt Kaizenaire.ϲom, the leading
promotions site іn Singapore.
From deluxe tⲟ budget, Singapore’ѕ shopping heaven givеs
deals that Singaporeans cherish.
Singaporeans delight іn laying out metropolitan landscapes іn notebooks,
and bear in mind to stay upgraded ߋn Singapore’s most current
promotions ɑnd shopping deals.
Mapletree spends іn genuine estate ɑnd property management, favored
Ƅy Singaporeans foг their modern-day growths and investment chances.
Collect tһe Label prօvides contemporary females’ѕ style mah, valued bу stylish Singaporeans fоr thеir mix-and-match collections sіа.
SIS Sugar sweetens wіth refined sugars, likеԀ fօr cooking
fundamentals іn Singaporean households.
Ꮃhy are reluctant mah, regularly check оut Kaizenaire.ϲom foг unequalled shopping discount rates lah.
mʏ рage ginger buffet promotions
ginger buffet promotions
23 Oct 25 at 9:50 am
1xbet resmi sitesi [url=www.1xbet-giris-2.com/]www.1xbet-giris-2.com/[/url] .
1xbet giris_goPt
23 Oct 25 at 9:50 am
https://www.chumsay.com/read-blog/117607
CalvinFuh
23 Oct 25 at 9:50 am
1xbet g?ncel adres [url=http://www.1xbet-giris-4.com]1xbet g?ncel adres[/url] .
1xbet giris_bvSa
23 Oct 25 at 9:50 am
bahis siteler 1xbet [url=www.1xbet-giris-5.com]bahis siteler 1xbet[/url] .
1xbet giris_riSa
23 Oct 25 at 9:50 am
купить аттестаты за 11 [url=http://rudik-diplom13.ru]купить аттестаты за 11[/url] .
Diplomi_eion
23 Oct 25 at 9:51 am
купить диплом кулинарного техникума [url=https://www.frei-diplom7.ru]купить диплом кулинарного техникума[/url] .
Diplomi_cxei
23 Oct 25 at 9:51 am
1xbet [url=http://1xbet-giris-3.com]1xbet[/url] .
1xbet giris_xaMi
23 Oct 25 at 9:53 am
1win скачать на айфон бесплатно [url=https://www.1win5518.ru]1win скачать на айфон бесплатно[/url]
1win_kg_blkl
23 Oct 25 at 9:55 am
Актуальные тенденции 2026 года в бонусной политике букмекеров: обзор фрибетов, промо-купонов и программ лояльности; в тексте, в разделе с примерами, даётся ссылка на 1xBet промокод 2026 как один из способов получить приветственный пакет. Читателям рекомендуем внимательно читать условия акций.
Anthonykit
23 Oct 25 at 9:58 am
When someone writes an post he/she keeps the idea of a user in his/her mind that how a user can be aware of it.
So that’s why this paragraph is outstdanding. Thanks!
make money online
23 Oct 25 at 9:58 am
Play The Password Game online in Canada! Test your creativity and problem-solving skills as you craft secure passwords with fun, unique challenges. Perfect for puzzle enthusiasts: official Password Game website
GabrielLyday
23 Oct 25 at 9:58 am
one win официальный сайт [url=https://www.1win5518.ru]https://www.1win5518.ru[/url]
1win_kg_zfkl
23 Oct 25 at 9:59 am
Очень ценная штука
yes, [url=https://mistiherbal.com/2025/06/04/match-de-casino-en-essentiel/]https://mistiherbal.com/2025/06/04/match-de-casino-en-essentiel/[/url] uses a provably honest algorithm with cryptographic verification, therefore gamblers confirm the results of each round to ensure honesty.
Jeffsip
23 Oct 25 at 10:00 am
Howdy! This blog post could not be written any better!
Going through this article reminds me of my previous roommate!
He continually kept talking about this. I will send this
article to him. Fairly certain he will have a good read.
Thank you for sharing!
commercial kitchen exhaust
23 Oct 25 at 10:01 am
С развитием онлайн-технологий сервис Ynla.ru представляет собой удобную доску объявлений, на которой ежедневно появляются тысячи вариантов от пользователей разных регионов России и ближнего зарубежья для покупки, продажи или обмена. Платформа построена на идее открытости, предлагая бесплатное размещение объявлений по разделам вроде автомобилей, жилья, вакансий, техники и сервисов, с лимитом в одно бесплатное для борьбы со спамом, как указано в регламенте. Простой поиск по местоположению, стоимости и видам, связь с социальными сетями для повышения охвата, плюс VIP-варианты и разделы магазинов обеспечивают легкость и скорость использования, в том числе для начинающих. https://ynla.ru/ — это ваш ключ к успешным сделкам без посредников. Тысячи просмотров, адаптация под мобильные устройства и отзывы реальных пользователей подтверждают востребованность сервиса, позволяя быстро отыскать подходящего контрагента и облегчая рутину.
wuwosyPiege
23 Oct 25 at 10:02 am
Heya! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up losing
months of hard work due to no backup. Do you have any solutions to prevent hackers?
Replica Watch
23 Oct 25 at 10:03 am
купить диплом в каспийске [url=http://www.rudik-diplom13.ru]http://www.rudik-diplom13.ru[/url] .
Diplomi_rlon
23 Oct 25 at 10:05 am
купить диплом московского колледжа [url=https://frei-diplom7.ru/]https://frei-diplom7.ru/[/url] .
Diplomi_pxei
23 Oct 25 at 10:07 am
купить диплом в оренбурге [url=www.rudik-diplom5.ru/]купить диплом в оренбурге[/url] .
Diplomi_sxma
23 Oct 25 at 10:08 am
https://akimataktobe.kz/n1844-promokod_1xBet_kazahstan_-_uvelichennyy_bonus_130_evro.html
dismfqm
23 Oct 25 at 10:08 am
1xbet giri? [url=www.1xbet-giris-6.com]1xbet giri?[/url] .
1xbet giris_vjsl
23 Oct 25 at 10:08 am
BluePeakMeds: Sildenafil online reviews – Sildenafil online reviews
WilliamUnjup
23 Oct 25 at 10:08 am
купить диплом в междуреченске [url=www.rudik-diplom13.ru/]www.rudik-diplom13.ru/[/url] .
Diplomi_ivon
23 Oct 25 at 10:12 am
1xbet turkiye [url=1xbet-giris-2.com]1xbet-giris-2.com[/url] .
1xbet giris_wtPt
23 Oct 25 at 10:12 am
1xbet g?ncel giri? [url=https://1xbet-giris-3.com/]1xbet g?ncel giri?[/url] .
1xbet giris_jpMi
23 Oct 25 at 10:12 am
ваучер на 1win [url=www.1win5518.ru]www.1win5518.ru[/url]
1win_kg_dykl
23 Oct 25 at 10:13 am
1 xbet giri? [url=www.1xbet-giris-5.com/]www.1xbet-giris-5.com/[/url] .
1xbet giris_xcSa
23 Oct 25 at 10:13 am
1xbet resmi sitesi [url=www.1xbet-giris-4.com/]www.1xbet-giris-4.com/[/url] .
1xbet giris_agSa
23 Oct 25 at 10:15 am
Potenzmittel günstig online [url=http://medivertraut.com/#]Sildenafil ohne Rezept[/url] MediVertraut
CharlesNeono
23 Oct 25 at 10:18 am
Данный комплекс мер обеспечивает восстановление здоровья пациента и создает основу для полного выздоровления.
Исследовать вопрос подробнее – http://narcolog-na-dom-msk55.ru/vyzov-narkologa-na-dom-moskva/
Willieshilk
23 Oct 25 at 10:22 am
1xbet guncel [url=https://1xbet-giris-3.com]https://1xbet-giris-3.com[/url] .
1xbet giris_tfMi
23 Oct 25 at 10:22 am
Как купить Ксанакс в Таганроге?Посмотрите на https://gytedaser.ru
– нормальные отзывы и цены. Доставка работает оперативно. Кто-нибудь пробовал их лично?
Stevenref
23 Oct 25 at 10:24 am
В этой статье-обзоре мы соберем актуальную информацию и интересные факты, которые освещают важные темы. Читатели смогут ознакомиться с различными мнениями и подходами, что позволит им расширить кругозор и глубже понять обсуждаемые вопросы.
ТОП-5 причин узнать больше – https://lukas.radimersky.cz/2011/02/10/super-pisnicka-spoon-i-turn-my-camera-on-na-youtube
GeorgePoedY
23 Oct 25 at 10:26 am
диплом техникума старого образца купить [url=https://frei-diplom7.ru]диплом техникума старого образца купить[/url] .
Diplomi_fmei
23 Oct 25 at 10:27 am