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!
Howdy! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading your
articles. Can you recommend any other blogs/websites/forums that go over the same subjects?
Many thanks!
download free internet
19 Aug 25 at 5:26 pm
What we’re covering
• Zelensky in Washington: European leaders will join Ukrainian President Volodymyr Zelensky at the White House, as he meets with US President Donald Trump this afternoon. Trump said Zelensky must agree to some of Russia’s conditions — including that Ukraine cede Crimea and agree never to join NATO — for the war to end.
[url=https://kra-35cc.com]kra33 cc[/url]
• Potential security guarantees: At last week’s summit with Trump, Russian President Vladimir Putin agreed to allow security guarantees for Ukraine and made concessions on “land swaps” as part of a potential peace deal, US envoy Steve Witkoff told CNN. Zelensky suggested that such guarantees would need to be stronger than those that “didn’t work” in the past. Russia has yet to mention such agreements.
[url=https://kraken33-at.com]kra33[/url]
• On the ground: Zelensky condemned Russia’s latest strikes across Ukraine, which killed at least 10 people, saying the Kremlin intends to “humiliate diplomatic efforts” and underscores “why reliable security guarantees are required.”
kra35 СЃСЃ
https://http-kra31.cc
Stephengoots
19 Aug 25 at 5:31 pm
https://hoo.be/efofiyoa
Lloydrar
19 Aug 25 at 5:32 pm
Вы можете создать учетную запись через браузер на своем компьютере или мобильную версию казино 1xslots mobile в
телефоне.
1xslots casino
19 Aug 25 at 5:34 pm
Thanks , I have just been looking for info about this topic for
a while and yours is the greatest I have found out so far.
However, what in regards to the conclusion? Are you sure concerning
the source?
Greyford Corebit
19 Aug 25 at 5:35 pm
Phần mềm này không hề tích hợp bất kì quảng cáo nào khi sử dụng.
https://www.ssyoutube.com/vi/youtube-to-mp4/
19 Aug 25 at 5:39 pm
I’m extremely inspired with your writing abilities as smartly as with the format to your blog.
Is that this a paid theme or did you customize it yourself?
Anyway keep up the nice high quality writing, it is rare to
peer a nice blog like this one today..
اگر فرهنگیان قبول شویم و نرویم
19 Aug 25 at 5:44 pm
888starz
JamalWanda
19 Aug 25 at 5:45 pm
http://kamameds.com/# Non-prescription ED tablets discreetly shipped
Danielchumn
19 Aug 25 at 5:49 pm
https://allmynursejobs.com/author/dianadianahappyholtcreative27/
Lloydrar
19 Aug 25 at 5:53 pm
Prostavive is a natural supplement formulated to support prostate health and
ease common concerns like frequent urination and weak flow.
Its blend of plant-based ingredients helps reduce inflammation and
promote better urinary comfort, especially as men age.
Many users appreciate it as a safe, effective, and convenient way to take care of long-term prostate
wellness.
Prostavive
19 Aug 25 at 5:56 pm
https://mez.ink/nin3kurookinewwyv
DavidWah
19 Aug 25 at 5:57 pm
gessi официальный сайт [url=https://gessi-santehnika-5.ru]gessi официальный сайт[/url] .
gessi santehnika_opoa
19 Aug 25 at 5:59 pm
Далее начинается детоксикация и купирование симптомов. Инфузионная терапия позволяет восстановить водно-электролитный баланс, снизить нагрузку на печень и продукты распада этанола. По показаниям применяются противорвотные, противотревожные и седативные средства, витамины группы B, магний, гепатопротекторы, антиоксиданты. Цель — убрать тремор, головную боль, тошноту, нормализовать сон и снизить уровень тревоги.
Исследовать вопрос подробнее – http://narkolog-na-dom-krasnogorsk6.ru/vyzov-narkologa-na-dom-v-krasnogorske/https://narkolog-na-dom-krasnogorsk6.ru
Cyrusemopy
19 Aug 25 at 6:01 pm
Safe access to generic ED medication: ED treatment without doctor visits – Non-prescription ED tablets discreetly shipped
PeterTEEFS
19 Aug 25 at 6:03 pm
It’s fantastic that you are getting thoughts from this article as well as from our
discussion made here.
Sandford Bitline
19 Aug 25 at 6:06 pm
https://www.themeqx.com/forums/users/vycudoca/
Lloydrar
19 Aug 25 at 6:13 pm
Boostaro is gaining attention as a natural supplement that helps
men improve energy, stamina, and overall vitality.
Its plant-based formula is designed to support healthy blood flow and performance, making it a popular choice for those wanting a safe and effective boost.
Many users appreciate that it focuses on long-term wellness rather than just quick
fixes.
Boostaro
19 Aug 25 at 6:14 pm
This article gives clear idea for the new viewers of blogging,
that in fact how to do blogging and site-building.
tractor hydraulic cylinder
19 Aug 25 at 6:22 pm
Завершает визит блок рекомендаций и краткий маршрут на 2–4 недели: питьевой режим и питание, режим сна, щадящая физическая активность, поддерживающая фармакотерапия. По желанию обсуждаются варианты кодирования и подключение к реабилитационной программе — только по допуску врача и при отсутствии противопоказаний. В первые дни возможны контрольные звонки для уточнения самочувствия и корректировки схемы.
Подробнее – [url=https://narkolog-na-dom-serpuhov6.ru/]вызов нарколога на дом[/url]
HowardDiz
19 Aug 25 at 6:22 pm
This paragraph is in fact a good one it assists
new net visitors, who are wishing in favor of blogging.
Voltryzapex
19 Aug 25 at 6:28 pm
Hello there I am so thrilled I found your blog page, I really found you by error, while I
was researching on Bing for something else, Regardless I
am here now and would just like to say thank you for a remarkable post and a all round thrilling blog (I also love the
theme/design), I don’t have time to read through it all at the minute but I have book-marked
it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up
the excellent jo.
فرق فرهنگیان و تربیت معلم
19 Aug 25 at 6:29 pm
https://bio.site/ubadobyga
Lloydrar
19 Aug 25 at 6:33 pm
итальянская сантехника [url=www.gessi-santehnika-5.ru]итальянская сантехника[/url] .
gessi santehnika_aqoa
19 Aug 25 at 6:34 pm
Pretty section of content. I just stumbled upon your site and in accession capital to assert
that I acquire actually enjoyed account your blog posts.
Anyway I will be subscribing to your augment and even I achievement you access consistently quickly.
Lighting Contractor near me
19 Aug 25 at 6:37 pm
Самостоятельно выйти из запоя — почти невозможно. В Санкт-Петербурге врачи клиники проводят медикаментозный вывод из запоя с круглосуточным выездом. Доверяйте профессионалам.
Подробнее – [url=https://vyvod-iz-zapoya-v-sankt-peterburge16.ru/]наркология вывод из запоя санкт-петербург[/url]
Dennisamito
19 Aug 25 at 6:37 pm
Самостоятельно выйти из запоя — почти невозможно. В Санкт-Петербурге врачи клиники проводят медикаментозный вывод из запоя с круглосуточным выездом. Доверяйте профессионалам.
Получить дополнительную информацию – [url=https://vyvod-iz-zapoya-v-sankt-peterburge.ru/]вывод из запоя дешево в санкт-петербурге[/url]
Craigjen
19 Aug 25 at 6:40 pm
https://say.la/read-blog/126273
DavidWah
19 Aug 25 at 6:41 pm
Затяжной запой опасен для жизни. Врачи наркологической клиники в Челябинске проводят срочный вывод из запоя — на дому или в стационаре. Анонимно, безопасно, круглосуточно.
Получить дополнительную информацию – [url=https://vyvod-iz-zapoya-chelyabinsk12.ru/]вывод из запоя капельница[/url]
Anthonyzip
19 Aug 25 at 6:42 pm
Yes! Finally someone writes about macauslot88.
macauslot88
19 Aug 25 at 6:57 pm
https://accupril24store.shop
https://accupril24store.shop
19 Aug 25 at 6:59 pm
plinko [url=https://www.plinko3002.ru]https://www.plinko3002.ru[/url]
plinko_kz_tjPa
19 Aug 25 at 7:01 pm
What we’re covering
• Zelensky in Washington: European leaders will join Ukrainian President Volodymyr Zelensky at the White House, as he meets with US President Donald Trump this afternoon. Trump said Zelensky must agree to some of Russia’s conditions — including that Ukraine cede Crimea and agree never to join NATO — for the war to end.
[url=https://at-kra34.cc]kra40[/url]
• Potential security guarantees: At last week’s summit with Trump, Russian President Vladimir Putin agreed to allow security guarantees for Ukraine and made concessions on “land swaps” as part of a potential peace deal, US envoy Steve Witkoff told CNN. Zelensky suggested that such guarantees would need to be stronger than those that “didn’t work” in the past. Russia has yet to mention such agreements.
[url=https://kra37-at.cc]kra39[/url]
• On the ground: Zelensky condemned Russia’s latest strikes across Ukraine, which killed at least 10 people, saying the Kremlin intends to “humiliate diplomatic efforts” and underscores “why reliable security guarantees are required.”
kra37 at
https://kra-30.cc
Francisrob
19 Aug 25 at 7:03 pm
What we’re covering
• Zelensky in Washington: European leaders will join Ukrainian President Volodymyr Zelensky at the White House, as he meets with US President Donald Trump this afternoon. Trump said Zelensky must agree to some of Russia’s conditions — including that Ukraine cede Crimea and agree never to join NATO — for the war to end.
[url=https://kra37.org]kra32 СЃСЃ[/url]
• Potential security guarantees: At last week’s summit with Trump, Russian President Vladimir Putin agreed to allow security guarantees for Ukraine and made concessions on “land swaps” as part of a potential peace deal, US envoy Steve Witkoff told CNN. Zelensky suggested that such guarantees would need to be stronger than those that “didn’t work” in the past. Russia has yet to mention such agreements.
[url=https://kra35-at.cc]kra32 cc[/url]
• On the ground: Zelensky condemned Russia’s latest strikes across Ukraine, which killed at least 10 people, saying the Kremlin intends to “humiliate diplomatic efforts” and underscores “why reliable security guarantees are required.”
kra33 СЃСЃ
https://kra-35at.com
JorgeKesia
19 Aug 25 at 7:04 pm
This is really interesting, You’re a very skilled blogger.
I’ve joined your feed and look forward to seeking more of your
fantastic post. Also, I’ve shared your website in my social networks!
BryxlordAi
19 Aug 25 at 7:06 pm
What’s up everybody, here every one is sharing these kinds of
know-how, thus it’s good to read this weblog, and I
used to pay a visit this webpage daily.
Maria Ozawa
19 Aug 25 at 7:07 pm
Фактически сайт был забанен, но лидеры виртуальных слотов все же
умудряются оставить полезное сообщение для своих пользователей.
гама казино официальный сайт
19 Aug 25 at 7:16 pm
plinko slot [url=http://plinko-kz2.ru/]http://plinko-kz2.ru/[/url]
plinko_kz_jker
19 Aug 25 at 7:16 pm
Когда организм на пределе, важна срочная помощь в Санкт-Петербурге — это команда опытных наркологов, которые помогут быстро и мягко выйти из запоя без вреда для здоровья.
Подробнее можно узнать тут – [url=https://azithromycinum.ru/]вывод из запоя клиника санкт-петербург[/url]
Terryunock
19 Aug 25 at 7:17 pm
cialis generic timeline [url=https://tadalify.shop/#]cialis generics[/url] tadalafil tablets side effects
RobertCat
19 Aug 25 at 7:18 pm
купить сантехнику италия [url=https://gessi-santehnika-5.ru]https://gessi-santehnika-5.ru[/url] .
gessi santehnika_xeoa
19 Aug 25 at 7:20 pm
Parents, don’t ignore leh, leading primary cultivates communication proficiencies,
vital fоr worldwide industry roles.
Listen ᥙρ, wah, excellent primary focuses ⲟn STEM prematurely, preparing your
child fⲟr IT expansion and popular jobs іn 2025.
Oh no, primary math teaches real-ᴡorld applications ѕuch as financial planning, tһus ensure your child grasps
іt properly starting young age.
Oi oi, Singapore folks, arithmetic іs likeⅼy the extremely crucial primary topic, encouraging imagination tһrough issue-resolving for innovative jobs.
Ιn аddition to establishment amenities, focus uρon math for prevent
typical mistakes including careless mistakes ԁuring
assessments.
Wow, math acts ⅼike tһe groundwork stone іn primary schooling, aiding
youngsters wіth geometric analysis for architecture paths.
Hey hey, Singapore parents, mathematics гemains
ⲣrobably the highly imⲣortant primary discipline, fostering innovation fоr
issue-resolving іn innovative careers.
Tanjong Katong Primary School promotes ɑ dynamic community promoting holistic development.
Dedicated staff build strong, capable үoung minds.
Punggol Ⅴiew Primary School useѕ beautiful knowing ѡith
contemporary facilities.
Thе school influences achievement tһrough quality mentor.
Moms ɑnd dads vaⅼue itѕ modern method.
Ꮋere is my blog post math tuition singapore (Hung)
Hung
19 Aug 25 at 7:21 pm
I think the admin of this website is genuinely working hard in favor of
his website, since here every material is quality based
material.
plantdisplays.uk.com Mua ma túy đá
19 Aug 25 at 7:21 pm
1хставка официальный сайт [url=https://1win22097.ru]https://1win22097.ru[/url]
1win_tdpr
19 Aug 25 at 7:22 pm
https://wanderlog.com/view/thjcmbypto/купить-марихуану-гашиш-канабис-мальдивы/shared
DavidWah
19 Aug 25 at 7:25 pm
Портал о ремонте https://dki.org.ua и строительстве: от отделки квартиры до возведения загородного дома. Подробные статьи, рекомендации экспертов и идеи для обустройства жилья.
MiltonGyday
19 Aug 25 at 7:26 pm
сантехника gessi [url=www.gessi-santehnika-5.ru]сантехника gessi[/url] .
gessi santehnika_pboa
19 Aug 25 at 7:26 pm
Портал о стройке https://sushico.com.ua и ремонте. Новости рынка, современные технологии, подборка идей для интерьера и экстерьера. Всё, что нужно для дома и дачи.
Jamesitesy
19 Aug 25 at 7:27 pm
1вин промокоды [url=http://1win22097.ru/]1вин промокоды[/url]
1win_fkpr
19 Aug 25 at 7:32 pm
При начислении кэшбэка учитываются
только средства с реального баланса.
вулкан в казахстане
19 Aug 25 at 7:32 pm