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!
Navigating the bridge between traditional finance and the digital
asset world has always been a logistical nightmare for many
in the GSA community. The endless delays and lack of integration between fiat and crypto
platforms can severely hinder operational efficiency. This is precisely why the Paybis
fintech platform is a game-changer. They aren’t just another crypto
exchange; they’ve built a truly unified gateway that
masterfully consolidates both fiat and cryptocurrency banking.
Imagine managing treasury across USD, EUR, and a vast selection of major digital assets—all from a streamlined platform.
Their focus on robust security measures means
you can transact with confidence. A brief comment can’t possibly do justice to the full scope of their offerings,
especially their advanced tools for high-volume traders.
To get a complete picture of how Paybis is building the future of finance, you absolutely
need to read the detailed analysis in the full article.
It breaks down their KYC process, supported regions,
and API integration in a way that is incredibly insightful.
I highly recommend check out the piece to see if their platform aligns with your operational requirements.
It’s a comprehensive overview for anyone in our field looking to stay ahead of
the curve. The link is in the main post—you won’t regret it.
press release
26 Oct 25 at 3:59 am
1xbet giri? [url=https://www.1xbet-15.com]1xbet giri?[/url] .
1xbet_dkpl
26 Oct 25 at 4:01 am
1x bet giri? [url=http://1xbet-12.com]http://1xbet-12.com[/url] .
1xbet_peSr
26 Oct 25 at 4:01 am
частная наркологическая клиника москва [url=http://narkologicheskaya-klinika-23.ru]http://narkologicheskaya-klinika-23.ru[/url] .
narkologicheskaya klinika_gget
26 Oct 25 at 4:01 am
1xbet ?yelik [url=https://www.1xbet-15.com]1xbet ?yelik[/url] .
1xbet_nqpl
26 Oct 25 at 4:03 am
кракен сайт
кракен Россия
JamesDaync
26 Oct 25 at 4:03 am
mostbet лицензия [url=https://mostbet12031.ru]https://mostbet12031.ru[/url]
mostbet_kg_siMa
26 Oct 25 at 4:04 am
Кто делал дератизация цена холодным туманом? Эффективно ли?
обработка от плесени
KennethceM
26 Oct 25 at 4:04 am
купить диплом с занесением в реестр в архангельске [url=www.frei-diplom3.ru]купить диплом с занесением в реестр в архангельске[/url] .
Diplomi_vbKt
26 Oct 25 at 4:04 am
usaonlinecasinos.org – Appreciate the typography choices; comfortable spacing improved my reading experience.
Jenifer Vicenteno
26 Oct 25 at 4:05 am
Ищу обработка участков от клещей с выездом в область. https://dezchel.ru
KennethceM
26 Oct 25 at 4:05 am
1 x bet giri? [url=http://1xbet-13.com/]http://1xbet-13.com/[/url] .
1xbet_ucKa
26 Oct 25 at 4:07 am
Highly energetic blog, I liked that bit. Will there be a part
2?
seo-crowd.com.ua
26 Oct 25 at 4:08 am
bahis sitesi 1xbet [url=https://1xbet-15.com/]bahis sitesi 1xbet[/url] .
1xbet_kopl
26 Oct 25 at 4:08 am
1xbet giri? yapam?yorum [url=https://1xbet-10.com/]1xbet-10.com[/url] .
1xbet_ndea
26 Oct 25 at 4:10 am
медсестра которая купила диплом врача [url=https://frei-diplom15.ru]медсестра которая купила диплом врача[/url] .
Diplomi_bboi
26 Oct 25 at 4:11 am
1xbet guncel [url=www.1xbet-15.com/]1xbet guncel[/url] .
1xbet_rhpl
26 Oct 25 at 4:12 am
мостбет андроид [url=mostbet12032.ru]mostbet12032.ru[/url]
mostbet_kg_ufmt
26 Oct 25 at 4:12 am
Как купить Кетамин в Майкопе?Как думаете, нормально ли пользоваться https://vo-production.ru
? Цены понравились, доставку обещают. Но не уверен насчет качества.
Stevenref
26 Oct 25 at 4:13 am
1xbet guncel [url=http://1xbet-16.com]1xbet guncel[/url] .
1xbet_hnOn
26 Oct 25 at 4:13 am
кракен Москва
кракен даркнет
JamesDaync
26 Oct 25 at 4:16 am
kraken ссылка
кракен даркнет
JamesDaync
26 Oct 25 at 4:16 am
мостбет вход, регистрация [url=www.mostbet12031.ru]www.mostbet12031.ru[/url]
mostbet_kg_ziMa
26 Oct 25 at 4:17 am
birxbet giri? [url=www.1xbet-12.com]www.1xbet-12.com[/url] .
1xbet_kfSr
26 Oct 25 at 4:17 am
1xbet ?yelik [url=https://www.1xbet-15.com]1xbet ?yelik[/url] .
1xbet_gwpl
26 Oct 25 at 4:18 am
What’s up all, here every person is sharing these familiarity, so it’s nice to read this webpage, and I used to go to see this weblog everyday.
cross border e commerce
26 Oct 25 at 4:21 am
1xbet resmi sitesi [url=https://1xbet-15.com]https://1xbet-15.com[/url] .
1xbet_vspl
26 Oct 25 at 4:21 am
Вызвать санэпидемстанция круглосуточно на дом, кто знает номер?
дератизация
KennethceM
26 Oct 25 at 4:22 am
xbet [url=1xbet-10.com]xbet[/url] .
1xbet_ovea
26 Oct 25 at 4:22 am
xbet [url=www.1xbet-15.com/]xbet[/url] .
1xbet_wwpl
26 Oct 25 at 4:23 am
После санэпидемстанция цены дом безопасный для детей.
дезинфекция от коронавируса
KennethceM
26 Oct 25 at 4:23 am
диплом колледжа купить с занесением в реестр [url=http://frei-diplom3.ru/]диплом колледжа купить с занесением в реестр[/url] .
Diplomi_nvKt
26 Oct 25 at 4:23 am
Boost your play with the official https://linkmate.mn.co/posts/92559380?utm_source=manual. Take advantage of free bets, cashback, and extra spins for a perfect start.
Justincit
26 Oct 25 at 4:23 am
At this time it sounds like BlogEngine is the top blogging platform available right now.
(from what I’ve read) Is that what you’re using on your blog?
visit website
26 Oct 25 at 4:24 am
Обработка обработка от клопов частного дома эффективная, рекомендую.
дезинфекция после животного
KennethceM
26 Oct 25 at 4:25 am
Asking questions are really good thing if you are not understanding anything entirely, except this paragraph provides pleasant understanding yet.
business goals
26 Oct 25 at 4:25 am
1xbwt giri? [url=1xbet-13.com]1xbet-13.com[/url] .
1xbet_jfKa
26 Oct 25 at 4:26 am
Wonderful goods from you, man. I’ve consider your stuff prior to and
you’re simply too great. I actually like what
you’ve received right here, really like what you’re stating and
the way in which you assert it. You’re making it enjoyable and you continue to care for to stay it wise.
I can not wait to read far more from you. That is really a tremendous
website.
PG99
26 Oct 25 at 4:29 am
swissreplica.pw – Found practical insights today; sharing this article with colleagues later.
Nathanial Giddings
26 Oct 25 at 4:30 am
1xbet giri?i [url=https://1xbet-16.com]1xbet giri?i[/url] .
1xbet_vcOn
26 Oct 25 at 4:30 am
1xbet resmi [url=www.1xbet-10.com/]www.1xbet-10.com/[/url] .
1xbet_ytea
26 Oct 25 at 4:30 am
Сеть стоматологических клиник Dinstom https://dinstom.ru/ предлагает эффективное и безопасное лечение с использованием инновационных технологий в максимально комфортной атмосфере. Ознакомьтесь на сайте с нашими услугами – от гигиены, лечения и исправления прикуса до имплантации и комплексного протезирования винирами и коронками. У нас выгодные цены и профессиональные врачи стоматологи.
mehomwaype
26 Oct 25 at 4:31 am
I think this is among the so much significant info for
me. And i am glad reading your article. But want to remark on few
basic issues, The site taste is wonderful, the articles is actually excellent : D.
Just right task, cheers
сборные грузоперевозки по россии
26 Oct 25 at 4:31 am
1xbet turkey [url=http://1xbet-15.com]1xbet turkey[/url] .
1xbet_vzpl
26 Oct 25 at 4:32 am
kraken ссылка
kraken vk5
JamesDaync
26 Oct 25 at 4:33 am
lovesomething.click
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
lovesomething.click
26 Oct 25 at 4:34 am
Whats up are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get started and
create my own. Do you need any html coding expertise to make your
own blog? Any help would be really appreciated!
tubidy music
26 Oct 25 at 4:34 am
kraken onion
kraken marketplace
JamesDaync
26 Oct 25 at 4:34 am
1xbet g?ncel adres [url=https://1xbet-12.com/]1xbet g?ncel adres[/url] .
1xbet_agSr
26 Oct 25 at 4:35 am
После уничтожение насекомых мыши пропали, рад!
санобработка
KennethceM
26 Oct 25 at 4:36 am