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!
kraken сайт
кракен тор
kraken ссылка
24 Oct 25 at 7:56 am
I’m extremely inspired together with your writing skills as
smartly as with the structure for your weblog. Is this a paid theme or did you modify it yourself?
Anyway keep up the nice high quality writing, it is uncommon to look a nice
weblog like this one nowadays..
new casino sites
24 Oct 25 at 7:56 am
1xbet guncel [url=1xbet-giris-8.com]1xbet guncel[/url] .
1xbet giris_zcPn
24 Oct 25 at 7:57 am
https://hottips.click/
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
https://hottips.click/
24 Oct 25 at 7:59 am
где можно купить диплом медсестры [url=http://frei-diplom14.ru/]где можно купить диплом медсестры[/url] .
Diplomi_ifoi
24 Oct 25 at 8:00 am
Где купить Мяу-Мяу в Северобайкальске?Друзья, подскажите с выбором – нашел https://burn-credite.ru
. Цены вроде ок, доставка заявлена. Кто-нибудь знаком с ними? Насколько надежно?
Stevenref
24 Oct 25 at 8:00 am
купить диплом техникума отзывы [url=https://frei-diplom11.ru]купить диплом техникума отзывы[/url] .
Diplomi_aksa
24 Oct 25 at 8:02 am
кракен тор
кракен онлайн
JamesDaync
24 Oct 25 at 8:02 am
купить диплом в петрозаводске [url=http://rudik-diplom9.ru/]купить диплом в петрозаводске[/url] .
Diplomi_flei
24 Oct 25 at 8:03 am
Эта обзорная заметка содержит ключевые моменты и факты по актуальным вопросам. Она поможет читателям быстро ориентироваться в теме и узнать о самых важных аспектах сегодня. Получите краткий курс по современной информации и оставайтесь в курсе событий!
Жми сюда — получишь ответ – https://pinstore.com.vn/pinstore-ha-noi-nhung-dieu-thu-vi-ve-linen
Bobbyfam
24 Oct 25 at 8:03 am
Useful information. Fortunate me I found your website accidentally, and I am stunned why this twist
of fate did not came about in advance! I bookmarked it.
نمایندگی تعمیر لباسشویی سامسونگ
24 Oct 25 at 8:08 am
farmaci per potenza maschile: Viagra generico online Italia – comprare Sildenafil senza ricetta
RandySkync
24 Oct 25 at 8:09 am
great points altogether, you just received a emblem new reader.
What might you recommend about your publish that you simply
made some days in the past? Any sure?
exhaust system
24 Oct 25 at 8:10 am
1xbet giri? linki [url=http://www.1xbet-giris-9.com]1xbet giri? linki[/url] .
1xbet giris_npon
24 Oct 25 at 8:10 am
промокод для 1win [url=http://1win5519.ru/]промокод для 1win[/url]
1win_kg_ehEr
24 Oct 25 at 8:10 am
кракен vk6
кракен клиент
kraken ссылка
24 Oct 25 at 8:12 am
Very rapidly this website will be famous among all
blog people, due to it’s pleasant articles or reviews
Viral photo tips
24 Oct 25 at 8:12 am
Pretty nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed surfing around your blog posts.
In any case I will be subscribing to your rss
feed and I hope you write again very soon!
website mua bán ma túy
24 Oct 25 at 8:13 am
kraken market
kraken market
кракен вход
24 Oct 25 at 8:13 am
1xbet tr [url=https://1xbet-giris-7.com/]1xbet tr[/url] .
1xbet giris_vsKn
24 Oct 25 at 8:14 am
It’s a pity you don’t have a donate button! I’d definitely donate to this brilliant blog!
I guess for now i’ll settle for book-marking
and adding your RSS feed to my Google account. I look forward to new updates and will
share this site with my Facebook group. Talk soon!
webpage
24 Oct 25 at 8:14 am
Saved as a favorite, I really like your web site!
MV88
24 Oct 25 at 8:15 am
1xbet yeni adresi [url=http://1xbet-giris-10.com/]1xbet yeni adresi[/url] .
1xbet giris_jmka
24 Oct 25 at 8:15 am
one x bet [url=www.1xbet-giris-7.com/]www.1xbet-giris-7.com/[/url] .
1xbet giris_dsKn
24 Oct 25 at 8:17 am
xbet [url=1xbet-7.com]xbet[/url] .
1xbet_lgol
24 Oct 25 at 8:17 am
1win ссылка [url=https://1win5519.ru/]https://1win5519.ru/[/url]
1win_kg_paEr
24 Oct 25 at 8:17 am
Ищете просторное такси для семьи, команды или багажа? Minivan Online подаст 8 местный минивэн вовремя, с детскими креслами без доплат и фиксированными тарифами на трансферы в аэропорты Москвы. В автопарке только свежие HYUNDAI Grand Starex, VW Caravelle, Mercedes V Class и другие — чистые, комфортные, с климат контролем. Подробности и онлайн заказ — https://minivan.online Вежливые водители со стажем 20+ лет и большой багажник решат задачу любой сложности.
kijesupum
24 Oct 25 at 8:18 am
В этой публикации мы предлагаем подробные объяснения по актуальным вопросам, чтобы помочь читателям глубже понять их. Четкость и структурированность материала сделают его удобным для усвоения и применения в повседневной жизни.
Изучить эмпирические данные – https://ttechsolutions.lk/16/a-step-by-step-guide-to-creating-a-blog-on-wordpress
LloydNal
24 Oct 25 at 8:19 am
кракен актуальная ссылка
kraken вход
JamesDaync
24 Oct 25 at 8:19 am
1win ставка [url=1win5518.ru]1win5518.ru[/url]
1win_kg_gdkl
24 Oct 25 at 8:20 am
купить диплом в оренбурге [url=http://rudik-diplom9.ru]купить диплом в оренбурге[/url] .
Diplomi_ynei
24 Oct 25 at 8:21 am
I have learn some good stuff here. Certainly price bookmarking for revisiting.
I wonder how a lot attempt you put to create this type of excellent informative website.
Local dump truck services
24 Oct 25 at 8:21 am
신용카드현금화 – 급전이 필요할 때, 신용카드 한도를 안전하고 간편하게
현금으로 바꿔드립니다. 낮은 수수료, 신용등급
걱정 없이 즉시 입금, 모든 카드사 이용
가능
신용카드현금화
24 Oct 25 at 8:22 am
1win как вывести деньги [url=www.1win5518.ru]www.1win5518.ru[/url]
1win_kg_zmkl
24 Oct 25 at 8:22 am
1xbet spor bahislerinin adresi [url=https://1xbet-giris-8.com]1xbet spor bahislerinin adresi[/url] .
1xbet giris_eqPn
24 Oct 25 at 8:22 am
kraken обмен
кракен онлайн
kraken сайт
24 Oct 25 at 8:24 am
Статья содержит практические рекомендации и полезные советы, которые можно легко применить в повседневной жизни. Мы делаем акцент на реальных примерах и проверенных методиках, которые способствуют личностному развитию и улучшению качества жизни.
Что скрывают от вас? – https://www.johanterlouw.nl/recept-en-recepten/bananen-ijs
RalphCig
24 Oct 25 at 8:25 am
скачать 1win на пк [url=www.1win5519.ru]www.1win5519.ru[/url]
1win_kg_qlEr
24 Oct 25 at 8:25 am
1xbet yeni adresi [url=https://1xbet-giris-7.com/]1xbet yeni adresi[/url] .
1xbet giris_chKn
24 Oct 25 at 8:27 am
Где купить Лирику в Ермолиное?Ваше мнение о https://zingshoes.ru
? Цены привлекательные, доставка быстрая. Кто-то покупал? Хочу знать о качестве?
Stevenref
24 Oct 25 at 8:27 am
speed n cash лайфак [url=https://1win5519.ru]https://1win5519.ru[/url]
1win_kg_slEr
24 Oct 25 at 8:28 am
Ремонт двигателей Джип [url=https://dzen.ru/a/aPkGhHYePCku9uSe]https://dzen.ru/a/aPkGhHYePCku9uSe[/url] .
Kapitalnii remont benzinovih dvigatelei v Moskve_gkEn
24 Oct 25 at 8:29 am
Hey there! I’m at work browsing your blog from my new iphone!
Just wanted to say I love reading through your
blog and look forward to all your posts! Carry on the great work!
tình dục trẻ em
24 Oct 25 at 8:29 am
1xbet t?rkiye [url=http://1xbet-giris-7.com]1xbet t?rkiye[/url] .
1xbet giris_waKn
24 Oct 25 at 8:32 am
1x giri? [url=www.1xbet-giris-9.com/]www.1xbet-giris-9.com/[/url] .
1xbet giris_wwon
24 Oct 25 at 8:33 am
Hello my loved one! I wish to say that this post is amazing, nice written and include almost all significant infos.
I’d like to see more posts like this .
tritium disposal
24 Oct 25 at 8:33 am
kraken ios
kraken onion
JamesDaync
24 Oct 25 at 8:34 am
Turn your cleaning into a game with Roomsy. A cozy way to clean your home with Roomsy. https://getroomsy.com/ Roomsy offers a simple, guided checklist that organizes chores and makes keeping a tidy home feel effortless. Cute virtual pet companion. Your pet lives in a tiny version of your home and joins you during cleaning sessions to make tidying more fun and engaging. Level up each room and watch your virtual pet grow happier as your real space gets cleaner, turning daily chores into a rewarding habit.
syqomsRax
24 Oct 25 at 8:35 am
Hi it’s me, I am also visiting this web site daily, this site is actually nice and the users are actually sharing good thoughts.
dubai layover tour from airport
24 Oct 25 at 8:36 am
Этот краткий обзор предлагает сжатую информацию из области медицины, включая ключевые факты и последние новости. Мы стремимся сделать информацию доступной и понятной для широкой аудитории, что позволит читателям оставаться в курсе актуальных событий в здравоохранении.
Детальнее – https://vitalady.ru/novoe/pomoshh-pri-alkogolnoj-zavisimosti-v-permi.html
Barbaracoege
24 Oct 25 at 8:37 am