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://rudik-diplom15.ru/]купить диплом в минусинске[/url] .
Diplomi_qwPi
20 Oct 25 at 8:05 am
online sportwetten app verluste zurückholen österreich
online sportwetten app
20 Oct 25 at 8:05 am
1win uz [url=https://www.1win5509.ru]https://www.1win5509.ru[/url]
1win_uz_ixKt
20 Oct 25 at 8:06 am
ចូលរួមលេងកម្សាន្តនៅ Joy Palace Cambodia!
យើងខ្ញុំមានផ្តល់ជូនហ្គេមកាស៊ីណូអនឡាញ,
ហ្គេមស្លុត និងការភ្នាល់កីឡាដ៏សម្បូរបែប។ ចុះឈ្មោះឥឡូវនេះដើម្បីទទួលបានប្រាក់រង្វាន់ស្វាគមន៍!
Joy Palace Cambodia - កាស៊ីណូអនឡាញ និងហ្គេមស្លុតឈានមុខគេ
20 Oct 25 at 8:07 am
кракен vk4
kraken 2025
JamesDaync
20 Oct 25 at 8:07 am
купить диплом автомеханика [url=www.rudik-diplom5.ru]купить диплом автомеханика[/url] .
Diplomi_htma
20 Oct 25 at 8:07 am
https://blogs.uoregon.edu/mmj3/2015/02/12/best-bmw-ad-ever/?unapproved=193731&moderation-hash=3d52fb10201c14708765d7120adee2ed#comment-193731
https://blogs.uoregon.edu/mmj3/2015/02/12/best-bmw-ad-ever/?unapproved=193731&moderation-hash=3d52fb10201c14708765d7120adee2ed#comment-193731
20 Oct 25 at 8:08 am
https://imageevent.com/jonesi388gir/szusj
Anthonycam
20 Oct 25 at 8:08 am
прогнозы на матчи [url=prognozy-ot-professionalov4.ru]prognozy-ot-professionalov4.ru[/url] .
prognozi ot professionalov_ocOr
20 Oct 25 at 8:09 am
диплом техникума купить [url=https://www.frei-diplom9.ru]диплом техникума купить[/url] .
Diplomi_gfea
20 Oct 25 at 8:11 am
купить диплом в астрахани [url=http://rudik-diplom8.ru]купить диплом в астрахани[/url] .
Diplomi_seMt
20 Oct 25 at 8:12 am
The hype around $MTAUR presale is justified—over 1M USDT in days. Unlocking boosts and outfits with tokens adds depth to gameplay. This is crypto meeting casual gaming perfectly.
minotaurus token
WilliamPargy
20 Oct 25 at 8:12 am
Very good info. Lucky me I discovered your website
by accident (stumbleupon). I have bookmarked it for later!
wireless adult toy
20 Oct 25 at 8:12 am
купить официальный диплом с занесением в реестр [url=https://frei-diplom1.ru/]https://frei-diplom1.ru/[/url] .
Diplomi_lrOi
20 Oct 25 at 8:13 am
Купить диплом техникума в Донецк [url=https://www.educ-ua7.ru]https://www.educ-ua7.ru[/url] .
Diplomi_jxea
20 Oct 25 at 8:13 am
купить диплом колледжа с занесением в реестр [url=www.frei-diplom3.ru/]купить диплом колледжа с занесением в реестр[/url] .
Diplomi_kxKt
20 Oct 25 at 8:14 am
Minotaurus ICO is targeting $6.4M, and with current traction, it’ll smash it. $MTAUR’s DeFi access empowers users without complexity. Referral rewards have me spreading the word.
minotaurus token
WilliamPargy
20 Oct 25 at 8:15 am
купить диплом в прокопьевске [url=http://rudik-diplom4.ru]купить диплом в прокопьевске[/url] .
Diplomi_hkOr
20 Oct 25 at 8:16 am
диплом медицинского колледжа купить [url=http://frei-diplom9.ru/]http://frei-diplom9.ru/[/url] .
Diplomi_srea
20 Oct 25 at 8:17 am
купить диплом в иркутске [url=http://rudik-diplom6.ru/]купить диплом в иркутске[/url] .
Diplomi_wfKr
20 Oct 25 at 8:17 am
купить диплом в самаре [url=http://rudik-diplom5.ru]купить диплом в самаре[/url] .
Diplomi_tpma
20 Oct 25 at 8:18 am
купить диплом о среднем профессиональном образовании с занесением в реестр [url=https://frei-diplom2.ru/]купить диплом о среднем профессиональном образовании с занесением в реестр[/url] .
Diplomi_zvEa
20 Oct 25 at 8:19 am
Good day! I could have sworn I’ve visited this website
before but after looking at many of the posts I realized it’s
new to me. Anyways, I’m certainly delighted I came across it and I’ll be bookmarking it and checking back frequently!
레비트라 구매
20 Oct 25 at 8:21 am
новости киберспорта [url=https://sportivnye-novosti-2.ru/]новости киберспорта[/url] .
sportivnie novosti_sdma
20 Oct 25 at 8:22 am
купить диплом о среднем профессиональном образовании с занесением в реестр [url=http://frei-diplom3.ru]купить диплом о среднем профессиональном образовании с занесением в реестр[/url] .
Diplomi_vhKt
20 Oct 25 at 8:22 am
Awesome blog! Do you have any tips for aspiring writers?
I’m planning to start my own blog soon but I’m a little lost on everything.
Would you propose starting with a free platform like WordPress or
go for a paid option? There are so many choices out there that I’m completely confused ..
Any suggestions? Many thanks!
UU88
20 Oct 25 at 8:22 am
1win mobil yuklash ios [url=http://1win5510.ru]http://1win5510.ru[/url]
1win_uz_cxsi
20 Oct 25 at 8:23 am
прогнозы на спорт с описанием [url=https://prognozy-ot-professionalov4.ru/]prognozy-ot-professionalov4.ru[/url] .
prognozi ot professionalov_atOr
20 Oct 25 at 8:23 am
I visit every day some blogs and blogs to read articles
or reviews, except this weblog presents quality based content.
토닥이
20 Oct 25 at 8:23 am
Having read this I believed it was very informative.
I appreciate you spending some time and energy to put this informative article
together. I once again find myself personally spending a significant amount of time both reading
and commenting. But so what, it was still worthwhile!
link hm88
20 Oct 25 at 8:24 am
кракен
кракен vk5
JamesDaync
20 Oct 25 at 8:24 am
Публикация предлагает уникальную подборку информации, которая будет интересна как специалистам, так и широкому кругу читателей. Здесь вы найдете ответы на часто задаваемые вопросы и полезные инсайты для дальнейшего применения.
Ознакомиться с деталями – https://ceveh.com.br/2024/05/secretaria-de-educacao-acompanha-selecao-de-alunos-para-escola-do-teatro-bolshoi-no-brasil
EduardoGeari
20 Oct 25 at 8:24 am
купить диплом в северске [url=https://rudik-diplom8.ru/]https://rudik-diplom8.ru/[/url] .
Diplomi_adMt
20 Oct 25 at 8:25 am
1win ishonchli saytmi [url=www.1win5510.ru]www.1win5510.ru[/url]
1win_uz_tksi
20 Oct 25 at 8:25 am
купить диплом в тюмени [url=http://rudik-diplom11.ru]купить диплом в тюмени[/url] .
Diplomi_ggMi
20 Oct 25 at 8:26 am
спортивные события [url=http://www.sportivnye-novosti-2.ru]спортивные события[/url] .
sportivnie novosti_iqma
20 Oct 25 at 8:26 am
диплом купить с занесением в реестр челябинск [url=https://frei-diplom3.ru]https://frei-diplom3.ru[/url] .
Diplomi_toKt
20 Oct 25 at 8:27 am
купить диплом в бийске [url=https://rudik-diplom14.ru/]купить диплом в бийске[/url] .
Diplomi_hsea
20 Oct 25 at 8:27 am
купить диплом в кызыле [url=https://www.rudik-diplom7.ru]купить диплом в кызыле[/url] .
Diplomi_uuPl
20 Oct 25 at 8:29 am
Эта публикация дает возможность задействовать различные источники информации и представить их в удобной форме. Читатели смогут быстро найти нужные данные и получить ответы на интересующие их вопросы. Мы стремимся к четкости и доступности материала для всех!
http://kakubunki.co.jp/2019/12/02/%E4%BB%A4%E5%92%8C%E5%85%83%E5%B9%B4%EF%BC%91%EF%BC%92%E6%9C%88%E3%81%AE%E3%81%94%E6%8C%A8%E6%8B%B6
ScottAnymn
20 Oct 25 at 8:30 am
диплом медсестры с занесением в реестр купить [url=http://www.frei-diplom2.ru]диплом медсестры с занесением в реестр купить[/url] .
Diplomi_rdEa
20 Oct 25 at 8:31 am
top clock radio [url=alarm-radio-clocks.com]alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_vwOa
20 Oct 25 at 8:31 am
https://tech37270.ampedpages.com/una-revisiГіn-de-detox-examen-de-orina-64717793
Limpieza para examen de muestra se ha vuelto en una solucion cada vez mas popular entre personas que necesitan eliminar toxinas del cuerpo y superar pruebas de analisis de drogas. Estos formulas estan disenados para facilitar a los consumidores a depurar su cuerpo de componentes no deseadas, especialmente las relacionadas con el ingesta de cannabis u otras sustancias ilicitas.
Un buen detox para examen de fluido debe proporcionar resultados rapidos y visibles, en gran cuando el tiempo para desintoxicarse es limitado. En el mercado actual, hay muchas alternativas, pero no todas prometen un proceso seguro o rapido.
Que funciona un producto detox? En terminos claros, estos suplementos actuan acelerando la expulsion de metabolitos y residuos a traves de la orina, reduciendo su presencia hasta quedar por debajo del nivel de deteccion de los tests. Algunos funcionan en cuestion de horas y su impacto puede durar entre 4 a cinco horas.
Es fundamental combinar estos productos con adecuada hidratacion. Beber al menos dos litros de agua diariamente antes y despues del uso del detox puede mejorar los beneficios. Ademas, se recomienda evitar alimentos grasos y bebidas acidas durante el proceso de desintoxicacion.
Los mejores productos de purga para orina incluyen ingredientes como extractos de plantas, vitaminas del complejo B y minerales que respaldan el funcionamiento de los sistemas y la funcion hepatica. Entre las marcas mas destacadas, se encuentran aquellas que tienen certificaciones sanitarias y estudios de eficacia.
Para usuarios frecuentes de marihuana, se recomienda usar detoxes con tiempos de accion largas o iniciar una preparacion temprana. Mientras mas prolongada sea la abstinencia, mayor sera la efectividad del producto. Por eso, combinar la planificacion con el uso correcto del producto es clave.
Un error comun es creer que todos los detox actuan igual. Existen diferencias en contenido, sabor, metodo de uso y duracion del impacto. Algunos vienen en formato liquido, otros en capsulas, y varios combinan ambos.
Ademas, hay productos que incluyen fases de preparacion o purga previa al dia del examen. Estos programas suelen recomendar abstinencia, buena alimentacion y descanso adecuado.
Por ultimo, es importante recalcar que todo detox garantiza 100% de exito. Siempre hay variables individuales como metabolismo, nivel de consumo, y tipo de examen. Por ello, es vital seguir ciertas instrucciones del fabricante y no descuidarse.
JuniorShido
20 Oct 25 at 8:32 am
купить диплом техникума цена [url=http://frei-diplom11.ru]купить диплом техникума цена[/url] .
Diplomi_xmsa
20 Oct 25 at 8:33 am
Эта информационная статья содержит полезные факты, советы и рекомендации, которые помогут вам быть в курсе последних тенденций и изменений в выбранной области. Материал составлен так, чтобы быть полезным и понятным каждому.
Обратиться к источнику – https://www.allaboutliving.nl/blog/coming-soon-all-about-living
RaymondThign
20 Oct 25 at 8:33 am
Hey! Would you mind if I share your blog with my
facebook group? There’s a lot of folks that I think would really
enjoy your content. Please let me know. Thank you
landscaping utah
20 Oct 25 at 8:34 am
Купить диплом колледжа в Винница [url=www.educ-ua7.ru]www.educ-ua7.ru[/url] .
Diplomi_yqea
20 Oct 25 at 8:34 am
https://bs2best.cat
Hermannalia
20 Oct 25 at 8:37 am
купить чистый диплом техникума пять плюс [url=http://frei-diplom8.ru/]купить чистый диплом техникума пять плюс[/url] .
Diplomi_lnsr
20 Oct 25 at 8:38 am
купить диплом с занесением в реестр в калуге [url=https://frei-diplom2.ru]купить диплом с занесением в реестр в калуге[/url] .
Diplomi_jsEa
20 Oct 25 at 8:39 am