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://kapelnica-ot-zapoya-lugansk-lnr0.ru/]врач на дом капельница от запоя в луганске[/url]
MatthewShuth
19 Sep 25 at 3:05 pm
купить диплом в севастополе [url=https://rudik-diplom1.ru]купить диплом в севастополе[/url] .
Diplomi_fler
19 Sep 25 at 3:08 pm
What’s up i am kavin, its my first occasion to commenting anyplace, when i
read this post i thought i could also create comment due
to this brilliant paragraph.
situs scam
19 Sep 25 at 3:09 pm
buy cialis pill [url=https://evertrustmeds.shop/#]Ever Trust Meds[/url] Ever Trust Meds
Michealstilm
19 Sep 25 at 3:10 pm
I every time spent my half an hour to read this web site’s posts all the time along with a mug of coffee.
소액결제현금화
19 Sep 25 at 3:10 pm
My partner and I stumbled over here different page and thought I should check things out.
I like what I see so now i am following you. Look forward to finding out
about your web page again.
online order medicine
19 Sep 25 at 3:11 pm
займы всем [url=https://zaimy-16.ru]займы всем[/url] .
zaimi_xoMi
19 Sep 25 at 3:11 pm
What a data of un-ambiguity and preserveness of valuable know-how concerning unpredicted
emotions.
online casino nederland legaal
19 Sep 25 at 3:14 pm
https://0bfba7f4abfeb3a9f615b7ca55.doorkeeper.jp/
HarryPaync
19 Sep 25 at 3:14 pm
This time, we bought warning; so, everyone gonna communicate English and this place
gonna look good.”Maria understood that there was no arguing this level and completed with the dishes. It’s “a little
bit of a laugh”.
GRATIS VIAGRA
19 Sep 25 at 3:15 pm
https://xn--krken21-bn4c.com
Howardreomo
19 Sep 25 at 3:15 pm
мфо займ [url=https://www.zaimy-16.ru]мфо займ[/url] .
zaimi_lrMi
19 Sep 25 at 3:18 pm
vardenafil for ed
best ed medication
19 Sep 25 at 3:19 pm
купить диплом автомеханика [url=http://www.rudik-diplom8.ru]купить диплом автомеханика[/url] .
Diplomi_glMt
19 Sep 25 at 3:20 pm
купить свидетельство о заключении брака [url=https://rudik-diplom7.ru/]купить свидетельство о заключении брака[/url] .
Diplomi_cvPl
19 Sep 25 at 3:21 pm
купить свидетельство о рождении ссср [url=www.rudik-diplom10.ru]купить свидетельство о рождении ссср[/url] .
Diplomi_ucSa
19 Sep 25 at 3:21 pm
Обращение за помощью к наркологу на дому имеет ряд преимуществ, особенно в экстренных ситуациях:
Узнать больше – [url=https://narcolog-na-dom-mariupol0.ru/]психиатр нарколог на дом мариуполь[/url]
Romanuteda
19 Sep 25 at 3:23 pm
Купить диплом университета поможем. Купить диплом бакалавра в Новосибирске – [url=http://diplomybox.com/kupit-diplom-bakalavra-v-novosibirske/]diplomybox.com/kupit-diplom-bakalavra-v-novosibirske[/url]
Cazrobw
19 Sep 25 at 3:24 pm
viagra vs cialis
diabetes and erectile dysfunction
19 Sep 25 at 3:26 pm
Me encanta el sitio 1win login, es muy cómodo.
juegos de casino
juegos de casino
19 Sep 25 at 3:30 pm
займы россии [url=http://zaimy-16.ru/]займы россии[/url] .
zaimi_ovMi
19 Sep 25 at 3:30 pm
Когда запой становится угрозой для жизни и здоровья, своевременная помощь профессионала может стать решающим фактором для скорейшего восстановления. В Мурманске, где суровые климатические условия добавляют стресса и осложнений, квалифицированные наркологи оказывают помощь на дому, обеспечивая оперативную детоксикацию и индивидуальную терапию в привычной обстановке. Такой подход позволяет пациентам избежать лишних перемещений и получить поддержку в комфортной атмосфере.
Изучить вопрос глубже – [url=https://vyvod-iz-zapoya-murmansk0.ru/]вывод из запоя круглосуточно[/url]
AlfredDierb
19 Sep 25 at 3:30 pm
за1мы онлайн [url=www.zaimy-16.ru/]www.zaimy-16.ru/[/url] .
zaimi_vtMi
19 Sep 25 at 3:31 pm
plug in prague https://cocaine-prague-shop.com
prague-drugs-71
19 Sep 25 at 3:33 pm
When I stumbled upon [url=https://ataspanking.art]spanking video[/url], I was blown away! The spanking-themed artwork and illustrations are so creative and detailed. If you love unique art with a spanking twist, this site is definitely worth checking out and discussing with fellow enthusiasts!
WilsonPaymn
19 Sep 25 at 3:33 pm
займы всем [url=http://zaimy-16.ru]займы всем[/url] .
zaimi_dfMi
19 Sep 25 at 3:34 pm
https://internet17272.ampedpages.com/acerca-de-empresa-de-reclutamiento-y-selecci%C3%B3n-52419542
La empresa de reclutamiento y selección es mucho más que publicar un anuncio en redes. En nuestro país, contratar mal a una candidato puede costar un dineral en recursos.
Por eso, cada vez más compañías buscan un consultora de selección de personal que asegure proceso eficiente y baje los problemas.
Razones para confiar en una empresa de seleccion de personal?
Acceso a perfiles que no buscan avisos tradicionales.
Metodologías probadas para filtrar competencias.
Rapidez en cerrar vacantes críticas.
Reducción de costos.
Resultados de un buen proceso de selección de personal
Ingresos más ajustados con la cultura interna.
Reducción de abandono temprano.
Departamentos más eficientes.
Imagen más competitiva.
Fallos comunes en la selección de personal en Chile
Depender solo en impresión.
Dejar de lado herramientas.
Pasar por alto la cultura de la empresa.
Acelerar la contratación por presión.
Cómo elegir una empresa de reclutamiento y selección
Revisa casos de éxito.
Verifica que usen pruebas psicométricas.
Evalúa la especialización en tu rubro.
Solicita procesos transparentes.
Un servicio de selección de personal es una apuesta que marca la clave entre sumar talento o pagar caro errores.
JuniorShido
19 Sep 25 at 3:37 pm
Казино 1xbet слот Chinese Treasures
BrandonLum
19 Sep 25 at 3:37 pm
все займы онлайн на карту [url=https://www.zaimy-16.ru]все займы онлайн на карту[/url] .
zaimi_paMi
19 Sep 25 at 3:38 pm
https://form.jotform.com/252563198013052
HarryPaync
19 Sep 25 at 3:40 pm
После первичной диагностики начинается активная фаза медикаментозного вмешательства. Современные препараты вводятся капельничным методом, что позволяет быстро снизить уровень токсинов в крови, восстановить нормальный обмен веществ и стабилизировать работу внутренних органов. Этот этап играет решающую роль в безопасном выводе из запоя.
Исследовать вопрос подробнее – https://vyvod-iz-zapoya-donetsk-dnr00.ru/vyvod-iz-zapoya-kruglosutochno-doneczk-dnr
Michaelduxuh
19 Sep 25 at 3:47 pm
займы всем [url=www.zaimy-16.ru/]займы всем[/url] .
zaimi_yrMi
19 Sep 25 at 3:49 pm
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2best
bs2best.at blacksprut Official
Jamesner
19 Sep 25 at 3:51 pm
Its like you read my mind! You seem to understand so much approximately this, like you
wrote the ebook in it or something. I feel that you
simply could do with a few p.c. to force the message house a little bit,
but instead of that, that is magnificent blog. An excellent read.
I’ll definitely be back.
Data Keluaran HK
19 Sep 25 at 3:52 pm
займы онлайн все [url=http://zaimy-16.ru/]http://zaimy-16.ru/[/url] .
zaimi_zzMi
19 Sep 25 at 3:53 pm
Получить диплом любого университета можем помочь. Купить диплом в Калуге – [url=http://diplomybox.com/kupit-diplom-kaluga/]diplomybox.com/kupit-diplom-kaluga[/url]
Cazrztq
19 Sep 25 at 3:55 pm
займы россии [url=https://www.zaimy-16.ru]https://www.zaimy-16.ru[/url] .
zaimi_wbMi
19 Sep 25 at 3:55 pm
все займы рф [url=www.zaimy-16.ru/]www.zaimy-16.ru/[/url] .
zaimi_eaMi
19 Sep 25 at 4:02 pm
Thank you for any other informative blog.
The place else may I get that kind of information written in such a
perfect manner? I have a undertaking that I’m simply now running on,
and I have been on the look out for such info.
innotox
19 Sep 25 at 4:04 pm
https://www.rwaq.org/users/collins_lindaf30908-20250910150238
HarryPaync
19 Sep 25 at 4:05 pm
kraken актуальные ссылки kraken onion, kraken onion ссылка, kraken onion зеркала, kraken рабочая ссылка onion, сайт kraken onion, kraken darknet, kraken darknet market, kraken darknet ссылка, сайт kraken darknet, kraken актуальные ссылки, кракен ссылка kraken, kraken официальные ссылки, kraken ссылка тор, kraken ссылка зеркало, kraken ссылка на сайт, kraken онион, kraken онион тор, кракен онион, кракен онион тор, кракен онион зеркало, кракен даркнет маркет, кракен darknet, кракен onion, кракен ссылка onion, кракен onion сайт, kra ссылка, kraken сайт, kraken актуальные ссылки, kraken зеркало, kraken ссылка зеркало, kraken зеркало рабочее, актуальные зеркала kraken, kraken сайт зеркала, kraken маркетплейс зеркало, кракен ссылка, кракен даркнет
RichardPep
19 Sep 25 at 4:05 pm
микрозаймы все [url=https://www.zaimy-16.ru]https://www.zaimy-16.ru[/url] .
zaimi_vzMi
19 Sep 25 at 4:07 pm
https://xn--krken23-bn4c.com
Howardreomo
19 Sep 25 at 4:10 pm
все займы онлайн на карту [url=http://zaimy-16.ru/]все займы онлайн на карту[/url] .
zaimi_byMi
19 Sep 25 at 4:13 pm
Возможность интеграции с популярными платформами для онлайн-продаж упрощает создание интернет-магазинов.
https://prowesserp.com/tsevrimitsi-kumar-platformu-hesap-olusturma-itsin-oedueller-ile-web-kaynagi/
19 Sep 25 at 4:13 pm
Hey there! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some
targeted keywords but I’m not seeing very good gains.
If you know of any please share. Many thanks!
Anonymous
19 Sep 25 at 4:14 pm
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2web at
bs2best.at blacksprut marketplace Official
CharlesNarry
19 Sep 25 at 4:16 pm
https://internet43210.bloguerosa.com/36203043/diagnostico-de-necesidades-de-capacitacion-una-visi%C3%B3n-general
El correcto diagnostico en necesidades de capacitacion es la base para construir programas de formacion que den resultados. En Chile, muchas companias gastan millones en talleres que no sirven porque casi nunca hicieron un levantamiento claro de lo que los trabajadores necesitan.
Razones para hacer un diagnostico en necesidades de capacitacion?
Identifica las faltas concretas de conocimientos.
Reduce gastos innecesarios en programas.
Conecta la capacitacion con la meta organizacional.
Aumenta la retencion de los colaboradores.
Metodos para aplicar un diagnostico en necesidades de capacitacion
Cuestionarios internos: rapidos de aplicar, ideales para detectar la voz de los trabajadores.
Reuniones con lideres: permiten detectar necesidades de cada unidad.
Monitoreo: ver el flujo real para notar oportunidades invisibles en papel.
Mediciones de desempeno: conectan KPIs con las habilidades que se deben reforzar.
Beneficios de un diagnostico de necesidades de capacitacion bien hecho
Cursos que se ajustan con las faltas reales.
Eficiencia de dinero.
Crecimiento profesional alineado con la estrategia de la empresa.
Resultados visibles en productividad.
Errores comunes al hacer un diagnostico en necesidades de capacitacion
Imitar modelos de otras empresas sin personalizar.
Reducir deseos de gerentes con necesidades reales.
Pasar por alto la opinion de los trabajadores.
Analizar solo una vez y no actualizar.
Un diagnostico en necesidades de capacitacion es la llave para lograr una estrategia de desarrollo real.
JuniorShido
19 Sep 25 at 4:18 pm
все займы онлайн [url=zaimy-16.ru]все займы онлайн[/url] .
zaimi_lcMi
19 Sep 25 at 4:19 pm
Simply want to say your article is as amazing. The clarity in your post is just great and
i can assume you’re an expert on this subject.
Well with your permission let me to grab your RSS feed to keep
updated with forthcoming post. Thanks a million and please continue the rewarding work.
Bedrock Restoration water damage repair
19 Sep 25 at 4:20 pm