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://wanderlog.com/view/rcvssvmrxp/купить-кокаин-марихуану-мефедрон-катовице/shared
Charleswar
20 Aug 25 at 11:28 pm
SildenaPeak [url=http://sildenapeak.com/#]SildenaPeak[/url] SildenaPeak
RobertCat
20 Aug 25 at 11:29 pm
оценка для бизнеса заказать оценку
ocenochnaya-kompaniya-736
20 Aug 25 at 11:30 pm
бесплатные прогнозы на спорт на сегодня [url=http://www.prognozy-na-sport-7.ru]http://www.prognozy-na-sport-7.ru[/url] .
prognozi na sport_yvMi
20 Aug 25 at 11:31 pm
blacksprut com зеркало
RichardPep
20 Aug 25 at 11:32 pm
Great delivery. Solid arguments. Keep up the good work.
manhwaland bokep
20 Aug 25 at 11:33 pm
Extreme heat is a killer. A recent heat wave shows how much more deadly it’s becoming
[url=https://tripscan.xyz]трипскан сайт[/url]
Extreme heat is a killer and its impact is becoming far, far deadlier as the human-caused climate crisis supercharges temperatures, according to a new study, which estimates global warming tripled the number of deaths in the recent European heat wave.
For more than a week, temperatures in many parts of Europe spiked above 100 degrees Fahrenheit. Tourist attractions closed, wildfires ripped through several countries, and people struggled to cope on a continent where air conditioning is rare.
https://tripscan.xyz
трипскан вход
The outcome was deadly. Thousands of people are estimated to have lost their lives, according to a first-of-its-kind rapid analysis study published Wednesday.
A team of researchers, led by Imperial College London and the London School of Hygiene and Tropical Medicine, looked at 10 days of extreme heat between June 23 and July 2 across 12 European cities, including London, Paris, Athens, Madrid and Rome.
They used historical weather data to calculate how intense the heat would have been if humans had not burned fossil fuels and warmed the world by 1.3 degrees Celsius. They found climate change made Europe’s heat wave 1 to 4 degrees Celsius (1.8 to 7.2 Fahrenheit) hotter.
The scientists then used research on the relationship between heat and daily deaths to estimate how many people lost their lives.
They found approximately 2,300 people died during ten days of heat across the 12 cities, around 1,500 more than would have died in a world without climate change. In other words, global heating was responsible for 65% of the total death toll.
“The results show how relatively small increases in the hottest temperatures can trigger huge surges in death,” the study authors wrote.
Heat has a particularly pernicious impact on people with underlying health conditions, such as heart disease, diabetes and respiratory problems.
People over 65 years old were most affected, accounting for 88% of the excess deaths, according to the analysis. But heat can be deadly for anyone. Nearly 200 of the estimated deaths across the 12 cities were among those aged 20 to 65.
Climate change was responsible for the vast majority of heat deaths in some cities. In Madrid, it accounted for about 90% of estimated heat wave deaths, the analysis found.
Austinnep
20 Aug 25 at 11:34 pm
https://potofu.me/u1uizb2o
ArturoVoift
20 Aug 25 at 11:36 pm
Перед выбором метода врач проводит диагностику, оценивает физическое и психоэмоциональное состояние пациента, рассказывает о возможных рисках и особенностях процедуры. Только после согласования всех деталей назначается дата и форма кодирования.
Подробнее можно узнать тут – [url=https://kodirovanie-ot-alkogolizma-ehlektrostal6.ru/]кодирование от алкоголизма уколом[/url]
Rodneyselry
20 Aug 25 at 11:36 pm
melbet официальный сайт скачать бесплатно [url=https://www.melbet3006.com]melbet официальный сайт скачать бесплатно[/url]
melbet_kwpa
20 Aug 25 at 11:36 pm
купить диплом о среднем образовании с занесением в реестр [url=www.arus-diplom34.ru]купить диплом о среднем образовании с занесением в реестр[/url] .
Kypit diplom o visshem obrazovanii!_hfkn
20 Aug 25 at 11:38 pm
Hey, I think your website might be having browser compatibility issues.
When I look at your website in Opera, it looks fine but when opening in Internet
Explorer, it has some overlapping. I just wanted to give you a
quick heads up! Other then that, superb blog!
will solar lights charge in the shade
20 Aug 25 at 11:42 pm
купить диплом с занесением в реестр новокузнецке [url=https://arus-diplom34.ru/]купить диплом с занесением в реестр новокузнецке[/url] .
Kypit diplom lubogo VYZa!_otkn
20 Aug 25 at 11:45 pm
Definitely believe that which you said. Your favorite justification appeared to
be on the web the simplest thing to be aware of. I say to you, I certainly get irked while people consider worries that
they just do not know about. You managed to hit the nail upon the
top as well as defined out the whole thing without having side effect , people can take a signal.
Will probably be back to get more. Thanks
bejeweled 3
20 Aug 25 at 11:47 pm
Really no matter if someone doesn’t know after that its
up to other users that they will help, so here it occurs.
رتبه قبولی پزشکی ۱۴۰۴
20 Aug 25 at 11:54 pm
https://www.themeqx.com/forums/users/abvauceheocu/
ArturoVoift
20 Aug 25 at 11:56 pm
SildenaPeak: SildenaPeak – SildenaPeak
PeterTEEFS
20 Aug 25 at 11:56 pm
plinko kz [url=http://plinko-kz2.ru/]http://plinko-kz2.ru/[/url]
plinko_kz_oser
20 Aug 25 at 11:57 pm
В Санкт-Петербурге решение есть — наркологическая клиника. Здесь помогают людям выйти из запоя без страха и осуждения. Всё анонимно, грамотно и с заботой о каждом пациенте.
Узнать больше – [url=https://vyvod-iz-zapoya-v-sankt-peterburge15.ru/]вывод из запоя клиника в санкт-петербурге[/url]
Jessekew
20 Aug 25 at 11:57 pm
плинко кз [url=https://plinko3002.ru/]плинко кз[/url]
plinko_kz_qiPa
20 Aug 25 at 11:58 pm
Visit the website https://aviamastersgame.online/ and you will find complete information about Avia Master. You will learn how to register, how to play, how to download the mobile application, what game strategies to choose for yourself, as well as what bonuses exist for registration and replenishment of the balance. Detailed information is presented in a simple form so that you can enjoy the game.
BeriyNow
21 Aug 25 at 12:01 am
В Челябинске решение есть — наркологическая клиника. Здесь помогают людям выйти из запоя без страха и осуждения. Всё анонимно, грамотно и с заботой о каждом пациенте.
Детальнее – [url=https://vyvod-iz-zapoya-chelyabinsk11.ru/]вывод из запоя недорого[/url]
TimothyKam
21 Aug 25 at 12:04 am
В Санкт-Петербурге решение есть — наркологическая клиника. Здесь помогают людям выйти из запоя без страха и осуждения. Всё анонимно, грамотно и с заботой о каждом пациенте.
Подробнее – [url=https://vyvod-iz-zapoya-v-sankt-peterburge18.ru/]sankt-peterburg[/url]
JoshuaBen
21 Aug 25 at 12:06 am
https://bio.site/qibidehmyyc
Charleswar
21 Aug 25 at 12:06 am
Awesome post.
Here is my homepage Ficken
Ficken
21 Aug 25 at 12:08 am
hi!,I like your writing so so much! percentage we
communicate extra about your article on AOL? I require a
specialist in this area to unravel my problem.
May be that is you! Taking a look ahead to see you.
https://rkkwin.com/
21 Aug 25 at 12:09 am
лучшие прогнозы на спорт [url=https://prognozy-na-sport-7.ru/]лучшие прогнозы на спорт[/url] .
prognozi na sport_aoMi
21 Aug 25 at 12:12 am
우선, 유튜브에서 시청하고 계신 영상을 다운받거나 소장할 영상을 검색해서 들어가 주세요.
http://www.ko.savefrom.net/
21 Aug 25 at 12:13 am
купить диплом с занесением в реестр вуза [url=http://arus-diplom31.ru/]купить диплом с занесением в реестр вуза[/url] .
Diplomi_bepl
21 Aug 25 at 12:16 am
https://bio.site/eubiugidv
ArturoVoift
21 Aug 25 at 12:17 am
Этап процедуры
Разобраться лучше – [url=https://vyvod-iz-zapoya-odincovo6.ru/]вывод из запоя клиника[/url]
MichaelAtoda
21 Aug 25 at 12:22 am
Далее проводится сама процедура: при медикаментозном варианте препарат может вводиться внутривенно, внутримышечно или имплантироваться под кожу; при психотерапевтическом — работа проходит в специально оборудованном кабинете, в спокойной атмосфере. После кодирования пациент находится под наблюдением, чтобы исключить осложнения и закрепить эффект. Важно помнить, что соблюдение рекомендаций и поддержка семьи играют решающую роль в сохранении трезвости.
Получить дополнительные сведения – [url=https://kodirovanie-ot-alkogolizma-kolomna6.ru/]metody-kodirovaniya-ot-alkogolizma-cena[/url]
JamesFrupe
21 Aug 25 at 12:23 am
It’s impressive that you are getting ideas from this post
as well as from our discussion made here.
casino online stranieri
21 Aug 25 at 12:24 am
As the admin of this website is working, no hesitation very
quickly it will be famous, due to its quality contents.
nnnkx2.tv
21 Aug 25 at 12:24 am
Online sources for Kamagra in the United States: Sildenafil oral jelly fast absorption effect – Kamagra oral jelly USA availability
ElijahKic
21 Aug 25 at 12:25 am
Tadalify [url=https://tadalify.com/#]cialis soft[/url] Tadalify
RobertCat
21 Aug 25 at 12:26 am
прогнозы на спорт на сегодня от профессионалов [url=https://prognozy-na-sport-7.ru]https://prognozy-na-sport-7.ru[/url] .
prognozi na sport_sdMi
21 Aug 25 at 12:26 am
Зависимость разрушает семью? Не медлите — обратитесь в московский центр «Alco.Rehab» за квалифицированной помощью.
Разобраться лучше – [url=https://nazalnyj.ru/]вывод из запоя анонимно город москва[/url]
DonaldFUP
21 Aug 25 at 12:27 am
ставки прогнозы [url=https://stavki-prognozy-1.ru/]https://stavki-prognozy-1.ru/[/url] .
stavki prognozy_dxMa
21 Aug 25 at 12:30 am
Hi every one, here every one is sharing these experience, thus it’s nice to read this Ai remake x AI beats x AI instrumental x AI remix x music producer publishing royalty split x AI stem splitter x jakkoutthebxx x type beat x the Artsulli blog, and I used to pay
a visit this webpage everyday.
Ai remake x AI beats x AI instrumental x AI remix x music producer publishing royalty split x AI stem splitter x jakkoutthebxx x type beat x the Artsulli blog
21 Aug 25 at 12:30 am
купить диплом с занесением в реестр самара [url=arus-diplom34.ru]arus-diplom34.ru[/url] .
Priobresti diplom o visshem obrazovanii!_rakn
21 Aug 25 at 12:34 am
Tadalify: canadian cialis no prescription – Tadalify
PeterTEEFS
21 Aug 25 at 12:36 am
Really no matter if someone doesn’t know then its up
to other users that they will assist, so here it takes place.
ssstik
21 Aug 25 at 12:37 am
https://say.la/read-blog/125843
ArturoVoift
21 Aug 25 at 12:37 am
оценка полезных моделей оценка стоимости заказать
ocenochnaya-kompaniya-654
21 Aug 25 at 12:38 am
tadalafil without a doctor prescription: when will generic cialis be available in the us – cialis black review
RichardTit
21 Aug 25 at 12:40 am
вызвать психиатра на дом в москве
psychiatr-moskva001.ru
консультация психиатра платно
psihiatrmskNeT
21 Aug 25 at 12:41 am
plinko [url=https://www.plinko3002.ru]plinko[/url]
plinko_kz_yiPa
21 Aug 25 at 12:44 am
детский психиатр на дом
psychiatr-moskva001.ru
психиатр на дом
psihiatrmskNeT
21 Aug 25 at 12:44 am
https://wanderlog.com/view/vebaflmgxv/купить-кокаин-марихуану-мефедрон-херцег-нови/shared
Charleswar
21 Aug 25 at 12:44 am