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!
best casino app in usa, free spins for registration usa 2021 and are there
casinos in montreal united kingdom, or top paying how
much has online gambling increased, Alisha, casinos united states
Alisha
8 Oct 25 at 9:29 am
bingo slots uk, free spins australia and pokies free online canada, or new casino bonus uk
Also visit my web page; intervention gabe the gambler full episode
intervention gabe the gambler full episode
8 Oct 25 at 9:30 am
спортивные события [url=sport-novosti-1.ru]sport-novosti-1.ru[/url] .
sport novosti_xhpa
8 Oct 25 at 9:30 am
newbluebook – I like how articles are spaced out, no clutter on pages.
Tilda Crummitt
8 Oct 25 at 9:31 am
прогнозы на спорт сегодня [url=https://www.prognozy-ot-professionalov5.ru]прогнозы на спорт сегодня[/url] .
prognozi ot professionalov_jxSt
8 Oct 25 at 9:31 am
It’s difficult to find experienced people in this particular topic, however,
you seem like you know what you’re talking about! Thanks
situs bola slot
8 Oct 25 at 9:33 am
Этот информационный материал подробно освещает проблему наркозависимости, ее причины и последствия. Мы предлагаем информацию о методах лечения, профилактики и поддерживающих программах. Цель статьи — повысить осведомленность и продвигать идеи о необходимости борьбы с зависимостями.
Разобраться лучше – https://inlineonline.com/forums/topic/%D0%BD%D0%B0%D1%80%D0%BA%D0%BE%D0%BB%D0%BE%D0%B3%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B0%D1%8F-%D0%BA%D0%BB%D0%B8%D0%BD%D0%B8%D0%BA%D0%B0-%D1%81%D0%B0%D0%BD%D0%B0%D1%82%D0%B8-%D0%B2-%D0%BC%D0%BE%D1%81
Jamesemamy
8 Oct 25 at 9:37 am
новости футбольных клубов [url=http://sport-novosti-1.ru]http://sport-novosti-1.ru[/url] .
sport novosti_tjpa
8 Oct 25 at 9:38 am
After I originally commented I seem to have clicked on the -Notify me when new comments are
added- checkbox and from now on whenever a
comment is added I recieve four emails with the same comment.
Perhaps there is an easy method you are able to remove me from that service?
Thanks!
turkey visa from australia
8 Oct 25 at 9:41 am
лучшие прогнозы на спорт [url=http://prognozy-ot-professionalov5.ru]лучшие прогнозы на спорт[/url] .
prognozi ot professionalov_asSt
8 Oct 25 at 9:43 am
новости спорта россии [url=http://www.sport-novosti-1.ru]http://www.sport-novosti-1.ru[/url] .
sport novosti_txpa
8 Oct 25 at 9:44 am
спортивные новости сегодня [url=https://sport-novosti-1.ru/]https://sport-novosti-1.ru/[/url] .
sport novosti_wkpa
8 Oct 25 at 9:47 am
linebet app apk
linebet free download
8 Oct 25 at 9:47 am
Этот текст призван помочь читателю расширить кругозор и получить практические знания. Мы используем простой язык, наглядные примеры и структурированное изложение, чтобы сделать обучение максимально эффективным и увлекательным.
Ознакомиться с деталями – https://escturkey.com/we-had-an-interview-with-let-3
Samuelamilm
8 Oct 25 at 9:50 am
обзор спортивных событий [url=www.sport-novosti-1.ru/]www.sport-novosti-1.ru/[/url] .
sport novosti_iopa
8 Oct 25 at 9:52 am
https://palpalych.amebaownd.com/posts/57502832
Thomasbip
8 Oct 25 at 9:53 am
Wonderful goods from you, man. I have consider your stuff prior to
and you are just extremely magnificent. I really like what you
have acquired right here, certainly like what
you’re saying and the way in which by which you assert it.
You are making it entertaining and you continue to take care of to stay it smart.
I cant wait to learn far more from you. This is really a wonderful site.
DD&B Custom Home & Pool Builders home builders
8 Oct 25 at 9:53 am
свежие прогнозы на спорт [url=https://www.prognozy-ot-professionalov5.ru]https://www.prognozy-ot-professionalov5.ru[/url] .
prognozi ot professionalov_tlSt
8 Oct 25 at 9:54 am
goldmetalshop – This could really streamline the process of art authentication.
Raleigh Goade
8 Oct 25 at 9:55 am
В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
Доступ к полной версии – https://biyology.com/2018-04-02-oesterlicher-ueberfluss-was-tun
Donaldidiok
8 Oct 25 at 9:55 am
прогноз на сегодня на спорт [url=http://www.prognozy-ot-professionalov5.ru]http://www.prognozy-ot-professionalov5.ru[/url] .
prognozi ot professionalov_tiSt
8 Oct 25 at 9:59 am
Very nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts.
In any case I’ll be subscribing to your rss feed and I hope you
write again soon!
Vitamin33
8 Oct 25 at 9:59 am
The Minotaurus presale vesting program is a game-changer for early birds. Extend for bonuses and avoid FOMO on post-TGE pumps. $MTAUR could be the dark horse in blockchain games.
minotaurus presale
WilliamPargy
8 Oct 25 at 10:00 am
Эта статья полна интересного контента, который побудит вас исследовать новые горизонты. Мы собрали полезные факты и удивительные истории, которые обогащают ваше понимание темы. Читайте, погружайтесь в детали и наслаждайтесь процессом изучения!
Жми сюда — получишь ответ – https://lootienda.com.co/producto/loo12-s-5-la
JamesRip
8 Oct 25 at 10:01 am
Этот обзор предлагает структурированное изложение информации по актуальным вопросам. Материал подан так, чтобы даже новичок мог быстро освоиться в теме и начать использовать полученные знания в практике.
Давай разберёмся досконально – https://www.radhe-radhe.net/%E0%A4%B9%E0%A4%A8%E0%A5%81%E0%A4%AE%E0%A4%BE%E0%A4%A8-%E0%A4%9C%E0%A5%80-%E0%A4%95%E0%A5%80-%E0%A4%86%E0%A4%B0%E0%A4%A4%E0%A5%80-hanuman-ji-ki-aarti
Michaelniz
8 Oct 25 at 10:01 am
новости мирового спорта [url=http://sportivnye-novosti-1.ru/]новости мирового спорта[/url] .
sportivnie novosti_vxpi
8 Oct 25 at 10:02 am
прогнозы на футбол [url=www.kompyuternye-prognozy-na-futbol24.ru]www.kompyuternye-prognozy-na-futbol24.ru[/url] .
komputernie prognozi na fytbol_ilsl
8 Oct 25 at 10:02 am
I was curious if you ever thought of changing the structure of your site?
Its very well written; I love what youve got to say. But maybe
you could a little more in the way of content so people
could connect with it better. Youve got an awful lot of text for
only having 1 or two pictures. Maybe you could space it out
better?
buy likes for youtube
8 Oct 25 at 10:04 am
I’ve been following the Minotaurus presale closely, and it’s impressive how they’ve structured the tokenomics for long-term sustainability. The vesting bonuses are a smart incentive that could really reward patient holders. Excited to see $MTAUR launch and disrupt the blockchain gaming space.
minotaurus ico
WilliamPargy
8 Oct 25 at 10:04 am
Hi there, all is going well here and ofcourse every one is
sharing data, that’s really excellent, keep up writing.
نسخه تحت وب اسنپ
8 Oct 25 at 10:04 am
https://t.me/win_1_casino_play/2
Richardwap
8 Oct 25 at 10:05 am
железобетонные прогнозы на спорт [url=prognozy-ot-professionalov5.ru]prognozy-ot-professionalov5.ru[/url] .
prognozi ot professionalov_yqSt
8 Oct 25 at 10:05 am
вывод из запоя череповец
vivod-iz-zapoya-cherepovec013.ru
экстренный вывод из запоя череповец
alkogolizmcherepovecNeT
8 Oct 25 at 10:07 am
спортивные события [url=sport-novosti-1.ru]sport-novosti-1.ru[/url] .
sport novosti_gmpa
8 Oct 25 at 10:07 am
dayofthedeadatx – The idea of a global art registry feels like a game-changer.
Lauren Hladik
8 Oct 25 at 10:08 am
I’m really enjoying the theme/design of your blog. Do you ever run into any internet
browser compatibility problems? A number of my blog audience have complained about my site
not working correctly in Explorer but looks great in Safari.
Do you have any solutions to help fix this issue?
Pewny Fundaryx Ervaringen
8 Oct 25 at 10:08 am
discreet delivery for ED medication: safe online pharmacy for ED pills – FDA-approved Tadalafil generic
Morrisluh
8 Oct 25 at 10:10 am
новости легкой атлетики [url=www.sport-novosti-1.ru]www.sport-novosti-1.ru[/url] .
sport novosti_xkpa
8 Oct 25 at 10:11 am
В этой статье вы найдете уникальные исторические пересечения с научными открытиями. Каждый абзац — это шаг к пониманию того, как наука и события прошлого создают основу для технологического будущего.
Слушай внимательно — тут важно – https://trattoriadacesare.it/portfolio/da-cesare-al-pellegrino-a-roma-settimio-ha-riaperto-grazie-a-vignoli-del-casaletto
EduardoSciet
8 Oct 25 at 10:11 am
Ich bin total fasziniert von Snatch Casino, es ist eine dynamische Erfahrung. Die Auswahl an Titeln ist riesig, mit dynamischen Tischspielen. Die Hilfe ist schnell und professionell, garantiert sofortige Hilfe. Der Prozess ist einfach und reibungslos, jedoch die Angebote konnten gro?zugiger sein. Zusammenfassend Snatch Casino bietet garantiertes Vergnugen fur Crypto-Liebhaber ! Hinzu kommt die Site ist stylish und schnell, was das Spielvergnugen steigert.
HeistMasterH8zef
8 Oct 25 at 10:11 am
Cabinet IQ Austin
2419 S Bell Blvd, Cedar Park,
TX 78613, United Տtates
+12543183528
Warranty
Warranty
8 Oct 25 at 10:13 am
новости спорта [url=http://sport-novosti-1.ru/]http://sport-novosti-1.ru/[/url] .
sport novosti_gqpa
8 Oct 25 at 10:13 am
I wanted to thank you for this great read!! I absolutely enjoyed every little bit
of it. I have got you saved as a favorite to check out new stuff you
post…
led panel singapore
8 Oct 25 at 10:16 am
Этот информативный текст выделяется своими захватывающими аспектами, которые делают сложные темы доступными и понятными. Мы стремимся предложить читателям глубину знаний вместе с разнообразием интересных фактов. Откройте новые горизонты и развивайте свои способности познавать мир!
Что ещё нужно знать? – https://aesm.be/de-hepcee-charley
Alfredtit
8 Oct 25 at 10:24 am
Публикация предлагает читателю не просто информацию, а инструменты для анализа и саморазвития. Мы стимулируем критическое мышление, предлагая различные точки зрения и призывая к самостоятельному поиску решений.
Ознакомьтесь с аналитикой – https://hanne-bau.de/hello-world
CharlesRainy
8 Oct 25 at 10:25 am
obviously like your website however you need to check the spelling on quite a few of your posts.
A number of them are rife with spelling problems and I in finding it very bothersome to
tell the reality nevertheless I will definitely come again again.
teflonbevonás
8 Oct 25 at 10:26 am
This is really interesting, You’re a very skilled blogger.
I’ve joined your rss feed and look forward to
seeking more of your wonderful post. Also,
I’ve shared your web site in my social networks!
slot gacor
8 Oct 25 at 10:27 am
No matter if some one searches for hiis necesssary thing,
so he/she needs to be available that in detail, thherefore that thing is maintained over
here.
insure my car
8 Oct 25 at 10:28 am
https://neurocaredirect.shop/# can you get fluoxetine
Kennethtut
8 Oct 25 at 10:31 am
hey there and thank you for your info – I’ve certainly picked up anything new from right here.
I did however expertise some technical points using this
site, as I experienced to reload the website a lot
of times previous to I could get it to load correctly.
I had been wondering if your web hosting is OK? Not
that I am complaining, but sluggish loading instances times
will sometimes affect your placement in google and can damage your high-quality score if ads and marketing
with Adwords. Anyway I’m adding this RSS to my e-mail and
can look out for much more of your respective exciting content.
Make sure you update this again very soon.
راهنمای دریافت تاییدیه دیپلم
8 Oct 25 at 10:33 am