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=www.frei-diplom1.ru/]где купить диплом с занесением реестр[/url] .
Diplomi_fsOi
21 Oct 25 at 1:09 am
пин ап играть в демо авиатор [url=pinup5007.ru]pinup5007.ru[/url]
pin_up_uz_lasr
21 Oct 25 at 1:10 am
диплом купить с занесением в реестр рязань [url=http://frei-diplom5.ru/]http://frei-diplom5.ru/[/url] .
Diplomi_woPa
21 Oct 25 at 1:10 am
I have been browsing online more than 4 hours today, yet I never found any interesting article like yours.
It is pretty worth enough for me. In my view, if all website owners and bloggers made good content as you
did, the net will be a lot more useful than ever before.
adult shop deals
21 Oct 25 at 1:11 am
купить диплом в хабаровске [url=http://rudik-diplom4.ru]http://rudik-diplom4.ru[/url] .
Diplomi_hsOr
21 Oct 25 at 1:13 am
cd clock [url=alarm-radio-clocks.com]alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_dnOa
21 Oct 25 at 1:13 am
seo оптимизация москва [url=https://reiting-seo-agentstv-moskvy.ru]https://reiting-seo-agentstv-moskvy.ru[/url] .
reiting seo agentstv moskvi_rrMl
21 Oct 25 at 1:13 am
купить диплом в севастополе [url=http://www.rudik-diplom1.ru]купить диплом в севастополе[/url] .
Diplomi_tper
21 Oct 25 at 1:13 am
купить свидетельство о рождении [url=www.rudik-diplom11.ru/]купить свидетельство о рождении[/url] .
Diplomi_ukMi
21 Oct 25 at 1:15 am
pin up tikish qanday qilinadi [url=https://pinup5008.ru]pin up tikish qanday qilinadi[/url]
pin_up_uz_spSt
21 Oct 25 at 1:15 am
купить диплом пту с занесением в реестр [url=http://www.frei-diplom3.ru]купить диплом пту с занесением в реестр[/url] .
Diplomi_bgKt
21 Oct 25 at 1:16 am
We are a bunch of volunteers and starting a brand new scheme in our community.
Your website provided us with helpful info to work on. You’ve performed an impressive process
and our entire group might be thankful to you.
webpage
21 Oct 25 at 1:16 am
купить диплом инженера механика [url=https://www.rudik-diplom8.ru]купить диплом инженера механика[/url] .
Diplomi_blMt
21 Oct 25 at 1:17 am
компании сео [url=http://www.seo-prodvizhenie-reiting.ru]http://www.seo-prodvizhenie-reiting.ru[/url] .
seo prodvijenie reiting_awEa
21 Oct 25 at 1:17 am
можно купить диплом техникума [url=frei-diplom10.ru]можно купить диплом техникума[/url] .
Diplomi_ixEa
21 Oct 25 at 1:18 am
купить диплом в омске [url=http://www.rudik-diplom10.ru]купить диплом в омске[/url] .
Diplomi_axSa
21 Oct 25 at 1:18 am
купить диплом института с реестром [url=www.frei-diplom5.ru]купить диплом института с реестром[/url] .
Diplomi_onPa
21 Oct 25 at 1:18 am
Приятная обстановка, чистота и уют чувствуется во всём. Девушка была внимательной, движения лёгкие и уверенные. Вышел с широкой улыбкой. Крайне рекомендую, шлюха заказать Новосибирск – https://sibirka.com/. Остался очень доволен, сервис и массаж на высоте.
Bobbyham
21 Oct 25 at 1:18 am
Listn uⲣ, Singapore folks, math гemains рrobably the extremely essential primary topic, encouraging
creativity tһrough challenge-tackling tօ innovative jobs.
Anglo-Chinese School (Independent) Junior College
оffers a faith-inspired education tһat harmonizes intellectual pursuits ѡith ethical values,
empowering trainees tⲟ beсome compassionate international people.
Ιts International Baccalaureate program encourages critical
thinking ɑnd inquiry, supported by world-class resources and devoted educators.Trainees excel іn a
broad selection of co-curricular activities, fгom robotics to
music, developing flexibility ɑnd imagination. The school’s emphasis οn service learning instills а sense of obligation and neighborhood engagement fгom
аn earlү stage. Graduates аre ѡell-prepared fߋr distinguished universities, continuing а legacy of excellence ɑnd stability.
Millennia Institute sticks ߋut with іts distinctive
three-year pre-university pathway leading t᧐ the GCE A-Level assessments, supplying
flexible ɑnd extensive resеarch study alternatives in commerce, arts, аnd sciences customized t᧐ accommodate a diverse variety оf students and tһeir distinct goals.
Ꭺs a centralized institute, іt provides personalized guidance аnd support ɡroup, including dedicated
academic consultants ɑnd therapy services, tο
guarantee evеry student’s holistic advancement ɑnd academic success іn a encouraging environment.
Тһe institute’ѕ cutting edge centers,ѕuch as digital learning hubs,
multimedia resource centers, аnd collaborative ѡork areas, ⅽreate an appealing platform fοr
innovative mentor tchniques аnd hands-on tasks that bridge theory ԝith practical application. Τhrough strong
market collaborations, trainees gain access tߋ real-world
experiences ⅼike internships, workshops witһ professionals, and scholarship chances tһat enhance
their employability аnd career preparedness. Alumni fгom Millennia Institute consistently attain success іn greateг education аnd professional arenas,
ѕhowing tһe institution’s unwavering commitment tо
promoting long-lasting learning, flexibility, аnd individual empowerment.
Listen ᥙp, steady pom ρi ρi, mathematics іs among ߋf
the һighest topics duгing Junior College, laying groundwork іn A-Level advanced math.
Besіdes from establishment amenities, concentrate
on mathematics to stop common mistakes including careless errors ɑt assessments.
Ⅾⲟn’t play play lah, combine ɑ reputable Junior College
ρlus maths proficiency for guarantee high A Levels marks аnd seamless changes.
Aiyo, minus strong maths in Junior College, even leading institution youngsters mɑy falter witһ
secondary equations, thսѕ cultivate tһat now leh.
A-level success correlates ԝith һigher starting salaries.
Parents, fearful ⲟf losing mode engaged lah, solid primary math results to superior scientific comprehension аnd construction aspirations.
Оh, mathematics serves aѕ thе foundation block օf primary
education, aiding children ԝith dimensional reasoning tߋ
architecture paths.
Feel free tօ visit my website: Catholic Junior College, Quincy,
Quincy
21 Oct 25 at 1:18 am
https://pilloleverdi.shop/# dove comprare Cialis in Italia
LarryArrix
21 Oct 25 at 1:18 am
купить диплом о среднем профессиональном образовании [url=www.educ-ua7.ru]www.educ-ua7.ru[/url] .
Diplomi_lbea
21 Oct 25 at 1:18 am
сколько стоит купить диплом в колледже [url=http://frei-diplom8.ru]http://frei-diplom8.ru[/url] .
Diplomi_ezsr
21 Oct 25 at 1:19 am
заказать проект перепланировки квартиры в москве цена [url=www.proekt-pereplanirovki-kvartiry11.ru]www.proekt-pereplanirovki-kvartiry11.ru[/url] .
proekt pereplanirovki kvartiri_hjot
21 Oct 25 at 1:20 am
купить диплом в березниках [url=rudik-diplom4.ru]купить диплом в березниках[/url] .
Diplomi_qyOr
21 Oct 25 at 1:20 am
В салоне царит спокойствие и гармония. Девушка была улыбчивая, внимательная и очень красивая. Массаж — просто восторг. Крайне советую, проститутки недорого нск, https://sibirka.com/. Был в восторге, сервис просто супер.
Bobbyham
21 Oct 25 at 1:22 am
купить диплом занесением в реестр [url=https://www.frei-diplom1.ru]купить диплом занесением в реестр[/url] .
Diplomi_ylOi
21 Oct 25 at 1:22 am
купить диплом по реестру [url=www.frei-diplom4.ru/]купить диплом по реестру[/url] .
Diplomi_olOl
21 Oct 25 at 1:23 am
best radio alarm clock [url=http://alarm-radio-clocks.com]http://alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_aaOa
21 Oct 25 at 1:23 am
Выезд нарколога на дом в Нижнем Новгороде — круглосуточная помощь при запое. Мы гарантируем анонимность и конфиденциальность на всех этапах лечения.
Подробнее тут – [url=https://vyvod-iz-zapoya-nizhnij-novgorod11.ru/]вывод из запоя на дому цена[/url]
TerrellOwelf
21 Oct 25 at 1:23 am
After I originally left a comment I apⲣear to һave clicked
tһе -Notify me whеn neԝ comments ɑrе added- checkbox and now eaсh tіme a commеnt is added I receive fouг emails
wіth the exact same comment. Theгe haas to be a means yoս are ablе to reove mе
from tһat service?Cheers!
my homeρage discreet shipping for pangolin scales online
discreet shipping for pangolin scales online
21 Oct 25 at 1:23 am
It’s a pity you don’t have a donate button! I’d certainly
donate to this excellent blog! I guess for now i’ll settle for book-marking and adding your RSS
feed to my Google account. I look forward to fresh updates
and will talk about this site with my Facebook group.
Talk soon!
CalvenRidge Trust
21 Oct 25 at 1:24 am
пин ап как вывести деньги [url=https://pinup5007.ru]https://pinup5007.ru[/url]
pin_up_uz_aisr
21 Oct 25 at 1:24 am
Мы не «усиливаем всё сразу». Лечение собирается из модулей: гидратация, адресная коррекция электролитов, гастропротекция, антиоксидантная поддержка, при необходимости — мягкие анксиолитики по строгим показаниям. Порядок и насыщенность зависят от ведущей жалобы и времени суток. Ниже — ориентир для пациента: почему назначение выглядит именно так и в какой горизонт мы ожидаем эффект.
Узнать больше – [url=https://narkologicheskaya-klinika-murmansk15.ru/]наркологические клиники алкоголизм в мурманске[/url]
PatrickNip
21 Oct 25 at 1:26 am
купить диплом в сыктывкаре [url=https://rudik-diplom15.ru/]купить диплом в сыктывкаре[/url] .
Diplomi_ioPi
21 Oct 25 at 1:26 am
купить диплом с занесением в реестр цена [url=https://frei-diplom3.ru]купить диплом с занесением в реестр цена[/url] .
Diplomi_zrKt
21 Oct 25 at 1:27 am
pin up xavfsizmi [url=www.pinup5007.ru]pin up xavfsizmi[/url]
pin_up_uz_ulsr
21 Oct 25 at 1:28 am
купить диплом с регистрацией [url=http://frei-diplom1.ru]купить диплом с регистрацией[/url] .
Diplomi_gyOi
21 Oct 25 at 1:29 am
пин ап бонус [url=https://www.pinup5007.ru]https://www.pinup5007.ru[/url]
pin_up_uz_gesr
21 Oct 25 at 1:29 am
шкалька
Jamesstalm
21 Oct 25 at 1:30 am
купить диплом в чапаевске [url=https://rudik-diplom1.ru/]купить диплом в чапаевске[/url] .
Diplomi_eker
21 Oct 25 at 1:32 am
где купить диплом нефтяного техникума [url=www.frei-diplom8.ru]где купить диплом нефтяного техникума[/url] .
Diplomi_vnsr
21 Oct 25 at 1:33 am
купить диплом в первоуральске [url=rudik-diplom10.ru]rudik-diplom10.ru[/url] .
Diplomi_euSa
21 Oct 25 at 1:33 am
диплом реестр купить [url=https://frei-diplom6.ru]диплом реестр купить[/url] .
Diplomi_naOl
21 Oct 25 at 1:33 am
купить диплом в нижним тагиле [url=www.rudik-diplom8.ru]купить диплом в нижним тагиле[/url] .
Diplomi_kbMt
21 Oct 25 at 1:34 am
диплом внесенный в реестр купить [url=http://frei-diplom3.ru]диплом внесенный в реестр купить[/url] .
Diplomi_xoKt
21 Oct 25 at 1:35 am
levaquin for ear infection dose
how long to take levaquin
21 Oct 25 at 1:37 am
купить диплом о высшем образовании с занесением в реестр [url=http://frei-diplom4.ru]купить диплом о высшем образовании с занесением в реестр[/url] .
Diplomi_pvOl
21 Oct 25 at 1:37 am
best cd clock radio [url=https://www.alarm-radio-clocks.com]https://www.alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_kwOa
21 Oct 25 at 1:38 am
купить диплом москва с занесением в реестр [url=www.frei-diplom1.ru/]купить диплом москва с занесением в реестр[/url] .
Diplomi_ycOi
21 Oct 25 at 1:38 am
These are really fantastic ideas in concerning blogging.
You have touched some good points here. Any way keep up
wrinting.
tips
21 Oct 25 at 1:39 am