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!
bonus pariuri online [url=http://1win40015.ru]http://1win40015.ru[/url]
1win_md_qxpi
13 Aug 25 at 12:22 pm
Этот информативный текст отличается привлекательным содержанием и актуальными данными. Мы предлагаем читателям взглянуть на привычные вещи под новым углом, предоставляя интересный и доступный материал. Получите удовольствие от чтения и расширьте кругозор!
Не упусти важное! – http://talim.darul-iman.org/2016/04/09/hello-world
BrianStymn
13 Aug 25 at 12:22 pm
В этом обзорном материале представлены увлекательные детали, которые находят отражение в различных аспектах жизни. Мы исследуем непонятные и интересные моменты, позволяя читателю увидеть картину целиком. Погрузитесь в мир знаний и удивительных открытий!
Ознакомьтесь с аналитикой – https://www.mystickers.be/2022/06/04/before-and-after-traditional-mixed-metal-kitchen-remodel
Josephsib
13 Aug 25 at 12:23 pm
1win букмекерская зеркало онлайн [url=https://www.1win1171.ru]https://www.1win1171.ru[/url]
1win_kg_swmr
13 Aug 25 at 12:24 pm
вино за 3000 рублей Белое вино под рыбу Белое вино – классический выбор к рыбным блюдам, но правильный выбор вина может значительно улучшить вкусовые ощущения. Для легких и нежных сортов рыбы, таких как треска или камбала, подойдут сухие белые вина с высокой кислотностью, например, Совиньон Блан или Альбариньо. Они подчеркнут свежесть рыбы и не перебьют ее вкус.
Michaelcoomy
13 Aug 25 at 12:25 pm
В этом обзорном материале представлены увлекательные детали, которые находят отражение в различных аспектах жизни. Мы исследуем непонятные и интересные моменты, позволяя читателю увидеть картину целиком. Погрузитесь в мир знаний и удивительных открытий!
Не пропусти важное – http://waffenblog.tetra-gun.de/?p=235
Josephsib
13 Aug 25 at 12:27 pm
https://shootinfo.com/author/redisabellecasillas0605/?pt=ads
Claudetrige
13 Aug 25 at 12:28 pm
Hey there! This is kind of off topic but I need some
advice from an established blog. Is it very hard to set up your own blog?
I’m not very techincal but I can figure things out pretty fast.
I’m thinking about setting up my own but I’m not sure where to start.
Do you have any tips or suggestions? Thank you
canada pharmacy
13 Aug 25 at 12:28 pm
промокод при регистрации 1win [url=https://1win1169.ru]https://1win1169.ru[/url]
1win_kg_hjpn
13 Aug 25 at 12:33 pm
1win официальный сайт 1вин промокоды [url=http://1win1168.ru/]http://1win1168.ru/[/url]
1win_ozma
13 Aug 25 at 12:34 pm
MediDirect USA [url=https://medidirectusa.com/#]safeway pharmacy methotrexate[/url] MediDirect USA
Houstonfloma
13 Aug 25 at 12:36 pm
https://odysee.com/@lindiswestwood
Thomasmub
13 Aug 25 at 12:36 pm
как скачать 1win [url=https://1win1170.ru]https://1win1170.ru[/url]
1win_kg_diEr
13 Aug 25 at 12:41 pm
сайт 1вин вход [url=http://1win1168.ru/]http://1win1168.ru/[/url]
1win_okma
13 Aug 25 at 12:41 pm
Клиника «Доктор Трезвость» в Сочи предлагает современное лечение запоя, основанное на комплексном подходе и индивидуальной программе для каждого пациента. Наши лицензированные специалисты используют передовые методы терапии, что позволяет достичь быстрого и безопасного вывода из запоя, а также создать надежную основу для дальнейшей реабилитации.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-sochi777.ru/]срочный вывод из запоя[/url]
JosephCix
13 Aug 25 at 12:48 pm
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 one or 2 images.
Maybe you could space it out better?
MadridX
13 Aug 25 at 12:49 pm
В этой статье вы найдете познавательную и занимательную информацию, которая поможет вам лучше понять мир вокруг. Мы собрали интересные данные, которые вдохновляют на размышления и побуждают к действиям. Открывайте новую информацию и получайте удовольствие от чтения!
Лучшее решение — прямо здесь – https://espigaoalerta.com.br/2023/02/03/inova-seleciona-projetos-para-programa-if-mais-empreendedor-portal-institucional-ifsp
JoshuaVat
13 Aug 25 at 12:50 pm
На сайте https://mantovarka.ru представлено огромное количество рецептов самых разных блюд, которыми вы сможете угостить домашних, родственников, близких людей. Есть самый простой рецепт манной каши, которая понравится даже детям. С этим сайтом получится приготовить, в том числе, и сложные блюда: яблочное повидло, клубничный сок, хлеб в аэрогриле, болгарский перец вяленый, канапе на крекерах и многое другое. Очень много блюд для правильного питания, которые понравятся всем, кто следит за весом. Все рецепты сопровождаются фотографиями, красочными картинками.
Zuyabroweld
13 Aug 25 at 12:51 pm
Этот увлекательный информационный материал подарит вам массу новых знаний и ярких эмоций. Мы собрали для вас интересные факты и сведения, которые обогатят ваш опыт. Откройте для себя увлекательный мир информации и насладитесь процессом изучения!
Читать дальше – http://rla.la/archives/77
JoshuaVat
13 Aug 25 at 12:54 pm
https://say.la/read-blog/124740
Thomasmub
13 Aug 25 at 12:55 pm
автоматика somfy [url=https://avtomatika-somfy77.ru/]https://avtomatika-somfy77.ru/[/url] .
avtomatika somfy_anOl
13 Aug 25 at 1:01 pm
один вин скачать [url=http://1win1169.ru/]http://1win1169.ru/[/url]
1win_kg_khpn
13 Aug 25 at 1:03 pm
вывод из запоя цена
vivod-iz-zapoya-omsk004.ru
вывод из запоя
zapojomskNeT
13 Aug 25 at 1:04 pm
https://rant.li/oubpiagahihi/dortmund-kupit-ekstazi-mdma-lsd-kokain
Claudetrige
13 Aug 25 at 1:10 pm
сколько стоит перепланировка квартиры [url=https://proekt-pereplanirovki-kvartiry5.ru/]сколько стоит перепланировка квартиры[/url] .
proekt pereplanirovki kvartiri_mcSn
13 Aug 25 at 1:11 pm
Sweet blog! I found it while searching on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem
to get there! Thank you
Best SEO Services
13 Aug 25 at 1:12 pm
https://wanderlog.com/view/rswfpsbjul/
Thomasmub
13 Aug 25 at 1:14 pm
Эта информационная заметка содержит увлекательные сведения, которые могут вас удивить! Мы собрали интересные факты, которые сделают вашу жизнь ярче и полнее. Узнайте нечто новое о привычных аспектах повседневности и откройте для себя удивительный мир информации.
Подробная информация доступна по запросу – https://espigaoalerta.com.br/2023/12/20/agencia-minas-gerais-ano-letivo-da-rede-publica-estadual-de-ensino-mineira-termina-nesta-quarta-feira-20-12
Waltersop
13 Aug 25 at 1:15 pm
ilucky88
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
ilucky88
13 Aug 25 at 1:19 pm
Everything is very open with a really clear clarification of the
challenges. It was definitely informative. Your website is very useful.
Thank you for sharing!
website
13 Aug 25 at 1:19 pm
dragonmoney
RobertAmeks
13 Aug 25 at 1:25 pm
промокод для 1win при пополнении счета [url=www.1win1170.ru]www.1win1170.ru[/url]
1win_kg_xbEr
13 Aug 25 at 1:29 pm
Arialief is getting great feedback for its ability to ease joint discomfort and
improve flexibility. Many users mention feeling less stiffness and more mobility in their daily routines.
It’s a promising natural choice for supporting long-term
joint and mobility health.
Arialief
13 Aug 25 at 1:30 pm
Калькулятор военной пенсии — реально полезный инструмент!
https://voenschet.ru/
новости СВО
13 Aug 25 at 1:31 pm
https://potofu.me/xt1z8nht
Thomasmub
13 Aug 25 at 1:34 pm
узнать провайдера по адресу уфа
ufa-domashnij-internet005.ru
провайдер по адресу
inernetufaelini
13 Aug 25 at 1:34 pm
https://indianmedsone.shop/# indian pharmacy paypal
Bruceben
13 Aug 25 at 1:36 pm
Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
Почему это важно? – https://espigaoalerta.com.br/2024/05/06/agencia-minas-gerais-minas-conclui-com-exito-2o-ciclo-do-pacto-nacional-pela-gestao-das-aguas
Waltersop
13 Aug 25 at 1:41 pm
dragon money официальный сайт
RobertAmeks
13 Aug 25 at 1:42 pm
Am Anfang war ich nicht ganz überzeugt, als ich von service.fit gehört habe.
Es gibt ja heutzutage haufenweise Optionen, deshalb war ich zurückhaltend.
Aber dann hab ich mir gedacht: Bonus ohne Einzahlung klingt
spannend, und meine Erwartungen wurden übertroffen. Nach dem Spinmama Login gab’s Freispiele, kostenlos.
Alles läuft flüssig, und die App läuft sogar noch besser als erwartet.
Was Spinmama besonders macht: Auch Bestandskunden bekommen Bonus Codes.
Das hab ich so selten erlebt. Für mich ist Spinmama aktuell ganz
vorne dabei. Wer ohne Risiko durchstarten will, ist bei Spinmama genau richtig.
SG
13 Aug 25 at 1:43 pm
https://ucgp.jujuy.edu.ar/profile/ueegjqbofedo/
Claudetrige
13 Aug 25 at 1:51 pm
sildenafil citrate pharmacy: MediDirect USA – MediDirect USA
Justinsoync
13 Aug 25 at 1:51 pm
https://hub.docker.com/u/elasrivahidi
Thomasmub
13 Aug 25 at 1:53 pm
Этот увлекательный информационный материал подарит вам массу новых знаний и ярких эмоций. Мы собрали для вас интересные факты и сведения, которые обогатят ваш опыт. Откройте для себя увлекательный мир информации и насладитесь процессом изучения!
Лучшее решение — прямо здесь – https://healthylemonlife.com/chou-farci-vegetarien-aux-lentilles
Bobbykig
13 Aug 25 at 1:55 pm
Hello There. I discovered your weblog using msn. This is a very well
written article. I’ll make sure to bookmark it and return to learn more of your useful info.
Thank you for the post. I’ll certainly comeback.
Pedestals Floral Decorators - Wedding & Event Florist of Long Island
13 Aug 25 at 1:59 pm
Лоукост авиабилеты https://lowcost-flights.com.ua по самым выгодным ценам. Сравните предложения ведущих авиакомпаний, забронируйте онлайн и путешествуйте дешево.
lowcost-flights-540
13 Aug 25 at 2:00 pm
Предлагаю услуги https://uslugi.yandex.ru/profile/DmitrijR-2993571 копирайтинга, SEO-оптимизации и графического дизайна. Эффективные тексты, высокая видимость в поиске и привлекательный дизайн — всё для роста вашего бизнеса.
DavidRooke
13 Aug 25 at 2:03 pm
Ⅴery good blߋg post. I certainly love tһiѕ site.
Thanks!
My page – hivnm; https://nogami-nohken.jp,
https://nogami-nohken.jp
13 Aug 25 at 2:06 pm
1win официальный промокод [url=http://1win1170.ru]1win официальный промокод[/url]
1win_kg_aoEr
13 Aug 25 at 2:08 pm
Эта статья для ознакомления предлагает читателям общее представление об актуальной теме. Мы стремимся представить ключевые факты и идеи, которые помогут читателям получить представление о предмете и решить, стоит ли углубляться в изучение.
Изучить аспект более тщательно – https://www.fsgeschichtebonn.de/fslogo-2
MauriceEvila
13 Aug 25 at 2:08 pm