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!
I love what you guys are usually up too. This type of clever work and
exposure! Keep up the very good works guys I’ve included you guys to blogroll.
중고알롤차사이트
20 Aug 25 at 7:21 pm
I do not even know the way I finished up right here,
but I thought this post was once good. I don’t know who you
are but definitely you’re going to a well-known blogger for those who aren’t already.
Cheers!
roofing contractor
20 Aug 25 at 7:21 pm
Nag Hammadi Wealth Code is an interesting program that blends ancient spiritual insights with modern mindset
techniques to attract wealth and abundance. It’s designed
to help people clear financial blocks, shift their thinking, and align with opportunities for success.
Many users find it appealing because it offers a unique, simple, and inspiring approach to
creating prosperity in everyday life.
Nag Hammadi Wealth Code
20 Aug 25 at 7:22 pm
https://paper.wf/eabyefigebeg/braga-kupit-kokain-mefedron-marikhuanu
Chriszek
20 Aug 25 at 7:25 pm
Один из важных принципов работы клиники — оперативность. В «РеабилитейшнПро» организована служба круглосуточного выезда: при необходимости врач-нарколог приезжает на дом в любой район Домодедово, чтобы оказать помощь пациенту на месте. Это особенно важно при острых состояниях, когда промедление может привести к осложнениям или создать угрозу для жизни.
Подробнее тут – [url=https://narkologicheskaya-pomoshch-domodedovo6.ru/]narkologicheskaya-pomoshch-na-domu-kruglosutochno[/url]
WilliamGet
20 Aug 25 at 7:26 pm
промокод для мелбет 2020 [url=http://melbet3006.com]http://melbet3006.com[/url]
melbet_repa
20 Aug 25 at 7:33 pm
Hurrah, that’s what I was looking for, what a information! existing here at this website, thanks admin of this site.
دانلود دفترچه ارشد دانشگاه آزاد ۱۴۰۴
20 Aug 25 at 7:34 pm
Why visitors still use to read news papers when in this
technological globe the whole thing is accessible on web?
dewascatter
20 Aug 25 at 7:35 pm
СтавкиПрогнозы [url=https://stavki-prognozy-2.ru/]https://stavki-prognozy-2.ru/[/url] .
stavki prognozy_lumn
20 Aug 25 at 7:35 pm
https://wanderlog.com/view/hjlmjrupet/купить-марихуану-гашиш-канабис-корфу/shared
Howardhib
20 Aug 25 at 7:37 pm
По любым вопросам касательно ставок в онлайн-казино всегда
можно обратиться в службу поддержки.
вулкан кз
20 Aug 25 at 7:43 pm
оценка завода оценка рыночной стоимости
ocenochnaya-kompaniya-560
20 Aug 25 at 7:45 pm
https://bio.site/ofyfrubao
Chriszek
20 Aug 25 at 7:46 pm
Thank you, I have just been searching for info
about this subject for a long time and yours is the greatest I have discovered
till now. However, what in regards to the conclusion? Are you positive about the source?
web page
20 Aug 25 at 7:51 pm
Great article.
how to charge a solar light without sun
20 Aug 25 at 7:51 pm
Затяжной запой опасен для жизни. Врачи наркологической клиники в Челябинске проводят срочный вывод из запоя — на дому или в стационаре. Анонимно, безопасно, круглосуточно.
Подробнее можно узнать тут – [url=https://vyvod-iz-zapoya-chelyabinsk11.ru/]наркология вывод из запоя[/url]
TimothyKam
20 Aug 25 at 7:54 pm
Men’s Growth is designed to naturally support male vitality, stamina,
and overall performance. With its blend of targeted ingredients, it aims to boost energy, confidence,
and long-term wellness. Many men appreciate it as a safe and effective way to maintain strength and feel more empowered
in daily life.
Men's Growth
20 Aug 25 at 7:59 pm
Когда вызывать нарколога на дом:
Получить дополнительную информацию – http://narkolog-na-dom-krasnogorsk6.ru/narkolog-na-dom-ceny-v-krasnogorske/
Joshuapatry
20 Aug 25 at 7:59 pm
Психолог
lilwlj7225
20 Aug 25 at 8:00 pm
Перед тем, как выбрать конкретный метод, проводится консультация и осмотр, чтобы исключить противопоказания и спрогнозировать результат.
Подробнее – [url=https://kodirovanie-ot-alkogolizma-dolgoprudnyj6.ru/]кодирование от алкоголизма[/url]
Fredrichop
20 Aug 25 at 8:03 pm
وکیل برای مهریه
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
وکیل برای مهریه
20 Aug 25 at 8:05 pm
Stavki Prognozy [url=https://stavki-prognozy-2.ru]https://stavki-prognozy-2.ru[/url] .
stavki prognozy_ghmn
20 Aug 25 at 8:07 pm
https://pxlmo.com/bitijhuma
Chriszek
20 Aug 25 at 8:07 pm
I’m not sure where you’re getting your information, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for fantastic info I was looking for this
information for my mission.
how to clean solar path lights
20 Aug 25 at 8:09 pm
Your style is so unique in comparison to other folks I’ve read stuff
from. Many thanks for posting when you’ve got the opportunity, Guess I’ll just
book mark this site.
solicitors
20 Aug 25 at 8:09 pm
Everything is very open with a clear explanation of
the challenges. It was definitely informative. Your site is extremely helpful.
Many thanks for sharing!
what is the highest lumens for solar lights
20 Aug 25 at 8:11 pm
Stavki Prognozy [url=https://stavki-prognozy-2.ru]https://stavki-prognozy-2.ru[/url] .
stavki prognozy_pkmn
20 Aug 25 at 8:12 pm
yunan escort Turkiye’de Egitim: Kaliteli ve Uluslararas? Standartlarda Turkiye, egitim alan?nda da onemli gelismeler kaydetmistir. Bircok universite, uluslararas? standartlarda egitim vermektedir. Turkiye’de egitim almak isteyen ogrenciler icin, cesitli burs imkanlar? da mevcuttur.
Aaronhoowl
20 Aug 25 at 8:13 pm
https://www.montessorijobsuk.co.uk/author/raacshmy/
Howardhib
20 Aug 25 at 8:16 pm
We are a group of volunteers and opening a brand new scheme in our community.
Your website provided us with useful information to work on. You’ve done a formidable task and
our entire group will probably be grateful to you.
best online casinos
20 Aug 25 at 8:17 pm
как вывести бонусы с 1win на карту [url=https://1win22097.ru]как вывести бонусы с 1win на карту[/url]
1win_pjpr
20 Aug 25 at 8:17 pm
Stavki Prognozy [url=https://stavki-prognozy-two.ru/]stavki-prognozy-two.ru[/url] .
stavki prognozi_feMr
20 Aug 25 at 8:19 pm
Поиграть в виртуальные слоты можно и без вложений
личных средств.
вулкан Россия
20 Aug 25 at 8:20 pm
ООО оценка активов оценить стоимость
ocenochnaya-kompaniya-411
20 Aug 25 at 8:24 pm
It’s an amazing article in support of all the internet visitors; they will
take advantage from it I am sure.
نگارش احکام فوق العاده ویژه فرهنگیان ۱۴۰۴
20 Aug 25 at 8:25 pm
stavkiprognozy [url=https://stavki-prognozy-1.ru/]https://stavki-prognozy-1.ru/[/url] .
stavki prognozy_voMa
20 Aug 25 at 8:25 pm
plinko slot [url=https://plinko3001.ru]https://plinko3001.ru[/url]
plinko_kz_vfEr
20 Aug 25 at 8:25 pm
купить диплом института ссср [url=https://educ-ua4.ru/]купить диплом института ссср[/url] .
Diplomi_tnPl
20 Aug 25 at 8:26 pm
Just read this insightful article, worth a look https://bbbbnewss.blogspot.com/2025/07/casino-de-charlevoix-in-canada.html
GonzaloGloth
20 Aug 25 at 8:27 pm
https://pxlmo.com/barodabopape
Chriszek
20 Aug 25 at 8:28 pm
проститутки алматы Эскорт Алматы предлагает уникальную возможность окунуться в мир роскоши и страсти. Опытные и привлекательные девушки готовы стать вашими спутницами на любое мероприятие, от делового ужина до романтического свидания. Они обладают не только безупречной внешностью, но и умением поддержать разговор, создать атмосферу комфорта и раскрепощенности. Кыздар нет – это место, где встречаются мечты и реальность. Здесь вы найдете именно ту девушку, которая соответствует вашим представлениям об идеальной спутнице. Вип эскорт Алматы – это выбор для тех, кто ценит высокий уровень сервиса и эксклюзивность. Девушки из этой категории обладают безупречными манерами, отличным образованием и умением вести себя в любом обществе. Эскортница Алматы – это профессионал своего дела, которая умеет доставить максимум удовольствия своему партнеру. Она знает все тонкости мужской психологии и готова воплотить в жизнь самые смелые фантазии. Алматы проститутки – это разнообразие выбора, от юных и нежных до опытных и страстных. Здесь каждый найдет то, что ищет. Эскорд Алмата – это возможность почувствовать себя королем, окруженным вниманием и заботой красивых и умных женщин.
MichaelBeift
20 Aug 25 at 8:28 pm
Hello, i believe that i saw you visited my
blog so i got here to go back the want?.I’m attempting to to find issues to
improve my site!I guess its adequate to make use of a few of your concepts!!
casino uden rofus
20 Aug 25 at 8:32 pm
В Челябинске решение есть — наркологическая клиника. Здесь помогают людям выйти из запоя без страха и осуждения. Всё анонимно, грамотно и с заботой о каждом пациенте.
Узнать больше – [url=https://vyvod-iz-zapoya-chelyabinsk13.ru/]вывод из запоя[/url]
JamesUrith
20 Aug 25 at 8:36 pm
Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Many thanks
can you charge solar lights indoors
20 Aug 25 at 8:36 pm
умный дом Современный интерьер – это стиль оформления интерьера, характеризующийся простотой, функциональностью и использованием современных материалов.
RaymondhaK
20 Aug 25 at 8:37 pm
cialis free trial coupon: cialis indications – Tadalify
PeterTEEFS
20 Aug 25 at 8:37 pm
plinko game online [url=http://plinko3001.ru]plinko game online[/url]
plinko_kz_urEr
20 Aug 25 at 8:40 pm
Наши усилия направлены на то, чтобы вернуть пациентам уверенность в себе, здоровье и качество жизни.
Детальнее – [url=https://tajnyj-vyvod-iz-zapoya.ru/vyvod-iz-zapoya-v-stacionare-v-omske.ru/]tajnyj-vyvod-iz-zapoya.ru/[/url]
DavidLah
20 Aug 25 at 8:40 pm
Stavki Prognozy [url=www.stavki-prognozy-2.ru]www.stavki-prognozy-2.ru[/url] .
stavki prognozy_exmn
20 Aug 25 at 8:42 pm
I am regular reader, how are you everybody? This post posted at this web site is genuinely pleasant.
Odkryj najmodniejsze fasony na ten sezon!
20 Aug 25 at 8:43 pm