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!
http://herengezondheid.com/# ED-medicatie zonder voorschrift
Hermanereli
23 Oct 25 at 8:53 pm
1xBet бонус код получите дополнительные преимущества при создании аккаунта на официальном сайте
CollinDwets
23 Oct 25 at 8:53 pm
trendandstyle – The mobile version loads quickly, browsing on phone was convenient.
Russ Utterback
23 Oct 25 at 8:53 pm
займ онлайн без отказа займ без звонков на мобильный
mfo zaym 309
23 Oct 25 at 8:53 pm
Регистрация в 2026 г. — руководство по бонусам и акциям. Узнайте, как правильно активировать регистрационные предложения, и в середине процесса обратите внимание на 1xBet промокод как вариант получения дополнительного вознаграждения. Часто задаваемые вопросы помогут быстро разобраться с верификацией и получением бонусов.
Petercet
23 Oct 25 at 8:54 pm
1win зеркало на сегодня [url=www.1win5519.ru]www.1win5519.ru[/url]
1win_kg_epEr
23 Oct 25 at 8:54 pm
1вин вывод средств [url=www.1win5519.ru]1вин вывод средств[/url]
1win_kg_kbEr
23 Oct 25 at 8:55 pm
Туси слишком долго прёт
https://annturnercook.info
и вы считаете что все отлично?
Alexistum
23 Oct 25 at 8:56 pm
Today, I went to the beach front with my children. I found a sea shell and gave it to my 4
year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the
shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is totally off topic
but I had to tell someone!
new88
23 Oct 25 at 8:56 pm
1xbet giri? [url=https://www.1xbet-giris-4.com]1xbet giri?[/url] .
1xbet giris_ptSa
23 Oct 25 at 8:57 pm
Узнайте, как можно активировать различные уникальные функции этого слота.
слоты на деньги
23 Oct 25 at 8:57 pm
1xbet turkiye [url=https://1xbet-giris-2.com/]1xbet-giris-2.com[/url] .
1xbet giris_aoPt
23 Oct 25 at 8:58 pm
apotek online utan recept: Sildenafil utan recept – billig Viagra Sverige
Jesuskax
23 Oct 25 at 8:58 pm
best real money online pokies new zealand, online casino review usa and free tangerine online gambling, Linnea,
pokies win real money united kingdom, or new zealandn online casino pokies
Linnea
23 Oct 25 at 8:58 pm
whoah this weblog is wonderful i love reading your articles. Stay up the great work! You realize, a lot of persons are hunting round for this information, you can help them greatly.
бонус драгон мани казино
ShaneDrync
23 Oct 25 at 8:59 pm
1xbet lite [url=http://1xbet-giris-4.com]http://1xbet-giris-4.com[/url] .
1xbet giris_mcSa
23 Oct 25 at 8:59 pm
1xbet giri? g?ncel [url=http://1xbet-giris-5.com]1xbet giri? g?ncel[/url] .
1xbet giris_gySa
23 Oct 25 at 9:00 pm
I was pretty pleased to discover this page. I want to
to thank you for your time for this fantastic read!! I definitely enjoyed every
part of it and I have you book marked to look at new information on your website.
AYUTOGEL
23 Oct 25 at 9:01 pm
Je suis totalement envoute par Spinit Casino, ca offre un plaisir veloce. Il y a une profusion de jeux excitants, offrant des sessions live immersives. Avec des depots instantanes. Le suivi est irreprochable, offrant des reponses claires. Le processus est simple et elegant, parfois des offres plus genereuses seraient veloces. Dans l’ensemble, Spinit Casino offre une experience memorable pour les passionnes de jeux modernes ! De plus la plateforme est visuellement veloce, facilitate une immersion totale. Un autre atout le programme VIP avec des niveaux exclusifs, qui booste l’engagement.
casinospinitfr.com|
WheelWhisperB3zef
23 Oct 25 at 9:01 pm
Юридическому лицу важно точное соблюдение сроков, и здесь с этим полный порядок https://tamozhenniiy-predstavitel.ru/
NathanDax
23 Oct 25 at 9:01 pm
I do agree with all of the concepts you have presented in your post.
They are very convincing and can definitely work. Still, the posts are too quick for beginners.
May just you please extend them a little from subsequent time?
Thank you for the post.
hair transplant in turkey
23 Oct 25 at 9:03 pm
Юридическому лицу важно точное соблюдение сроков, и здесь с этим полный порядок, https://tamozhenniiy-predstavitel.ru/
NathanDax
23 Oct 25 at 9:05 pm
рейтинг интернет агентств seo [url=https://reiting-seo-kompaniy.ru/]https://reiting-seo-kompaniy.ru/[/url] .
reiting seo kompanii_owon
23 Oct 25 at 9:05 pm
Если вы хотите найти информацию о бонусах при регистрации, ознакомьтесь с наши рекомендации о видах вознаграждений; в одном из разделов материала естественно упомянут 1хБет промокод для получения приветственного бонуса. Мы объясняем, как указывать данные при регистрации и какие условия нужно выполнить для отыгрыша.
Petercet
23 Oct 25 at 9:05 pm
1xbet ?ye ol [url=http://www.1xbet-giris-6.com]http://www.1xbet-giris-6.com[/url] .
1xbet giris_nisl
23 Oct 25 at 9:05 pm
приложение 1 вин [url=https://1win5518.ru/]приложение 1 вин[/url]
1win_kg_jfkl
23 Oct 25 at 9:07 pm
Домашнее лечение уместно, если сохранён ясный контакт и стабильное дыхание, нет признаков делирия и угрожающей гемодинамики. Алгоритм выстроен так, чтобы каждая минута работала на результат. Семья видит прозрачные шаги, а у пациента появляется ощущение предсказуемости. Ниже — ориентир; по месту врач адаптирует длительности и приоритеты под возраст, сопутствующие заболевания и исходные показатели.
Разобраться лучше – https://narcolog-na-dom-ekaterinburg0.ru/narkolog-ekaterinburg-na-dom/
Wayneorifs
23 Oct 25 at 9:09 pm
kraken обмен
кракен 2025
379587a
JamesDaync
23 Oct 25 at 9:10 pm
Heren Gezondheid: Heren Gezondheid – officiële Sildenafil webshop
Jesuskax
23 Oct 25 at 9:12 pm
1xbet yeni giri? adresi [url=www.1xbet-giris-4.com]www.1xbet-giris-4.com[/url] .
1xbet giris_pgSa
23 Oct 25 at 9:13 pm
1x bet [url=www.1xbet-giris-1.com/]www.1xbet-giris-1.com/[/url] .
1xbet giris_lqkt
23 Oct 25 at 9:17 pm
1x bet giri? [url=1xbet-giris-2.com]1xbet-giris-2.com[/url] .
1xbet giris_ctPt
23 Oct 25 at 9:17 pm
1xbetgiri? [url=https://1xbet-giris-6.com]https://1xbet-giris-6.com[/url] .
1xbet giris_nesl
23 Oct 25 at 9:18 pm
старые дипломы купить [url=www.rudik-diplom9.ru]старые дипломы купить[/url] .
Diplomi_nxei
23 Oct 25 at 9:19 pm
Je suis emerveille par Olympe Casino, ca offre un plaisir immortel. La selection de jeux est olympienne, comprenant des jeux optimises pour les cryptos. 100% jusqu’a 500 € + tours gratuits. Le support client est olympien, joignable a toute heure. Les gains arrivent sans delai, cependant des offres plus genereuses seraient olympiennes. Dans l’ensemble, Olympe Casino offre une experience legendaire pour les passionnes de jeux antiques ! En bonus le site est rapide et glorieux, facilite une immersion totale. Un plus divin le programme VIP avec des niveaux exclusifs, qui booste l’engagement.
https://olympefr.com/|
AthenaWisdomB2zef
23 Oct 25 at 9:19 pm
https://t.me/s/iw_1xbet
Bradleysaige
23 Oct 25 at 9:19 pm
1xbet mobil giri? [url=www.1xbet-giris-1.com]www.1xbet-giris-1.com[/url] .
1xbet giris_rhkt
23 Oct 25 at 9:19 pm
kraken сайт
кракен зеркало
[url=https://retailkassa.ru/forum/messages/forum1/topic6/message93599/?result=reply#message93599]Jamesdus[/url] 7c93a6_
JamesDaync
23 Oct 25 at 9:20 pm
Всё сделали быстро и грамотно, приятно работать с профессионалами своего дела https://svobroker.ru/
Brianovalt
23 Oct 25 at 9:23 pm
seo агентство топ [url=https://reiting-seo-kompaniy.ru/]https://reiting-seo-kompaniy.ru/[/url] .
reiting seo kompanii_nnon
23 Oct 25 at 9:24 pm
Hello men
Hello. A 24 fine site 1 that I found on the Internet.
Check out this site. There’s a great article there. https://resistancemedia.org/progressive-slots/how-to-develop-super-memory/|
There is sure to be a lot of useful and interesting information for you here.
You’ll find everything you need and more. Feel free to follow the link below.
Arisha24et
23 Oct 25 at 9:24 pm
https://mundocosplayer.com.br/2022/09/codigo-promocional-1xbet-gratis-hoje-bonus1x200/
ubdrlfr
23 Oct 25 at 9:24 pm
obviously like your web site but you need to check the spelling on several of your posts.
Many of them are rife with spelling problems and I in finding it very bothersome
to inform the reality then again I’ll surely come again again.
Akiho Yoshizawa
23 Oct 25 at 9:25 pm
https://t.me/s/ofitsialniy_1win
Bradleysaige
23 Oct 25 at 9:25 pm
кракен ссылка
вход
кракен ссылка
23 Oct 25 at 9:25 pm
1xbet com giri? [url=https://www.1xbet-giris-7.com]https://www.1xbet-giris-7.com[/url] .
1xbet giris_utKn
23 Oct 25 at 9:27 pm
Hi there! I could have sworn I’ve been to this blog before but after reading through some of the post I realized it’s new to me.
Nonetheless, I’m definitely happy I found it and I’ll
be book-marking and checking back frequently!
bästa casino online
23 Oct 25 at 9:27 pm
купить диплом с занесением в реестр [url=www.rudik-diplom6.ru]купить диплом с занесением в реестр[/url] .
Diplomi_lqKr
23 Oct 25 at 9:28 pm
Для достижения положительного результата применяются комплексные подходы, сочетающие медикаментозные и поддерживающие методы. Врачи оценивают общее состояние пациента, степень выраженности абстинентного синдрома и сопутствующие заболевания. На основе этих данных подбираются оптимальные методы коррекции.
Изучить вопрос глубже – [url=https://vyvod-iz-zapoya-volgograd0.ru/]вывод из запоя клиника в санкт-петербурге[/url]
Hermanjassy
23 Oct 25 at 9:28 pm
kraken client
кракен маркет
кракен
23 Oct 25 at 9:29 pm