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://www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]https://www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_rrml
23 Oct 25 at 1:10 am
купить диплом в майкопе [url=https://rudik-diplom12.ru]https://rudik-diplom12.ru[/url] .
Diplomi_ikPi
23 Oct 25 at 1:12 am
Appreciation to my father who stated to me regarding this website,
this webpage is in fact awesome.
Opulatrix TEST
23 Oct 25 at 1:12 am
юридический перевод на английский [url=teletype.in/@alexd78/HN462R01hzy]teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_mest
23 Oct 25 at 1:13 am
sildenafil 50 mg ou 100 mg posologie [url=https://santehommefrance.com/#]prix du Viagra générique en France[/url] sildenafil 50 mg ou 100 mg posologie
CharlesNeono
23 Oct 25 at 1:14 am
купить диплом колледжа искусств в спб [url=www.frei-diplom7.ru]www.frei-diplom7.ru[/url] .
Diplomi_xeei
23 Oct 25 at 1:14 am
We are a group of volunteers and starting a brand new scheme in our community.
Your web site offered us with valuable information to work on. You’ve done a formidable
task and our whole community shall be grateful to you.
Zeno Flow Engine
23 Oct 25 at 1:16 am
технический перевод ошибки [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ/]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ/[/url] .
Tehnicheskii perevod_txml
23 Oct 25 at 1:18 am
что такое медицинский перевод [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16]https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_hwEr
23 Oct 25 at 1:18 am
купить диплом в белово [url=http://rudik-diplom9.ru]http://rudik-diplom9.ru[/url] .
Diplomi_dtei
23 Oct 25 at 1:24 am
ім’я ясміна
JessieVinge
23 Oct 25 at 1:24 am
coatsjps.pw – I found some interesting sections already; content feels genuine and useful.
Demetrius Studler
23 Oct 25 at 1:24 am
diwang2.pw – Works great on mobile, responsive and well aligned across sections.
Sang Drohan
23 Oct 25 at 1:25 am
wqpyp.pw – The site loads fast and gives a modern, professional impression right away.
Junita Plateros
23 Oct 25 at 1:26 am
что такое технические перевод [url=http://dzen.ru/a/aPFFa3ZMdGVq1wVQ/]http://dzen.ru/a/aPFFa3ZMdGVq1wVQ/[/url] .
Tehnicheskii perevod_fpml
23 Oct 25 at 1:27 am
медицинский перевод заключений [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/]https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/[/url] .
Medicinskii perevod_buEr
23 Oct 25 at 1:27 am
Viagra sans ordonnance avis [url=http://santehommefrance.com/#]Viagra homme prix en pharmacie[/url] Viagra sans ordonnance avis
CharlesNeono
23 Oct 25 at 1:27 am
affordable potency tablets: Brit Meds Uk – BritMedsUk
AnthonySep
23 Oct 25 at 1:27 am
1xBet бонусный код на приветственный бонус в 2026 — активируйте промокод и активируйте приветственный подарок в размере 100% до 100$. Уникальный бонусный код позволяет активировать приветственный бонус от БК 1xBet при регистрации.Промокод всегда доступен на сайте 1xBet: куда вводить промокоды 1xbet.
WilliamFaw
23 Oct 25 at 1:27 am
Hello would you mind letting me know which web host you’re utilizing?
I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot faster then most.
Can you suggest a good internet hosting provider at a honest price?
Thanks a lot, I appreciate it!
Heroin
23 Oct 25 at 1:29 am
Услуга вывода из запоя в Екатеринбурге от «Похмельной Службы» включает как базовый, так и премиум-уровень помощи — со всеми необходимыми препаратами и поддержкой.
Углубиться в тему – [url=https://vyvod-iz-zapoya-ekaterinburg26.ru/]анонимный вывод из запоя в екатеринбурге[/url]
ClydeLak
23 Oct 25 at 1:29 am
Если вы ищете надежную клинику для вывода из запоя, обратитесь в «Детокс» в Краснодаре. Услуга вызова нарколога на дом доступна круглосуточно. Врачи приедут к вам в течение 1–2 часов и окажут необходимую помощь.
Ознакомиться с деталями – [url=https://narkolog-na-dom-krasnodar28.ru/]выезд нарколога на дом краснодар[/url]
Jeremytrete
23 Oct 25 at 1:30 am
перевод медицинских документов [url=www.teletype.in/@alexd78/HN462R01hzy/]www.teletype.in/@alexd78/HN462R01hzy/[/url] .
Vidi perevodov v buro Perevod i Pravo_vost
23 Oct 25 at 1:31 am
quotenvergleich surebets
My blog post: sportwetten bester bonus
sportwetten bester bonus
23 Oct 25 at 1:31 am
технический перевод ошибки [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ/]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ/[/url] .
Tehnicheskii perevod_suml
23 Oct 25 at 1:34 am
медицинский перевод заключений [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/]telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_jvEr
23 Oct 25 at 1:34 am
mouse click the next document
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
mouse click the next document
23 Oct 25 at 1:37 am
everydayvaluecorner – Great deals and very easy to navigate, found everything fast.
Dirk Frary
23 Oct 25 at 1:39 am
Как купить Мефедрон в Поляные?Как считаете про https://goodworkstudios.ru
? Цены нормальные, быстрая доставка. Кто-нибудь тестил? Интересует реальное качество?
Stevenref
23 Oct 25 at 1:40 am
технический английский перевод [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_ryml
23 Oct 25 at 1:42 am
медицинский перевод выписок [url=http://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16]http://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_hbEr
23 Oct 25 at 1:42 am
виды технического перевода [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_haml
23 Oct 25 at 1:44 am
требования медицинского перевода [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/]telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_qlEr
23 Oct 25 at 1:44 am
купить диплом колледжа рукой [url=http://frei-diplom7.ru/]http://frei-diplom7.ru/[/url] .
Diplomi_tdei
23 Oct 25 at 1:45 am
перевод английской научно технической литературы [url=http://www.teletype.in/@alexd78/HN462R01hzy]http://www.teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_cbst
23 Oct 25 at 1:46 am
Please let me know if you’re looking for a author for your weblog.
You have some really good posts and I feel I would be a good asset.
If you ever want to take some of the load off, I’d really like to write some content for your blog
in exchange for a link back to mine. Please blast me an email if interested.
Thank you!
data
23 Oct 25 at 1:48 am
перевод медицинский на русский язык [url=https://teletype.in/@alexd78/HN462R01hzy]https://teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_bjst
23 Oct 25 at 1:48 am
рейтинг лучших seo агентств [url=https://reiting-seo-kompaniy.ru]https://reiting-seo-kompaniy.ru[/url] .
reiting seo kompanii_cfon
23 Oct 25 at 1:49 am
use 9seed.click
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
use 9seed.click
23 Oct 25 at 1:50 am
Hi just wanted to give you a brief heads up and let you know a few of the
pictures aren’t loading properly. I’m not sure why but I think
its a linking issue. I’ve tried it in two different web
browsers and both show the same results.
시알리스 10mg 20mg 차이
23 Oct 25 at 1:50 am
Клиника «Детокс» в Сочи предлагает услугу вывода из запоя в стационаре. Под наблюдением профессиональных врачей пациент получит необходимую медицинскую помощь и поддержку. Услуга доступна круглосуточно, анонимно и начинается от 2000 ?.
Выяснить больше – [url=https://vyvod-iz-zapoya-sochi23.ru/]вывод из запоя вызов на дом[/url]
Keithskito
23 Oct 25 at 1:53 am
купить диплом логиста [url=http://rudik-diplom12.ru/]купить диплом логиста[/url] .
Diplomi_cwPi
23 Oct 25 at 1:53 am
https://factava.com.ua/vidpovidy/khto-taki-aytishnyky-vidkryyte-svit-it-profesiy-i-ikh-mozhlyvostey/
JessieVinge
23 Oct 25 at 1:53 am
I do not know if it’s just me or if everybody else encountering
problems with your site. It seems like some of the text on your
content are running off the screen. Can someone else please comment and let me know if this is happening to them as well?
This could be a problem with my web browser because I’ve
had this happen previously. Thank you
Proxel Bit
23 Oct 25 at 1:55 am
0pdpof.xyz – Found some interesting posts, definitely worth revisiting for updates.
Ashely Tekulve
23 Oct 25 at 1:56 am
технический перевод в машиностроении [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_kcml
23 Oct 25 at 1:56 am
медицинский перевод с английского [url=http://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16]http://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_hgEr
23 Oct 25 at 1:57 am
licensed online pharmacy UK: affordable potency tablets – Viagra online UK
WilliamUnjup
23 Oct 25 at 1:57 am
The Inheritance Games Canada: A thrilling mystery game where players unravel secrets, solve puzzles, and compete for a billionaire’s fortune. Perfect for fans of strategy and suspense: Jennifer Lynn Barnes author site
GabrielLyday
23 Oct 25 at 1:57 am
юридический перевод [url=https://teletype.in/@alexd78/HN462R01hzy/]teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_xost
23 Oct 25 at 1:59 am