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!
Heya this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have
to manually code with HTML. I’m starting a blog soon but have no coding know-how
so I wanted to get guidance from someone with experience.
Any help would be enormously appreciated!
Pink salt trick recipe
21 Aug 25 at 3:42 am
Hello, I do think your site may be having browser compatibility issues.
Whenever I look at your website in Safari, it looks fine
however, if opening in Internet Explorer, it has some overlapping issues.
I merely wanted to give you a quick heads up! Other than that, great website!
Powell's Plumbing
21 Aug 25 at 3:44 am
Психолог
qpfeof3162
21 Aug 25 at 3:50 am
СтавкиПрогнозы [url=stavki-prognozy-one.ru]stavki-prognozy-one.ru[/url] .
stavki prognozi_adsr
21 Aug 25 at 3:54 am
Magnificent goods from you, man. I have understand your stuff
previous to and you’re just too wonderful. I actually like what
you have acquired here, certainly like what you are saying and the way in which you say it.
You make it enjoyable and you still care for to keep it wise.
I cant wait to read much more from you. This is actually a terrific site.
Niezwykłe swetry dla kobiet
21 Aug 25 at 3:55 am
Когда организм на пределе, важна срочная помощь в Челябинске — это команда опытных наркологов, которые помогут быстро и мягко выйти из запоя без вреда для здоровья.
Узнать больше – [url=https://vyvod-iz-zapoya-chelyabinsk11.ru/]вывод из запоя капельница в челябинске[/url]
TimothyKam
21 Aug 25 at 3:56 am
My relatives every time say that I am wasting my time here at net, except I know I am getting experience every day by reading
such nice content.
macauslot88
21 Aug 25 at 3:57 am
These are really impressive ideas in concerning blogging. You have touched some nice factors here.
Any way keep up wrinting.
Source
21 Aug 25 at 3:57 am
you are in point of fact a good webmaster. The website loading pace is
incredible. It sort of feels that you are doing any
unique trick. In addition, The contents are masterwork.
you have done a fantastic task on this matter!
soi keo nha cai hom nay
21 Aug 25 at 3:58 am
ставки прогнозы [url=http://www.stavki-prognozy-one.ru]http://www.stavki-prognozy-one.ru[/url] .
stavki prognozi_acsr
21 Aug 25 at 3:59 am
https://say.la/read-blog/126387
Charleswar
21 Aug 25 at 3:59 am
http://webanketa.com/forms/6mrk8dhm6wqk8sk6cgrk6e1h/
ArturoVoift
21 Aug 25 at 4:03 am
свидетельство о разводе купить недорого [url=educ-ua4.ru]educ-ua4.ru[/url] .
Diplomi_nuPl
21 Aug 25 at 4:05 am
20
Получить больше информации – [url=https://nazalnyj.ru/]нарколог вывод из запоя в москве[/url]
DonaldFUP
21 Aug 25 at 4:11 am
Затяжной запой опасен для жизни. Врачи наркологической клиники в Санкт-Петербурге проводят срочный вывод из запоя — на дому или в стационаре. Анонимно, безопасно, круглосуточно.
Разобраться лучше – [url=https://vyvod-iz-zapoya-v-sankt-peterburge16.ru/]скорая вывод из запоя санкт-петербург[/url]
StevenGuirl
21 Aug 25 at 4:12 am
I think this is one of the most significant information for me.
And i’m glad reading your article. But want to remark on few general things, The web site style is wonderful, the
articles is really excellent : D. Good job, cheers
Voltryzapex
21 Aug 25 at 4:12 am
Neat blog! Is your theme custom made or did you download
it from somewhere? A design like yours with a few simple
adjustements would really make my blog shine. Please let me know where you got your theme.
Thank you
macauslot88
21 Aug 25 at 4:15 am
Everything is very open with a really clear clarification of the challenges.
It was truly informative. Your site is very helpful. Many
thanks for sharing!
site
21 Aug 25 at 4:21 am
https://say.la/read-blog/125873
ArturoVoift
21 Aug 25 at 4:24 am
Hi there it’s me, I am also visiting this website daily, this site is
truly nice and the visitors are really sharing good thoughts.
Staniki dla pań plus size - szerokie ramiączka i wygodne miseczki to klucz do sukcesu!
21 Aug 25 at 4:26 am
купить диплом в одессе [url=http://educ-ua4.ru/]купить диплом в одессе[/url] .
Diplomi_ojPl
21 Aug 25 at 4:29 am
Do you mind if I quote a couple of your articles as long as I
provide credit and sources back to your weblog?
My website is in the very same area of interest as
yours and my users would genuinely benefit from some of the information you
present here. Please let me know if this ok with you.
Thanks a lot!
dewascatter link alternatif
21 Aug 25 at 4:34 am
СтавкиПрогнозы [url=https://stavki-prognozy-one.ru/]stavki-prognozy-one.ru[/url] .
stavki prognozi_cksr
21 Aug 25 at 4:36 am
It’s difficult to find knowledgeable people for this subject, but
you sound like you know what you’re talking about! Thanks
best solar wall lights
21 Aug 25 at 4:36 am
Сильно ценный программа для пользователей, кто часто передвигается населенными пунктами и городами.
Планировщик маршрутов
способен сберечь время и установить эффективный путь.
Очень нравится функция
принимать во внимание несколько
видов транспорта. Советую каждому, кто обращает
внимание на комфорт и эффективность во время поездок.
www.kjcampus.co.kr
21 Aug 25 at 4:37 am
https://pxlmo.com/latricejanuary82
Charleswar
21 Aug 25 at 4:38 am
оценка недвижимости Москва оценочная компания
ocenochnaya-kompaniya-943
21 Aug 25 at 4:38 am
https://kamameds.com/# Kamagra oral jelly USA availability
Danielchumn
21 Aug 25 at 4:38 am
купить диплом о средне специальном образовании реестр [url=http://arus-diplom31.ru]купить диплом о средне специальном образовании реестр[/url] .
Priobresti diplom VYZa!_rnOl
21 Aug 25 at 4:39 am
Hey I know this is off topic but I was wondering
if you knew of any widgets I could add to my blog that automatically tweet my
newest twitter updates. I’ve been looking for a
plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading
your blog and I look forward to your new updates.
adubo
21 Aug 25 at 4:39 am
можно купить аттестат об окончании 11 классов [url=http://arus-diplom23.ru]http://arus-diplom23.ru[/url] .
Diplomi_ltSr
21 Aug 25 at 4:42 am
I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire
someone to do it for you? Plz reply as I’m looking to design my own blog
and would like to know where u got this from.
thank you
Stahlwandpools
21 Aug 25 at 4:42 am
Heya! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due
to no data backup. Do you have any solutions to stop
hackers?
xing
21 Aug 25 at 4:44 am
I constantly spent my half an hour to read this blog’s articles or reviews
everyday along with a cup of coffee.
Track Dexair Sys
21 Aug 25 at 4:44 am
Hi there, everything is going nicely here and ofcourse every one is sharing facts, that’s genuinely
excellent, keep up writing.
personal injury attorneys in Port Jefferson
21 Aug 25 at 4:45 am
https://www.montessorijobsuk.co.uk/author/iacwyuci/
ArturoVoift
21 Aug 25 at 4:46 am
где купить аттестат за 11 класс [url=www.arus-diplom22.ru]где купить аттестат за 11 класс[/url] .
Diplomi_pesl
21 Aug 25 at 4:48 am
блэкспрут вход
RichardPep
21 Aug 25 at 4:48 am
Tremendous things here. I’m very happy to look your
article. Thanks a lot and I am having a look ahead to contact you.
Will you kindly drop me a e-mail?
трипскан вход
21 Aug 25 at 4:48 am
Stavki Prognozy [url=www.stavki-prognozy-one.ru]www.stavki-prognozy-one.ru[/url] .
stavki prognozi_ogsr
21 Aug 25 at 4:52 am
WOW just what I was searching for. Came
here by searching for มวยเว็บตรง
มวยออนไลน์
21 Aug 25 at 4:54 am
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.
Davidcob
21 Aug 25 at 5:00 am
It’s really a nice and useful piece of info. I am glad that you just shared this useful information with us.
Please keep us informed like this. Thanks for sharing.
toto togel online
21 Aug 25 at 5:00 am
Московская Академия Медицинского Образования – https://mosamo.ru/ это возможность пройти переподготовку и повышение квалификации по медицине. Мы проводим дистанционное обучение врачей и медицинских работников по 260 направлениям и выдаем документы установленного образца, сертификат дополнительного образования. Узнайте подробнее на сайте.
newaxhychari
21 Aug 25 at 5:02 am
Do you mind if I quote a couple of your articles as long
as I provide credit and sources back to your blog? My blog site is
in the exact same niche as yours and my visitors would definitely benefit from
a lot of the information you present here. Please let me know
if this ok with you. Appreciate it!
Pink salt trick
21 Aug 25 at 5:05 am
plinko game online [url=https://www.plinko3001.ru]https://www.plinko3001.ru[/url]
plinko_kz_qcEr
21 Aug 25 at 5:07 am
https://wanderlog.com/view/zwqdmwbwmr/купить-марихуану-гашиш-канабис-ереван/shared
ArturoVoift
21 Aug 25 at 5:07 am
Процедура всегда начинается с личной консультации. Врач-нарколог выясняет анамнез, оценивает физическое и психическое состояние, рассказывает о возможных методиках и противопоказаниях. После выбора оптимального метода пациенту объясняются все нюансы, отвечают на вопросы, выдают письменные рекомендации по поведению в период действия кодировки.
Подробнее – [url=https://kodirovanie-ot-alkogolizma-kolomna6.ru/]metody-kodirovaniya-ot-alkogolizma[/url]
JamesFrupe
21 Aug 25 at 5:09 am
кашпо для цветов с автополивом [url=http://www.kashpo-s-avtopolivom-spb.ru]кашпо для цветов с автополивом[/url] .
gorshok s avtopolivom_zysr
21 Aug 25 at 5:13 am
купить диплом внесенный в реестр [url=https://www.arus-diplom31.ru]купить диплом внесенный в реестр[/url] .
Priobresti diplom VYZa!_ooOl
21 Aug 25 at 5:17 am