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!
https://form.jotform.com/252556805133052
RonaldWep
19 Sep 25 at 12:53 am
https://xn--krken23-bn4c.com
Howardreomo
19 Sep 25 at 12:54 am
This article will help the internet visitors for building up new weblog or even a weblog from start
to end.
Feel free to surf to my web-site; sustainable waste management
sustainable waste management
19 Sep 25 at 12:56 am
It’s a pity you don’t have a donate button! I’d definitely donate to
this superb blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my
Google account. I look forward to fresh updates and will share this blog with
my Facebook group. Chat soon!
آدرس دانشگاه الزهرا
19 Sep 25 at 12:57 am
where to buy erectile dysfunction pills: VitalEdge Pharma – VitalEdgePharma
Dennisted
19 Sep 25 at 12:59 am
Hi there, I would like to subscribe for this website
to get most up-to-date updates, so where can i do it please help.
QubiQuant Deep
19 Sep 25 at 1:03 am
Хотите, чтобы ваш бренд был на виду? Наша [url=https://баннер-москва.рф/price]печать на оракале[/url] — это решение №1 в Москве. Мы используем лучшее оборудование, создаем насыщенные цвета и четкие линии, которые невозможно не заметить. Конкуренты обещают, а мы гарантируем. Мы делаем все быстрее, надежнее и эффективнее, чем кто-либо на рынке. Не тратьте время на посредников — обращайтесь к лидеру.
DonaldLip
19 Sep 25 at 1:04 am
https://xn--krken23-bn4c.com
Howardreomo
19 Sep 25 at 1:08 am
Carnival Cat Bonus Combo игра
Eddiefen
19 Sep 25 at 1:09 am
gvh axx
youtubezkm
19 Sep 25 at 1:10 am
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
blsp at
bs2best.at blacksprut Official
Jamesner
19 Sep 25 at 1:13 am
Great post. I used to be checking continuously this blog and
I’m inspired! Very useful information specially the final section 🙂 I care
for such information a lot. I used to be looking for this certain information for a very lengthy
time. Thanks and good luck.
dewascatter link alternatif
19 Sep 25 at 1:14 am
накрутка подписчиков в тг группу
Stevenplolf
19 Sep 25 at 1:15 am
https://xn--krken21-bn4c.com
Howardreomo
19 Sep 25 at 1:16 am
Можно также получить дополнительный
презент, применив бонус код для казино.
7к
19 Sep 25 at 1:18 am
https://form.jotform.com/252566251367057
RonaldWep
19 Sep 25 at 1:18 am
Tourists fined and banned from Venice for swimming in canal
[url=https://trip-scan.co]tripscan top[/url]
A couple from the United Kingdom had to cut their vacation in Venice short after being caught swimming in the Grand Canal.
The 35-year-old British man and his 25-year-old Romanian girlfriend were forced to return to their home in the UK on Thursday, the same day they arrived in the city, after gondoliers reported them to local police for taking a dip in the canal.
The pair were fined €450 ($529) each and expelled from Venice for 48 hours, marking the 1,136th such sanction to be handed down to badly behaved tourists in the city so far this year, according to the Venice City Police.
The unnamed couple took the plunge near the Accademia bridge near St. Mark’s Square and gondoliers at the Rio San Vidal kiosk immediately called authorities, who removed them from the water.
“I thank the gondoliers for their cooperation and timely reporting,” said Venice Security Councillor Elisabetta Pesce in a statement published by city authorities on Friday.
https://trip-scan.co
трип скан
“Venice must be defended from those who disrespect it: protecting the city means ensuring decorum for residents and visitors who experience it with civility.”
Swimming in the Venice canals is prohibited for a variety of reasons, including the intense boat traffic and the cleanliness — or lack thereof — of the water, according to the city’s tourism ministry.
Of the 1,136 orders of expulsion from the city so far this year, about 10 were for swimming.
Related article
Tourists take photographs on the Rialto Bridge in Venice, Italy, on Saturday, April 8, 2023. Italy’s upcoming budget outlook will probably incorporate a higher growth forecast for 2023 followed by a worsened outlook for subsequent years, according to people familiar with the matter. Photographer: Andrea Merola/Bloomberg via Getty Images
Rising waters and overtourism are killing Venice. Now the fight is on to save its soul
“Since the beginning of the year, we have issued a total of 1,136 orders of expulsion for incidents of degradation and uncivilized behavior,” Venice local police deputy commander Gianni Franzoi said in a statement shared with CNN.
Poor visitor behavior is one of the worst byproducts of overtourism, Franzoi said, and incidents are on the rise.
In July 2024, an Australian man was fined and expelled for diving off the Rialto Bridge after his friends posted about it on social media.
The year before, two French tourists were fined and expelled for skinny dipping in the canal under the moonlight. In August 2022, a German man was fined and expelled for surfing in the canal.
Related article
Aerial view of the plagued ghost island of Poveglia in the Venetian lagoon
‘Haunted’ Venice island to become a locals-only haven where tourists are banned
Venice’s authorities have been trying to balance the need for visitor income with residents’ demands for a city that works for them.
Day trippers now pay a €10 entrance fee on summer weekends and during busy periods throughout the year.
The city has also banned tour groups of more than 25 people, loudspeakers and megaphones, and even standing on narrow streets to listen to tour guides.
“It was necessary to establish a system of penalties that would effectively deter potential violations,” Pesce said when the ordinance was passed in February.
“Our goal remains to combat all forms of irregularities related to overtourism in the historic lagoon city center,” she added.
“The new rules for groups accompanied by guides encourage a more sustainable form of tourism, while also ensuring greater protection and safety in the city and better balancing the needs of Venice residents and visitors.”
BryanCanda
19 Sep 25 at 1:18 am
https://andrespygdw.pointblog.net/la-mejor-parte-de-coaching-para-directivo-79330775
El poder del coaching gerencial está transformando la forma en que las empresas locales dirigen a sus colaboradores.
Hoy, hablar de coaching ejecutivo no es una tendencia, es una clave fundamental para lograr resultados en un escenario cada vez más competitivo.
¿Por qué el coaching gerencial funciona?
Ayuda a los líderes a manejar mejor su agenda.
Mejora la interacción con equipos.
Fortalece el rol directivo en procesos difíciles.
Reduce el desgaste en jefaturas.
Resultados del coaching organizacional en Chile
Más alta retención de colaboradores.
Ambiente organizacional positivo.
Colaboradores sincronizados con los metas corporativos.
Formación de jefaturas que lideran nuevas metas.
Casos donde el coaching para directivo marca la gran diferencia
Un gerente que necesita resolver conflictos con stakeholders.
Una subgerencia que tiene que conducir equipos multigeneracionales.
Un ejecutivo que se enfrenta un proceso de expansión.
La forma en que implementar coaching ejecutivo en tu compañía
Detectar objetivos claros.
Seleccionar un facilitador validado.
Diseñar planes adaptados.
Revisar cambios en periodos concretos.
Un plan de coaching para directivo puede ser la clave entre sobrevivir o ganar.
JuniorShido
19 Sep 25 at 1:21 am
Близкий человек в запое? Не ждите ухудшения. Обратитесь в клинику — здесь проведут профессиональный вывод из запоя с последующим восстановлением организма.
Исследовать вопрос подробнее – [url=https://vyvod-iz-zapoya-krasnodar16.ru/]вызвать нарколога на дом в краснодаре[/url]
RichardRig
19 Sep 25 at 1:23 am
В Twitter заметил обсуждение, где рекомендовали [url=https://баннер-москва.рф/mobilnye-stendy/roll-up]роллап[/url] как самый удобный формат мобильного стенда. Проверил на практике — действительно так. Легкий, компактный, собирать проще простого, а дизайн выглядит стильно. Теперь даже для внутренних встреч в офисе использую его — удобно демонстрировать проекты и акцентировать внимание. Отличное решение для бизнеса.
DonaldLip
19 Sep 25 at 1:25 am
If you’re searching for a trustworthy and powerful financial service that handles not only cryptocurrency transactions like
buying Bitcoin but also supports a wide range of fiat operations, then you should definitely check out this topic where users share their opinion about
a truly all-in-one crypto-financial platform.
I found the forum topic to be incredibly insightful because it
covers not just the basics of buying crypto, but also
the extended features like multi-currency fiat support,
bulk payment processing, and advanced tools for businesses.
Whether you’re running a startup or managing finances for a multinational corporation, the
features highlighted in this discussion could be a game-changer –
multi-user accounts, compliance tools, fiat gateways, and crypto custody all in one.
This topic could be particularly useful for anyone seeking a compliant,
scalable, and secure solution for managing both crypto and fiat funds.
The website being discussed is built to handle everything
from simple BTC purchases to large-scale B2B transactions.
Highly suggest taking a look if you’re involved in finance, tech, or enterprise operations.
The recommendation alone is worth checking out.
discussion
19 Sep 25 at 1:28 am
Ahaa, its pleasant dialogue concerning this piece of writing here
at this website, I have read all that, so at this time me
also commenting here.
Check out my site – renewable energy
renewable energy
19 Sep 25 at 1:32 am
What’s Going down i am new to this, I stumbled upon this I have discovered It absolutely useful and it has helped me out loads.
I’m hoping to contribute & aid other users like its aided me.
Good job.
https://me88.qpon/
19 Sep 25 at 1:33 am
накрутка подписчиков тг без регистрации
GustavoRiz
19 Sep 25 at 1:38 am
Thanks for another excellent post. The place else may just anyone get that kind of information in such an ideal way
of writing? I’ve a presentation subsequent week, and I am on the look
for such info.
Anchor Bitrow
19 Sep 25 at 1:40 am
Затяжной запой опасен для жизни. Врачи наркологической клиники в Краснодаре проводят срочный вывод из запоя — на дому или в стационаре. Анонимно, безопасно, круглосуточно.
Узнать больше – [url=https://vyvod-iz-zapoya-krasnodar15.ru/]врач нарколог на дом город краснодар[/url]
BrandonAttet
19 Sep 25 at 1:40 am
I like the valuable information you provide in your articles.
I’ll bookmark your blog and check again here frequently. I’m quite certain I’ll learn lots of new stuff right here!
Best of luck for the next!
My blog post; environmental sustainability
environmental sustainability
19 Sep 25 at 1:43 am
https://rant.li/abjyodecuh/chapaevsk-kupit-gashish-boshki-marikhuanu
RonaldWep
19 Sep 25 at 1:43 am
Saved as a favorite, I love your website!
deck installation contractor Windsor
19 Sep 25 at 1:43 am
Ever Trust Meds: Cialis without a doctor prescription – Cialis 20mg price
DerekStops
19 Sep 25 at 1:49 am
It’s awesome in support of me to have a web site, which is good in favor
of my knowledge. thanks admin
dewascatter daftar
19 Sep 25 at 1:51 am
https://word36789.suomiblog.com/coaching-grupal-la-clave-para-potenciar-equipos-y-resolver-conflictos-en-las-organizaciones-52331745
El coaching grupal online esta revolucionando la dinamica en que las companias latinas lideran sus colaboradores.
Hoy, optar por un coaching grupal online no es un beneficio, sino una estrategia critica para alcanzar resultados en escenarios cada vez mas dinamicos.
?Por que elegir coaching de equipos?
Consolida la comunicacion entre areas.
Resuelve conflictos internos.
Mejora la sinergia en procesos.
Activa respeto dentro del grupo.
Ventajas de un coaching para grupos
Areas mas conectados con los planes estrategicos.
Reduccion de salidas.
Ambiente laboral productivo.
Mayor prevencion de conflictos.
Ejemplos donde el coaching grupal online hace la clave
Unidades con tensiones entre lideres.
Proyectos que trabajan en formato hibrido.
Empresas que padecen altas tasas de rotacion.
La forma de implementar programa de coaching de equipos en tu organizacion
Definir objetivos claros.
Seleccionar un coach especializado.
Implementar sesiones virtuales adaptados a las necesidades.
Monitorear el resultado en plazos claros.
El programa de coaching de equipos es un salvavidas que transforma la forma de colaborar. Un coaching grupal online bien aplicado puede ser en la clave entre sobrevivir o escalar.
JuniorShido
19 Sep 25 at 1:52 am
https://evertrustmeds.com/# EverTrustMeds
RichardceaNy
19 Sep 25 at 1:53 am
Казино Cat слот Cash Streak
Davidfes
19 Sep 25 at 1:55 am
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2best.at blacksprut marketplace Official
CharlesNarry
19 Sep 25 at 1:56 am
https://kulick.ru
JoshuaStism
19 Sep 25 at 1:56 am
Близкий человек в запое? Не ждите ухудшения. Обратитесь в клинику — здесь проведут профессиональный вывод из запоя с последующим восстановлением организма.
Ознакомиться с деталями – [url=https://vyvod-iz-zapoya-krasnodar12.ru/]vrach-narkolog-na-dom krasnodar[/url]
FrankBoymn
19 Sep 25 at 1:57 am
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You clearly know what youre talking about, why throw away your intelligence on just posting videos to your site when you
could be giving us something informative to read?
canadian online pharmacies
19 Sep 25 at 1:59 am
Chilli Joker slot rating
BrandonLum
19 Sep 25 at 2:02 am
A person necessarily assist to make severely articles I’d state.
This is the very first time I frequented your web page and to this point?
I surprised with the research you made to
make this actual put up amazing. Great task!
Mevryon Platform
19 Sep 25 at 2:05 am
https://xn--krken21-bn4c.com
Howardreomo
19 Sep 25 at 2:05 am
Have you ever considered creating an ebook or guest authoring on other websites?
I have a blog centered on the same information you discuss and would love to have
you share some stories/information. I know my audience would appreciate your work.
If you are even remotely interested, feel
free to send me an e mail.
LunavestiaMax
19 Sep 25 at 2:05 am
Have you ever considered about including a little bit more than just your
articles? I mean, what you say is valuable and all. Nevertheless imagine if you added
some great visuals or video clips to give your posts more, “pop”!
Your content is excellent but with pics and video clips, this
site could definitely be one of the greatest in its niche.
Excellent blog!
آدرس مرکز آموزش عالی شهید باهنر تهران
19 Sep 25 at 2:05 am
Thanks a lot for sharing this with all of us you really realize what you’re talking
approximately! Bookmarked. Please additionally seek advice from my site =).
We could have a hyperlink change contract between us
Anchor Bitrow
19 Sep 25 at 2:06 am
https://www.band.us/page/99943766/
RonaldWep
19 Sep 25 at 2:08 am
накрутка премиум подписчиков тг
JohnnyPhido
19 Sep 25 at 2:08 am
My spouse and I stumbled over here coming from a different
web page and thought I may as well check things out. I like what I
see so now i’m following you. Look forward to going over your web page again.
casino zonder cruks
19 Sep 25 at 2:08 am
Лечение в клинике проводится последовательно, что позволяет контролировать процесс выздоровления и обеспечивать пациенту максимальную безопасность.
Подробнее можно узнать тут – [url=https://narkologicheskaya-klinika-v-doneczke0.ru/]наркологическая клиника нарколог[/url]
Dallasvam
19 Sep 25 at 2:09 am
Близкий человек в запое? Не ждите ухудшения. Обратитесь в клинику — здесь проведут профессиональный вывод из запоя с последующим восстановлением организма.
Подробнее тут – [url=https://vyvod-iz-zapoya-krasnodar11.ru/]нарколог на дом анонимно в краснодаре[/url]
DouglasLon
19 Sep 25 at 2:12 am
I’m not sure exactly why but this web site is loading
very slow for me. Is anyone else having this problem or is it a problem on my end?
I’ll check back later and see if the problem still exists.
جواب امتحانات شهریور کی میاد مدرسه
19 Sep 25 at 2:13 am