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!
Купить диплом техникума в Мариуполь [url=https://educ-ua7.ru/]https://educ-ua7.ru/[/url] .
Diplomi_rtea
22 Oct 25 at 6:14 am
В Краснодаре клиника «Детокс» предлагает выезд нарколога на дом. Помощь оказывается быстро, анонимно и круглосуточно.
Детальнее – [url=https://narkolog-na-dom-krasnodar27.ru/]запой нарколог на дом в краснодаре[/url]
Georgerapse
22 Oct 25 at 6:14 am
как купить диплом с занесением в реестр в екатеринбурге [url=https://frei-diplom2.ru/]https://frei-diplom2.ru/[/url] .
Diplomi_kdEa
22 Oct 25 at 6:15 am
купить диплом с занесением в реестр в иркутске [url=www.frei-diplom3.ru/]купить диплом с занесением в реестр в иркутске[/url] .
Diplomi_hvKt
22 Oct 25 at 6:16 am
лидеры seo продвижения веб студия [url=https://reiting-seo-agentstv.ru/]https://reiting-seo-agentstv.ru/[/url] .
reiting seo agentstv_njsa
22 Oct 25 at 6:16 am
Где купить Лирику в Слободском?Обратил внимание на https://podevalki.ru
– по отзывам неплохо. Цены приемлемые, доставляют быстро. Кто-то брал? Как с чистотой?
Stevenref
22 Oct 25 at 6:16 am
kraken официальный
кракен маркет
JamesDaync
22 Oct 25 at 6:18 am
купить диплом в белгороде [url=https://rudik-diplom5.ru/]купить диплом в белгороде[/url] .
Diplomi_wima
22 Oct 25 at 6:18 am
купить диплом внесенный в реестр [url=https://www.frei-diplom2.ru]купить диплом внесенный в реестр[/url] .
Diplomi_zjEa
22 Oct 25 at 6:20 am
диплом колледжа купить в москве [url=www.frei-diplom9.ru/]www.frei-diplom9.ru/[/url] .
Diplomi_vbea
22 Oct 25 at 6:20 am
Excited for $MTAUR’s exchange debut at double presale price. Token utility in outfits personalizes fun. Community engagement strong.
mtaur token
WilliamPargy
22 Oct 25 at 6:20 am
купить диплом владимирского техникума [url=http://frei-diplom8.ru/]купить диплом владимирского техникума[/url] .
Diplomi_ipsr
22 Oct 25 at 6:21 am
Un Code promo 1xbet 2026 : obtenez un bonus de bienvenue de 100% sur votre premier depot avec un bonus allant jusqu’a 130 €. Jouez et placez vos paris facilement grace aux fonds bonus. Apres l’inscription, il est important de recharger votre compte. Si votre compte est verifie, vous pourrez retirer toutes les sommes d’argent, y compris les bonus. Le code promo 1xbet est disponible via ce lien — Un Code Promo X Bet. Le code promo 1xBet sans depot est valable pour les nouveaux utilisateurs en Afrique du Sud, au Gabon et en RDC. Ce code promotionnel 1xBet offre des bonus gratuits 1xBet et des tours gratuits 1xBet aujourd’hui. Utilisez le meilleur code promo pour 1xBet et recevez votre bonus d’inscription 1xBet en quelques clics.
Marvinspaft
22 Oct 25 at 6:22 am
рейтинг seo компаний [url=https://top-10-seo-prodvizhenie.ru/]top-10-seo-prodvizhenie.ru[/url] .
top 10 seo prodvijenie_hwKa
22 Oct 25 at 6:23 am
купить диплом в норильске [url=http://rudik-diplom5.ru]купить диплом в норильске[/url] .
Diplomi_sfma
22 Oct 25 at 6:25 am
sildenafil over the counter: difference between Viagra and generic Sildenafil – difference between Viagra and generic Sildenafil
AnthonySep
22 Oct 25 at 6:26 am
диплом о окончании колледжа купить [url=frei-diplom8.ru]frei-diplom8.ru[/url] .
Diplomi_cysr
22 Oct 25 at 6:27 am
Приветственные бонусы также отличаются по типу. Определённые акции предлагаются новым клиентам. Во время регистрации на 1xBet, введите промокод и оформите 100% приветственный бонус в размере 32500 рублей.Букмекерская контора 1xBet предлагает своим клиентам участвовать в спортивных ставках и казино с использованием акционных предложений. Это увеличивает вовлечённость к играм и повышает надежность игры.Действующий код 1xBet можно активировать на странице регистрации — http://www.medtronik.ru/images/pages/1hbet_2021_promokod_pri_registracii.html.
Jasonbrado
22 Oct 25 at 6:27 am
рейтинг фирм seo [url=http://top-10-seo-prodvizhenie.ru/]http://top-10-seo-prodvizhenie.ru/[/url] .
top 10 seo prodvijenie_dfKa
22 Oct 25 at 6:27 am
услуги seo компании [url=https://reiting-seo-agentstv.ru]услуги seo компании[/url] .
reiting seo agentstv_hksa
22 Oct 25 at 6:27 am
купить диплом об окончании колледжа [url=http://frei-diplom9.ru/]купить диплом об окончании колледжа[/url] .
Diplomi_hiea
22 Oct 25 at 6:28 am
kraken vk6
кракен обмен
JamesDaync
22 Oct 25 at 6:29 am
купить диплом техникума красноярск [url=http://www.frei-diplom7.ru]купить диплом техникума красноярск[/url] .
Diplomi_qnei
22 Oct 25 at 6:29 am
seo оптимизация и продвижение сайтов москва [url=http://www.seo-prodvizhenie-reiting-kompanij.ru]seo оптимизация и продвижение сайтов москва[/url] .
seo prodvijenie reiting kompanii_erst
22 Oct 25 at 6:30 am
The $MTAUR ICO raffle tempts big. Token’s role in mini-games deep. Momentum unstoppable.
minotaurus coin
WilliamPargy
22 Oct 25 at 6:31 am
купить диплом маляра [url=http://rudik-diplom5.ru]купить диплом маляра[/url] .
Diplomi_ohma
22 Oct 25 at 6:31 am
купить диплом о высшем образовании с занесением в реестр владивосток [url=https://frei-diplom1.ru]https://frei-diplom1.ru[/url] .
Diplomi_rwOi
22 Oct 25 at 6:32 am
This is really interesting, You are an overly skilled blogger.
I’ve joined your rss feed and stay up for
in quest of more of your magnificent post. Also, I have shared your web site in my social networks
1xbet казино бездепозитный бонус
22 Oct 25 at 6:32 am
Стационар «Частного Медика 24» в Воронеже — место, где пациент, страдающий от длительного запоя, получает не просто деньги-стоимость услуги, а полноценную помощь: диагностику, детоксикацию, медикаментозную поддержку и психологическую помощь с уважением к личному пространству и анонимности.
Узнать больше – [url=https://vyvod-iz-zapoya-v-stacionare-voronezh24.ru/]нарколог вывод из запоя в стационаре воронеж[/url]
Donaldjange
22 Oct 25 at 6:33 am
Code promo sur 1xBet est unique et permet a chaque nouveau joueur de beneficier jusqu’a 100€ de bonus sportif a hauteur de 100% en 2026. Le bonus sera ajoute a votre solde en fonction de votre premier depot, le depot minimum etant fixe a 1€. Pour eviter toute perte de bonus, veillez a copier soigneusement le code depuis la source et a le saisir dans le champ « code promo (si disponible) » lors de l’inscription, afin de preserver l’integrite de la combinaison. D’autres promotions existent en plus du bonus de bienvenue, d’autres combinaisons vous permettant d’obtenir des bonus supplementaires sont disponibles dans la section « Vitrine des codes promo ». Consultez le lien pour plus d’informations sur les promotions disponibles — Code Promo 1xbet Burundi. Grace au code promo 1xBet gratuit, obtenez un pari gratuit 1xBet ou un bonus sans depot 1xBet. Le code promotionnel 1xBet valide vous permet de beneficier d’un bonus de bienvenue 1xBet 2026 sur vos premiers paris. Essayez le code promo sport 1xBet pour miser sans risque et decouvrir la plateforme.
Marvinspaft
22 Oct 25 at 6:33 am
저희 이태원가라오케는 남녀노소, 내외국인 누구나 편하게 찾아와 노래를 즐길 수 있는 개방적인 공간으로,
룸컨디션, 가격, 음향시설, 위치와 주차까지 모든 면에서
이태원가라오케
22 Oct 25 at 6:34 am
Yoou ought to be a part of a contest for one of the finest sies
online. I’m going to highly recommend this site!
Alsso visit my web site :: ремонт стиральных машин в краснознаменске московской области
ремонт стиральных машин в краснознаменске московской области
22 Oct 25 at 6:35 am
seo продвижение сайта цена в москве [url=seo-prodvizhenie-reiting-kompanij.ru]seo продвижение сайта цена в москве[/url] .
seo prodvijenie reiting kompanii_uest
22 Oct 25 at 6:37 am
kraken сайт
кракен Москва
JamesDaync
22 Oct 25 at 6:37 am
https://www.exchangle.com/1xbetfreebetpromo33
Duanehig
22 Oct 25 at 6:38 am
Bullish on Minotaurus ICO’s community. $MTAUR’s boosts strategic. Market growth aligns.
minotaurus ico
WilliamPargy
22 Oct 25 at 6:38 am
купить диплом в екатеринбург реестр [url=https://frei-diplom5.ru/]купить диплом в екатеринбург реестр[/url] .
Diplomi_znPa
22 Oct 25 at 6:39 am
где купить диплом с занесением реестр [url=http://www.frei-diplom2.ru]где купить диплом с занесением реестр[/url] .
Diplomi_dlEa
22 Oct 25 at 6:39 am
Hello There. I discovered your blog the usage of msn. That is an extremely
well written article. I’ll make sure to bookmark
it and return to learn extra of your helpful information. Thank you for the post.
I’ll certainly return.
nudes
22 Oct 25 at 6:39 am
seo продвижение россия [url=http://reiting-kompanii-po-prodvizheniyu-sajtov.ru/]seo продвижение россия[/url] .
agentstvo poiskovogo prodvijeniya_jvKt
22 Oct 25 at 6:39 am
I go to see day-to-day some websites and blogs to read posts, however this website gives quality based posts.
BlueQubit Legit Or Not
22 Oct 25 at 6:39 am
купить аттестат за классов [url=www.rudik-diplom12.ru/]купить аттестат за классов[/url] .
Diplomi_kcPi
22 Oct 25 at 6:41 am
sildenafil 50 mg ou 100 mg posologie [url=https://santehommefrance.shop/#]prix du Viagra générique en France[/url] Viagra générique pas cher
CharlesNeono
22 Oct 25 at 6:41 am
компании занимающиеся продвижением сайтов [url=http://www.top-10-seo-prodvizhenie.ru]компании занимающиеся продвижением сайтов[/url] .
top 10 seo prodvijenie_jlKa
22 Oct 25 at 6:41 am
как купить легально диплом о высшем образовании [url=www.frei-diplom1.ru]как купить легально диплом о высшем образовании[/url] .
Diplomi_kcOi
22 Oct 25 at 6:42 am
Hello are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started
and create my own. Do you need any coding expertise to
make your own blog? Any help would be really appreciated!
crypto casino sign up bonus
22 Oct 25 at 6:44 am
В «Частном Медике 24» в Самаре лечение организовано так, чтобы пациент чувствовал себя безопасно и защищённо.
Получить больше информации – [url=https://vyvod-iz-zapoya-v-stacionare-samara24.ru/]нарколог вывод из запоя в стационаре в самаре[/url]
JamessoypE
22 Oct 25 at 6:45 am
топ сео сайтов [url=https://www.reiting-seo-agentstv.ru]https://www.reiting-seo-agentstv.ru[/url] .
reiting seo agentstv_ersa
22 Oct 25 at 6:46 am
Капельница для вывода из запоя — проверенный способ снять признаки зависимости от алкоголя. В Красноярске много наркологических клиник предлагают услуги по выводу из запоя, но необходимо выбрать наиболее подходящую.Первым шагом является определение степени зависимости. Опытные врачи осуществят оценку состояния пациента и разработают персонализированный план лечения. Важночтобы клиника обеспечивала стационарные условия и анонимное лечение алкогольной зависимости. Это обеспечит удобные условия для пациентов.Обратите внимание на наличии психотерапии при алкоголизме. Психотерапия является важным аспектом в реабилитации после запоя и лечении алкогольной зависимости. Семейная поддержка также способствует успешному восстановлению зависимых. вывод из запоя Красноярск Посмотрите отзывы о медицинских учреждениях и уточните, какие услуги предоставляет нарколог. Выбор правильной наркологической клиники — залог успешного и безопасного лечения.
izzapoyakrasnoyarskNeT
22 Oct 25 at 6:46 am
kraken 2025
kraken зеркало
JamesDaync
22 Oct 25 at 6:46 am