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!
Расчёт оклада военнослужащего по должности командира взвода — 16 500 руб. в 2025. новости военная служба
Brentagila
18 Sep 25 at 7:01 am
микрозайм всем [url=www.zaimy-11.ru]www.zaimy-11.ru[/url] .
zaimi_cyPt
18 Sep 25 at 7:03 am
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2best.at blacksprut marketplace Official
CharlesNarry
18 Sep 25 at 7:04 am
электрические карнизы купить [url=https://razdvizhnoj-elektrokarniz.ru/]https://razdvizhnoj-elektrokarniz.ru/[/url] .
razdvijnoi elektrokarniz_xhei
18 Sep 25 at 7:04 am
https://xn--krken21-bn4c.com
Howardreomo
18 Sep 25 at 7:04 am
займы всем [url=www.zaimy-14.ru]www.zaimy-14.ru[/url] .
zaimi_pbSr
18 Sep 25 at 7:05 am
I like the valuable info you supply for your articles.
I’ll bookmark your weblog and take a look at again here frequently.
I am relatively certain I will be told many new stuff right here!
Good luck for the following!
آدرس دانشگاه پیام نور مرکز تهران جنوب
18 Sep 25 at 7:05 am
After I initially left a comment I appear to have clicked the -Notify me
when new comments are added- checkbox and now every time a comment
is added I receive four emails with the exact same comment.
Is there a way you are able to remove me from that service?
Thanks!
Click here
18 Sep 25 at 7:06 am
https://odysee.com/@lyyens62486
Timothyces
18 Sep 25 at 7:07 am
I like what you guys tend to be up too. This sort of clever work and coverage!
Keep up the amazing works guys I’ve you guys to blogroll.
macauslot88
18 Sep 25 at 7:07 am
список займов онлайн на карту [url=http://zaimy-15.ru/]http://zaimy-15.ru/[/url] .
zaimi_sfpn
18 Sep 25 at 7:07 am
раздвижные шторы [url=http://razdvizhnoj-elektrokarniz.ru/]http://razdvizhnoj-elektrokarniz.ru/[/url] .
razdvijnoi elektrokarniz_quei
18 Sep 25 at 7:08 am
займы все онлайн [url=https://zaimy-14.ru/]https://zaimy-14.ru/[/url] .
zaimi_dwSr
18 Sep 25 at 7:09 am
займы россии [url=http://zaimy-11.ru]http://zaimy-11.ru[/url] .
zaimi_fkPt
18 Sep 25 at 7:09 am
online apotheke preisvergleich: sicherheit und wirkung von potenzmitteln – gГјnstigste online apotheke
Israelpaync
18 Sep 25 at 7:10 am
микрозаймы все [url=http://zaimy-14.ru/]http://zaimy-14.ru/[/url] .
zaimi_faSr
18 Sep 25 at 7:11 am
Good day very nice web site!! Guy .. Excellent .. Amazing
.. I’ll bookmark your blog and take the feeds also? I am glad to find a lot
of helpful info right here within the put up, we need work
out extra techniques on this regard, thanks for sharing.
. . . . .
آدرس دانشگاه محقق اردبیلی
18 Sep 25 at 7:13 am
Эта статья предлагает захватывающий и полезный контент, который привлечет внимание широкого круга читателей. Мы постараемся представить тебе идеи, которые вдохновят вас на изменения в жизни и предоставят практические решения для повседневных вопросов. Читайте и вдохновляйтесь!
Все материалы собраны здесь – https://onlitrip.com/convergent-and-divergent-plate-margins
RichardChemo
18 Sep 25 at 7:15 am
Good day! I just would like to give you a huge thumbs
up for your great information you have right here
on this post. I will be coming back to your blog for more soon.
http://akqog.sa.com
18 Sep 25 at 7:15 am
займы все [url=zaimy-15.ru]zaimy-15.ru[/url] .
zaimi_cqpn
18 Sep 25 at 7:15 am
всезаймы [url=http://zaimy-15.ru/]http://zaimy-15.ru/[/url] .
zaimi_hepn
18 Sep 25 at 7:19 am
Your style is really unique compared to other people I have read stuff from.
I appreciate you for posting when you’ve got the opportunity, Guess I will
just book mark this web site.
Кракен даркнет
18 Sep 25 at 7:19 am
Hello, i think that i saw you visited my site thus i came to “return the favor”.I’m attempting to find things to improve
my website!I suppose its ok to use some of your ideas!!
شرایط اقساط سرای ایرانی برای بازنشستگان فرهنگی
18 Sep 25 at 7:19 am
официальные займы онлайн на карту бесплатно [url=https://www.zaimy-11.ru]https://www.zaimy-11.ru[/url] .
zaimi_wcPt
18 Sep 25 at 7:20 am
займы россии [url=www.zaimy-14.ru/]www.zaimy-14.ru/[/url] .
zaimi_zvSr
18 Sep 25 at 7:23 am
займы все онлайн [url=https://zaimy-11.ru/]https://zaimy-11.ru/[/url] .
zaimi_xgPt
18 Sep 25 at 7:24 am
Surf Kaizenaire.com for Singapore’s curated promotions,
mаking it tһe top choicee fоr deals аnd event notifies.
Singaporeans’ deal-chasing prowess іs legendary in Singapore, tһe
utmostt shopping heaven bursting witһ promotions.
Singaporeans appгeciate DIY home design projects fօr individualized
spaces, аnd bear in mind t᧐ remɑіn upgraded оn Singapore’s neѡest promotions and shopping deals.
Negligent Ericka ρrovides edgy, experimental style, cherished Ƅy bold Singaporeans for theіr
bold cuts aand vibrant prints.
TWG Tea supplies gourmet teas ɑnd devices lah, treasured by tea connoisseurs in Singapore f᧐r their beautiful blends
аnd elegant packaging lor.
Τhings ‘ԁ fills burritos аnd kebabs with fresh active
ingredients, loved Ƅy locals fοr tasty, mobile fusion consumes.
Aiyo, fаst leh, visit Kaizenaire.com for price cuts օne.
Feel free to surf t᧐ my рage :: singapore promotions
singapore promotions
18 Sep 25 at 7:25 am
раздвижной электрокарниз [url=http://razdvizhnoj-elektrokarniz.ru/]http://razdvizhnoj-elektrokarniz.ru/[/url] .
razdvijnoi elektrokarniz_pvei
18 Sep 25 at 7:28 am
https://muckrack.com/person-27946774
Timothyces
18 Sep 25 at 7:29 am
займ все [url=https://www.zaimy-14.ru]https://www.zaimy-14.ru[/url] .
zaimi_mlSr
18 Sep 25 at 7:31 am
электрокарнизы для штор купить в москве [url=http://razdvizhnoj-elektrokarniz.ru]http://razdvizhnoj-elektrokarniz.ru[/url] .
razdvijnoi elektrokarniz_hbei
18 Sep 25 at 7:32 am
Эта обзорная заметка содержит ключевые моменты и факты по актуальным вопросам. Она поможет читателям быстро ориентироваться в теме и узнать о самых важных аспектах сегодня. Получите краткий курс по современной информации и оставайтесь в курсе событий!
Погрузиться в научную дискуссию – https://webofthings.org/2009/01/20/we-want-you
Melvinevofs
18 Sep 25 at 7:33 am
For the reason that the admin of this web site is working, no uncertainty very
rapidly it will be well-known, due to its quality contents.
au88
18 Sep 25 at 7:34 am
I constantly spent my half an hour to read this weblog’s content everyday along with a mug of
coffee.
Miroxa Viax
18 Sep 25 at 7:34 am
раздвижные электрокарнизы для штор [url=https://www.razdvizhnoj-elektrokarniz.ru]https://www.razdvizhnoj-elektrokarniz.ru[/url] .
razdvijnoi elektrokarniz_gwei
18 Sep 25 at 7:35 am
микро займы онлайн [url=https://zaimy-14.ru/]микро займы онлайн[/url] .
zaimi_axSr
18 Sep 25 at 7:36 am
Great web site. Lots of useful info here. I am sending it to a few pals ans additionally sharing in delicious.
And certainly, thanks to your sweat!
Puro Bitline
18 Sep 25 at 7:36 am
Hurrah, that’s what I was exploring for, what a material! present here at
this blog, thanks admin of this site.
آدرس دانشگاه علوم پزشکی شهید صدوقی یزد
18 Sep 25 at 7:37 am
список займов онлайн [url=zaimy-15.ru]список займов онлайн[/url] .
zaimi_ndpn
18 Sep 25 at 7:37 am
все микрозаймы [url=http://zaimy-14.ru/]все микрозаймы[/url] .
zaimi_iwSr
18 Sep 25 at 7:38 am
мфо займ онлайн [url=http://zaimy-15.ru]мфо займ онлайн[/url] .
zaimi_zdpn
18 Sep 25 at 7:41 am
кракен onion kraken onion, kraken onion ссылка, kraken onion зеркала, kraken рабочая ссылка onion, сайт kraken onion, kraken darknet, kraken darknet market, kraken darknet ссылка, сайт kraken darknet, kraken актуальные ссылки, кракен ссылка kraken, kraken официальные ссылки, kraken ссылка тор, kraken ссылка зеркало, kraken ссылка на сайт, kraken онион, kraken онион тор, кракен онион, кракен онион тор, кракен онион зеркало, кракен даркнет маркет, кракен darknet, кракен onion, кракен ссылка onion, кракен onion сайт, kra ссылка, kraken сайт, kraken актуальные ссылки, kraken зеркало, kraken ссылка зеркало, kraken зеркало рабочее, актуальные зеркала kraken, kraken сайт зеркала, kraken маркетплейс зеркало, кракен ссылка, кракен даркнет
RichardPep
18 Sep 25 at 7:42 am
взо [url=www.zaimy-15.ru/]www.zaimy-15.ru/[/url] .
zaimi_hqpn
18 Sep 25 at 7:44 am
займы россии [url=http://zaimy-11.ru]http://zaimy-11.ru[/url] .
zaimi_ugPt
18 Sep 25 at 7:45 am
раздвижной электрокарниз купить [url=razdvizhnoj-elektrokarniz.ru]razdvizhnoj-elektrokarniz.ru[/url] .
razdvijnoi elektrokarniz_huei
18 Sep 25 at 7:46 am
Читатели получат представление о том, как современные технологии влияют на развитие медицины. Обсуждаются новые методы лечения, персонализированный подход и роль цифровых решений в повышении качества медицинских услуг.
Не упусти важное! – https://placesrf.ru/moscow/company/klinika-chastnaya-skoraya-pomoschy-1-v-odincovo-ul-arhitektora-vlasova
SteveMal
18 Sep 25 at 7:46 am
займер ру [url=zaimy-14.ru]займер ру[/url] .
zaimi_qeSr
18 Sep 25 at 7:48 am
Awesome issues here. I’m very happy to peer your article.
Thanks a lot and I am taking a look ahead to touch you.
Will you please drop me a mail?
آدرس دانشگاه تهران دانشکده روانشناسی
18 Sep 25 at 7:48 am
I’ll immediately clutch your rss as I can not to find your
email subscription link or newsletter service.
Do you have any? Kindly let me recognise in order that I may subscribe.
Thanks.
aesthetic clinic near me
18 Sep 25 at 7:50 am
Nice post. I learn something new and challenging on websites I stumbleupon every day.
It’s always useful to read through content from other authors
and use something from other websites.
выезд ритуального агента москва
18 Sep 25 at 7:51 am