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!
ExploreAmazingIdeas – Customer support responded quickly, very helpful and polite throughout the process.
Earl Botz
26 Oct 25 at 5:53 pm
клиника наркологическая москва [url=https://narkologicheskaya-klinika-23.ru/]https://narkologicheskaya-klinika-23.ru/[/url] .
narkologicheskaya klinika_alet
26 Oct 25 at 5:54 pm
sportwetten ohne oasis paysafecard
Feel free to visit my blog post … live wetten anbieter (Dillon)
Dillon
26 Oct 25 at 5:55 pm
kraken РФ
кракен 2025
JamesDaync
26 Oct 25 at 5:55 pm
платный наркологический диспансер москва [url=https://narkologicheskaya-klinika-24.ru/]https://narkologicheskaya-klinika-24.ru/[/url] .
narkologicheskaya klinika_tnSr
26 Oct 25 at 5:55 pm
Wow, this post is pleasant, my younger sister is analyzing these things, so I am going to tell her.
buôn bán nội tạng
26 Oct 25 at 5:56 pm
1xbet ?ye ol [url=1xbet-14.com]1xbet-14.com[/url] .
1xbet_fvet
26 Oct 25 at 5:57 pm
кракен ссылка
кракен актуальная ссылка
Henryamerb
26 Oct 25 at 5:58 pm
https://bioniclerpg.getbb.ru/viewtopic.php?f=6&t=8651
FrancisRag
26 Oct 25 at 5:59 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra41-at.net]kra37[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra41—cc.ru]kra37 at[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
[url=https://kra-41at.net]kra42[/url]
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
[url=https://kra42-at.cc]kra38 сс[/url]
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra40 cc
https://kra—42-at.ru
Ronniefluem
26 Oct 25 at 6:00 pm
https://kssp.borda.ru/?1-5-0-00002833-000-0-0-1756944940
FrancisRag
26 Oct 25 at 6:00 pm
gratis bonus ohne einzahlung strategien sportwetten – https://Www.kompster.com –
https://Www.kompster.com
26 Oct 25 at 6:02 pm
Вызвать дезинфекция школы дешево, где?
уничтожение клещей на даче
KennethceM
26 Oct 25 at 6:03 pm
1xbetgiri? [url=http://1xbet-17.com]1xbetgiri?[/url] .
1xbet_wlpl
26 Oct 25 at 6:04 pm
поставка медицинского оборудования [url=www.medoborudovanie-postavka.ru]поставка медицинского оборудования[/url] .
postavka medicinskogo oborydovaniya_nzsn
26 Oct 25 at 6:04 pm
sfdh10.pw – Navigation felt smooth, found everything quickly without any confusing steps.
Phillis Demonbreun
26 Oct 25 at 6:05 pm
It’s a pity you don’t have a donate button! I’d without a doubt donate to this outstanding blog!
I suppose for now i’ll settle for bookmarking and adding your
RSS feed to my Google account. I look forward to fresh updates and will share this website with
my Facebook group. Talk soon!
fast payout casinos online
26 Oct 25 at 6:05 pm
1x bet [url=http://1xbet-17.com]1x bet[/url] .
1xbet_lhpl
26 Oct 25 at 6:05 pm
поставка медоборудования [url=www.medoborudovanie-postavka.ru/]www.medoborudovanie-postavka.ru/[/url] .
postavka medicinskogo oborydovaniya_cosn
26 Oct 25 at 6:06 pm
кракен даркнет маркет
kraken darknet
Henryamerb
26 Oct 25 at 6:06 pm
Editor’s Choice here: https://www.elboomeran.com
CedricSyday
26 Oct 25 at 6:07 pm
кракен онлайн
кракен официальный сайт
Henryamerb
26 Oct 25 at 6:07 pm
The most valuable is here: https://cascadeclimbers.com
JasonEmait
26 Oct 25 at 6:08 pm
I’ve been exploring for a little for any high quality articles or weblog
posts in this sort of area . Exploring in Yahoo I ultimately stumbled upon this
website. Studying this information So i’m satisfied to convey that I’ve an incredibly excellent uncanny feeling I
found out just what I needed. I such a lot
definitely will make sure to don?t overlook this website and provides it a glance on a continuing basis.
kra32 at
26 Oct 25 at 6:09 pm
кракен актуальная ссылка
kraken ios
Henryamerb
26 Oct 25 at 6:11 pm
поставка медицинского оборудования [url=https://medoborudovanie-postavka.ru/]поставка медицинского оборудования[/url] .
postavka medicinskogo oborydovaniya_lusn
26 Oct 25 at 6:12 pm
one x bet [url=https://www.1xbet-17.com]one x bet[/url] .
1xbet_owpl
26 Oct 25 at 6:13 pm
Quality articles or reviews is the secret to interest the visitors
to go to see the site, that’s what this site is providing.
Find Out More
26 Oct 25 at 6:13 pm
To ensure maximum performance of the 1xbet application for iOS, OS is required.Three either more later [url=https://a9c.c91.myftpupload.com/1bet5/ultimate-guide-to-1xbet-casino-malaysia-your/]https://a9c.c91.myftpupload.com/1bet5/ultimate-guide-to-1xbet-casino-malaysia-your/[/url] and iPod touch.
Rosehaise
26 Oct 25 at 6:14 pm
кракен даркнет маркет
кракен маркет
JamesDaync
26 Oct 25 at 6:15 pm
Yoga for back and cervical pain
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
Yoga for back and cervical pain
26 Oct 25 at 6:16 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra–42—cc.ru]kra36 сс[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra-41-cc.net]kra41 сс[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra38
https://kra41-cc.com
Jamessew
26 Oct 25 at 6:16 pm
кракен vk3
kraken официальный
Henryamerb
26 Oct 25 at 6:17 pm
поставка медоборудования [url=https://medoborudovanie-postavka.ru/]https://medoborudovanie-postavka.ru/[/url] .
postavka medicinskogo oborydovaniya_jnsn
26 Oct 25 at 6:18 pm
Поделитесь отзывами о дезинфекция после потопа! Как качество и цена?
травля тараканов
KennethceM
26 Oct 25 at 6:18 pm
sportwetten online seriös
Here is my site Pferderennen wetten
Pferderennen wetten
26 Oct 25 at 6:19 pm
1xbet t?rkiye giri? [url=www.1xbet-14.com/]1xbet t?rkiye giri?[/url] .
1xbet_iyet
26 Oct 25 at 6:20 pm
Hello there! I know this is kinda off topic but I was wondering
if you knew where I could find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having
trouble finding one? Thanks a lot!
casino online schweiz
26 Oct 25 at 6:20 pm
https://herengezondheid.shop/# Viagra online kopen Nederland
Hermanereli
26 Oct 25 at 6:21 pm
I got this web site from my pal who shared with me on the topic of this web site and now
this time I am visiting this web page and reading very informative
posts at this place.
Result Macau live hari ini
26 Oct 25 at 6:21 pm
Как купить A-PVP в Тайге?Смотрите, что нашел – сайт https://hossevenhome.ru
. Цены порадовали, доставка заявлена. Кто-то покупал у них? Как у них с надежностью?
Stevenref
26 Oct 25 at 6:22 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra—41cc.ru]kra40 сс[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra-41at.net]kra41 cc[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra41 at
https://kra42-at.com
Kennethtip
26 Oct 25 at 6:22 pm
клиника наркологическая платная [url=http://www.narkologicheskaya-klinika-24.ru]http://www.narkologicheskaya-klinika-24.ru[/url] .
narkologicheskaya klinika_ouSr
26 Oct 25 at 6:23 pm
поставщик медоборудования [url=https://medoborudovanie-postavka.ru/]medoborudovanie-postavka.ru[/url] .
postavka medicinskogo oborydovaniya_xxsn
26 Oct 25 at 6:24 pm
InspireEverydayLife – Navigation is smooth, layout is clean and visually very appealing overall.
Bess Reusch
26 Oct 25 at 6:24 pm
Ich freue mich riesig uber Cat Spins Casino, es bietet eine dynamische Erfahrung. Die Auswahl ist einfach unschlagbar, mit Spielen, die Krypto unterstutzen. Er steigert das Spielvergnugen sofort. Die Mitarbeiter sind schnell und kompetent. Gewinne kommen ohne Verzogerung, dennoch mehr Bonusoptionen waren top. Zusammengefasst, Cat Spins Casino sorgt fur ununterbrochenen Spa?. Daruber hinaus die Oberflache ist benutzerfreundlich, was jede Session spannender macht. Ein super Vorteil ist das VIP-Programm mit besonderen Vorteilen, das die Motivation steigert.
Vertiefen|
WildFirear1zef
26 Oct 25 at 6:24 pm
наркологическая клиника trezviy vibor [url=www.narkologicheskaya-klinika-23.ru]www.narkologicheskaya-klinika-23.ru[/url] .
narkologicheskaya klinika_mbet
26 Oct 25 at 6:25 pm
кракен ссылка
кракен маркетплейс
Henryamerb
26 Oct 25 at 6:25 pm
kraken РФ
кракен официальный сайт
Henryamerb
26 Oct 25 at 6:26 pm
Ich schatze die Energie bei Cat Spins Casino, es ladt zu unvergesslichen Momenten ein. Das Spieleportfolio ist unglaublich breit, mit Spielen, die Krypto unterstutzen. Mit einfachen Einzahlungen. Der Kundensupport ist top. Transaktionen laufen reibungslos, manchmal gro?zugigere Angebote waren klasse. Kurz gesagt, Cat Spins Casino ist ein Ort fur pure Unterhaltung. Au?erdem die Navigation ist einfach und klar, das Spielerlebnis steigert. Ein tolles Feature sind die zuverlassigen Krypto-Zahlungen, zuverlassige Transaktionen sichern.
Eintauchen|
GlobalTigeron6zef
26 Oct 25 at 6:26 pm