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=http://frei-diplom2.ru/]как купить диплом проведенный[/url] .
Diplomi_zvEa
20 Oct 25 at 6:06 am
I’ve learn some good stuff here. Definitely price bookmarking for revisiting.
I wonder how much attempt you place to create this kind of great informative site.
Clair Invion
20 Oct 25 at 6:06 am
купить диплом техникума с занесением в реестр [url=http://frei-diplom8.ru/]купить диплом техникума с занесением в реестр[/url] .
Diplomi_mtsr
20 Oct 25 at 6:06 am
Искусственные венки — практичное решение: выглядят реалистично, не вянут, выдерживают активный день и переиспользование. В «Флорион» — десятки моделей от воздушных пастельных до ярких тематических, с легкой посадкой и надежной фиксацией. Это экономично и удобно для карнавалов, свадеб и съемок. Подберите образ на https://www.florion.ru/catalog/venok-iz-iskusstvennyh-cvetov — весь каталог с фото и ценами, доставка по Москве. Долговечная красота, которая всегда под рукой.
qohavtax
20 Oct 25 at 6:10 am
Этот информационный материал привлекает внимание множеством интересных деталей и необычных ракурсов. Мы предлагаем уникальные взгляды на привычные вещи и рассматриваем вопросы, которые волнуют общество. Будьте в курсе актуальных тем и расширяйте свои знания!
Что ещё нужно знать? – https://uapj.fr/alerte-information-conseil-municipal-du-30-septembre-2024
Francisfip
20 Oct 25 at 6:10 am
купить диплом в гуково [url=https://rudik-diplom7.ru]купить диплом в гуково[/url] .
Diplomi_nuPl
20 Oct 25 at 6:11 am
Публикация предлагает читателю не просто информацию, а инструменты для анализа и саморазвития. Мы стимулируем критическое мышление, предлагая различные точки зрения и призывая к самостоятельному поиску решений.
https://cgmbc.co.uk/projects/hr-smith-gazebo-relocation
ScottAnymn
20 Oct 25 at 6:11 am
новости тенниса [url=https://novosti-sporta-7.ru/]новости тенниса[/url] .
novosti sporta_hhOt
20 Oct 25 at 6:12 am
диплом купить с занесением в реестр рязань [url=https://frei-diplom2.ru/]https://frei-diplom2.ru/[/url] .
Diplomi_ygEa
20 Oct 25 at 6:12 am
1win mobil ilova uz [url=https://1win5509.ru]https://1win5509.ru[/url]
1win_uz_ocKt
20 Oct 25 at 6:12 am
купить диплом в нижнекамске [url=http://rudik-diplom8.ru]http://rudik-diplom8.ru[/url] .
Diplomi_ouMt
20 Oct 25 at 6:14 am
заказать проект перепланировки квартиры в москве [url=http://proekt-pereplanirovki-kvartiry11.ru]заказать проект перепланировки квартиры в москве[/url] .
proekt pereplanirovki kvartiri_qgot
20 Oct 25 at 6:14 am
диплом автодорожного техникума купить [url=https://educ-ua7.ru/]https://educ-ua7.ru/[/url] .
Diplomi_dnea
20 Oct 25 at 6:15 am
прогнозы на хоккей с высокой проходимостью [url=https://luchshie-prognozy-na-khokkej8.ru/]luchshie-prognozy-na-khokkej8.ru[/url] .
lychshie prognozi na hokkei_znEi
20 Oct 25 at 6:15 am
купить диплом в улан-удэ [url=www.rudik-diplom14.ru/]купить диплом в улан-удэ[/url] .
Diplomi_ioea
20 Oct 25 at 6:15 am
1win bog‘lanish uz [url=www.1win5510.ru]www.1win5510.ru[/url]
1win_uz_rlsi
20 Oct 25 at 6:20 am
https://reality38261.blogzet.com/detox-examen-de-orina-cosas-que-debe-saber-antes-de-comprar-52593947
Detox para examen de orina se ha convertido en una solucion cada vez mas conocida entre personas que requieren eliminar toxinas del organismo y superar pruebas de test de drogas. Estos productos estan disenados para ayudar a los consumidores a limpiar su cuerpo de sustancias no deseadas, especialmente esas relacionadas con el consumo de cannabis u otras drogas.
Un buen detox para examen de orina debe brindar resultados rapidos y efectivos, en especial cuando el tiempo para prepararse es limitado. En el mercado actual, hay muchas variedades, pero no todas prometen un proceso seguro o fiable.
De que funciona un producto detox? En terminos basicos, estos suplementos funcionan acelerando la depuracion de metabolitos y residuos a traves de la orina, reduciendo su nivel hasta quedar por debajo del umbral de deteccion de los tests. Algunos actuan en cuestion de horas y su accion puede durar entre 4 a 6 horas.
Resulta fundamental combinar estos productos con adecuada hidratacion. Beber al menos 2 litros de agua por jornada antes y despues del consumo del detox puede mejorar los beneficios. Ademas, se recomienda evitar alimentos grasos y bebidas azucaradas durante el proceso de preparacion.
Los mejores productos de limpieza para orina incluyen ingredientes como extractos de hierbas, vitaminas del tipo B y minerales que respaldan el funcionamiento de los organos y la funcion hepatica. Entre las marcas mas destacadas, se encuentran aquellas que ofrecen certificaciones sanitarias y estudios de prueba.
Para usuarios frecuentes de cannabis, se recomienda usar detoxes con ventanas de accion largas o iniciar una preparacion previa. Mientras mas prolongada sea la abstinencia, mayor sera la eficacia del producto. Por eso, combinar la planificacion con el uso correcto del suplemento es clave.
Un error comun es pensar que todos los detox actuan identico. Existen diferencias en formulacion, sabor, metodo de uso y duracion del efecto. Algunos vienen en presentacion 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 sugerir abstinencia, buena alimentacion y descanso recomendado.
Por ultimo, es importante recalcar que ninguno detox garantiza 100% de exito. Siempre hay variables biologicas como metabolismo, nivel de consumo, y tipo de examen. Por ello, es vital seguir las instrucciones del fabricante y no confiarse.
JuniorShido
20 Oct 25 at 6:20 am
доставка алкоголя круглосуточно москва [url=http://www.alcoygoloc.ru]http://www.alcoygoloc.ru[/url] .
dostavka alkogolya_kfki
20 Oct 25 at 6:20 am
где найти промокод для 1xBet узнайте надёжные источники получения действующих кодов и предложений
Aaronawads
20 Oct 25 at 6:21 am
купить диплом с проводкой моего [url=www.frei-diplom4.ru/]купить диплом с проводкой моего[/url] .
Diplomi_qyOl
20 Oct 25 at 6:21 am
купить диплом занесенный реестр [url=https://www.frei-diplom3.ru]купить диплом занесенный реестр[/url] .
Diplomi_flKt
20 Oct 25 at 6:22 am
купить диплом в железногорске [url=http://rudik-diplom7.ru/]http://rudik-diplom7.ru/[/url] .
Diplomi_vlPl
20 Oct 25 at 6:23 am
1win uz [url=www.1win5510.ru]www.1win5510.ru[/url]
1win_uz_oysi
20 Oct 25 at 6:23 am
прогнозы на баскетбол сегодня бесплатно [url=https://www.prognozy-ot-professionalov4.ru]https://www.prognozy-ot-professionalov4.ru[/url] .
prognozi ot professionalov_sjOr
20 Oct 25 at 6:23 am
Sou viciado em PlayPIX Casino, sinto um ritmo alucinante. O catalogo e rico e diversificado, suportando jogos compativeis com criptomoedas. Eleva a diversao do jogo. O acompanhamento e impecavel, sempre pronto para ajudar. Os ganhos chegam sem demora, contudo algumas rodadas gratis extras seriam incriveis. Em resumo, PlayPIX Casino vale uma visita epica para quem aposta com cripto ! Tambem o site e rapido e atraente, adiciona um toque de conforto. Igualmente impressionante os pagamentos seguros em cripto, assegura transacoes confiaveis.
Ver agora|
SambaRiserX9zef
20 Oct 25 at 6:23 am
купить диплом в магадане [url=https://www.rudik-diplom14.ru]купить диплом в магадане[/url] .
Diplomi_veea
20 Oct 25 at 6:23 am
диплом техникума 2002 года купить в [url=https://frei-diplom9.ru]диплом техникума 2002 года купить в[/url] .
Diplomi_ydea
20 Oct 25 at 6:23 am
1win uz [url=www.1win5510.ru]www.1win5510.ru[/url]
1win_uz_tysi
20 Oct 25 at 6:23 am
Статья содержит практические рекомендации и полезные советы, которые можно легко применить в повседневной жизни. Мы делаем акцент на реальных примерах и проверенных методиках, которые способствуют личностному развитию и улучшению качества жизни.
Слушай внимательно — тут важно – https://okusurijoho.com/2013/12/30/just-a-cool-blog-post-with-images
Geraldequax
20 Oct 25 at 6:23 am
stereo radio alarm clock [url=http://www.alarm-radio-clocks.com]http://www.alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_qoOa
20 Oct 25 at 6:24 am
Unlike conventional supply exchanges, the forex market operates 24
hours a day, supplying continuous possibilities for trading different money sets.
Tools like the forex heatmap can confirm indispensable by aesthetically representing currency efficiency throughout
the market spectrum. By evaluating this data, investors can establish reliable methods that line up
with market problems and take advantage of on short-term
changes.
market trading
20 Oct 25 at 6:25 am
где купить диплом техникума в москве [url=http://www.frei-diplom8.ru]где купить диплом техникума в москве[/url] .
Diplomi_hssr
20 Oct 25 at 6:26 am
новости футбола [url=www.novosti-sporta-7.ru/]новости футбола[/url] .
novosti sporta_ynOt
20 Oct 25 at 6:26 am
Статья знакомит с важнейшими моментами, которые сформировали наше общество. От великих изобретений до культурных переворотов — вы узнаете, как прошлое влияет на наше мышление, технологии и образ жизни.
Где почитать поподробнее? – https://dominicluke.com/hello-world
Williamdok
20 Oct 25 at 6:26 am
https://hoo.be/fucifeho
Anthonycam
20 Oct 25 at 6:27 am
The rise of social trading systems has actually changed how investors involve with one
an additional. By permitting individuals to share
their strategies openly and adhere to effective traders, these platforms promote
a collaborative environment and urge knowledge-sharing.
Amateur investors can pick up from the successes and blunders of more knowledgeable peers, progressively building
their own trading approaches. The psychological part in trading is frequently overlooked; nonetheless, social liability can positively influence an investor’s efficiency by giving motivation and support in navigating the rollercoaster of economic markets.
market trading
20 Oct 25 at 6:27 am
купить диплом в феодосии [url=https://www.rudik-diplom11.ru]https://www.rudik-diplom11.ru[/url] .
Diplomi_uuMi
20 Oct 25 at 6:27 am
1вин криптовалюта [url=http://1win5509.ru/]http://1win5509.ru/[/url]
1win_uz_sqKt
20 Oct 25 at 6:27 am
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—41-at.ru]kra37 cc[/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.ru]kra42 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.”
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.
kra39 сс
https://kra–42–at.ru
Michaelbaf
20 Oct 25 at 6:29 am
That is a good tip particularly to those new to the blogosphere.
Short but very accurate information… Many thanks for sharing
this one. A must read article!
비아그라 구입
20 Oct 25 at 6:29 am
купить диплом во владивостоке [url=https://rudik-diplom6.ru/]купить диплом во владивостоке[/url] .
Diplomi_fsKr
20 Oct 25 at 6:29 am
Ready to start your journey into anonymous browsing? An antidetect browser is the ideal solution for beginners and small teams to manage multi-accounting safely without getting flagged by anti-fraud systems.
DouglasJasse
20 Oct 25 at 6:30 am
купить диплом техникума дешево [url=https://frei-diplom9.ru/]купить диплом техникума дешево[/url] .
Diplomi_fiea
20 Oct 25 at 6:30 am
The mix of typical market analysis with an expanding scheme of
contemporary technological tools marks a significant growth in the trading
globe. As formulas procedure large quantities of data to determine patterns
and execute orders, they contribute to enhanced market performance but additionally raise
worries regarding the capacity for market control and unexpected fluctuations.
cfd global app
20 Oct 25 at 6:31 am
купить морской диплом [url=http://rudik-diplom7.ru/]купить морской диплом[/url] .
Diplomi_ktPl
20 Oct 25 at 6:31 am
1win savollar va javoblar [url=http://1win5510.ru/]1win savollar va javoblar[/url]
1win_uz_qysi
20 Oct 25 at 6:31 am
$MTAUR coin’s security audits by SolidProof and Coinsult make it trustworthy amid scam fears. Presale raffle for $100K is drawing crowds. Loving the whimsical creature battles in the demo.
minotaurus coin
WilliamPargy
20 Oct 25 at 6:32 am
The combination of conventional market evaluation with an expanding palette of modern-day technological
devices marks a substantial development in the trading world.
Traders currently have the ability to combine chart-based evaluation with
real-time data feeds and algorithmic trading systems, producing an advanced trading
atmosphere. Algorithmic trading, utilizing computer programs to carry
out trades based on predefined techniques, has actually gotten enormous grip, often outshining human investors in rate and accuracy.
As algorithms process huge amounts of data to
determine patterns and implement orders, they contribute to enhanced market effectiveness but additionally increase
problems concerning the possibility for market adjustment and unexpected changes.
cfd global app
20 Oct 25 at 6:36 am
Hello! I could have sworn I’ve been to this website before but
after browsing through some of the post I realized it’s new to me.
Nonetheless, I’m definitely happy I found it and I’ll be book-marking and checking back
often!
bokep remaja indonesia
20 Oct 25 at 6:36 am
ночная доставка алкоголя [url=www.alcoygoloc.ru]www.alcoygoloc.ru[/url] .
dostavka alkogolya_gcki
20 Oct 25 at 6:36 am