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=https://sportivnye-novosti-1.ru/]sportivnye-novosti-1.ru[/url] .
sportivnie novosti_hppi
8 Oct 25 at 3:35 pm
При регистрации аккаунта клиент может рассчитывать на
получение бонуса для новичков.
вулкан
8 Oct 25 at 3:35 pm
generic ivermectin online pharmacy: generic ivermectin online pharmacy – order Stromectol discreet shipping USA
Morrisluh
8 Oct 25 at 3:36 pm
Greate article. Keep writing such kind of information on your page.
Im really impressed by your site.
Hi there, You’ve done a fantastic job. I’ll certainly
digg it and individually suggest to my friends. I’m
sure they’ll be benefited from this web site.
Pink Salt Weight Loss
8 Oct 25 at 3:37 pm
самые точные прогнозы на футбол [url=www.kompyuternye-prognozy-na-futbol24.ru]самые точные прогнозы на футбол[/url] .
komputernie prognozi na fytbol_mxsl
8 Oct 25 at 3:38 pm
Hi to all, it’s in fact a good for me to visit this website, it contains priceless Information.
text to image
8 Oct 25 at 3:39 pm
1win casino скачать [url=http://1win5516.ru]1win casino скачать[/url]
1win_ycOa
8 Oct 25 at 3:40 pm
I enjoy what you guys are usually up too. Such clever work and reporting!
Keep up the great works guys I’ve included you guys to blogroll.
https://car-loans.us.com/
8 Oct 25 at 3:40 pm
точный прогнозы на футбол [url=http://www.kompyuternye-prognozy-na-futbol23.ru]http://www.kompyuternye-prognozy-na-futbol23.ru[/url] .
komputernie prognozi na fytbol_rlPi
8 Oct 25 at 3:41 pm
Если самостоятельные попытки выйти из запоя не даются, в стационаре «Частного Медика 24» в Воронеже оказывают помощь профессионально: круглосуточное наблюдение, дозированные капельницы, поддержка сердечно-сосудистой системы, нормализация сна и работы внутренних органов.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-v-stacionare-voronezh23.ru/]стационар вывод из запоя в воронеже[/url]
WilliamDef
8 Oct 25 at 3:41 pm
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! By the way,
how could we communicate?
Trenqor Logic AI Opinie
8 Oct 25 at 3:42 pm
вывод из запоя на дому в москве [url=http://vyvod-iz-zapoya-9.ru]http://vyvod-iz-zapoya-9.ru[/url] .
vivod iz zapoya_rcEl
8 Oct 25 at 3:42 pm
новости олимпиады [url=www.sportivnye-novosti-1.ru]новости олимпиады[/url] .
sportivnie novosti_xhpi
8 Oct 25 at 3:43 pm
обложки вайлдберрис Нейросеть для создания карточки товара Вайлдберриз – это специализированная нейросеть, обученная на данных о товарах, представленных на платформе Вайлдберриз (Wildberries). Она способна анализировать фотографии, описания, характеристики и отзывы покупателей, чтобы автоматически генерировать качественные и привлекательные карточки товаров, которые соответствуют требованиям Вайлдберриз и привлекают внимание потенциальных покупателей.
JeromeThatt
8 Oct 25 at 3:45 pm
Экскурсии по Казани — обзор маршрутов и лучших туров по Казани
Казань — жемчужина Поволжья с богатой историей и неповторимой культурой. Если вы ищете интересные экскурсии по Казани, на нашем сайте представлены лучшие маршруты — от обзорных программ до авторских прогулок.
[url=https://to-kazan.ru/tours/bolgar/avtobus-b]экскурсии в болгар из казани на автобусе[/url]
Экскурсии Казань — автобусные, пешеходные и тематические туры
Мы предлагаем разнообразные экскурсии Казань: обзорные автобусные маршруты (включают Кремль, Баумана, Кабан и Старо-Татарскую Слободу), пешеходные прогулки, гастрономические экскурсии, квесты и семейные форматы.
Что такое обзорная экскурсия по Казани
Отзывы туристов подтверждают: «Казань за 4 часа — экскурсия Казань за 4 часа + Кремль… экскурсовод Елена увлекла рассказом».
Программа включает:
посещение Казанского Кремля и мечети Кул-Шариф;
знакомство с озером Кабан, ул. Баумана и памятниками города .
https://to-kazan.ru/tours/ekskursii-kazan/obzornaya-avtobus
обзорные экскурсии по казани
Экскурсии в Казани — вечерние и ночные маршруты
Если вы хотите увидеть город в другом свете, выбирайте экскурсии в Казани вечером. Самый популярный формат — ночная экскурсия Казань, когда подсветка архитектурных объектов — Кремль, ЗАГС, мост Миллениум — создаёт невероятные впечатления.
Обзорные экскурсии Казань по ночному городу
Тур длится около 2–3 часов и включает: заезд к ключевым смотровым точкам, прогулку по набережной Казанки с иллюминацией, катание на колесе обозрения «Вокруг света».
Почему выбрать именно экскурсию Казань от нас?
Лицензированные гиды с живым, эмоциональным стилем (отзывы: «гид Марсель — просто супер-гид!»)
Малые группы для комфортного восприятия и безопасных остановок
Современный и удобный транспорт, радиогиды, подогрев зимний-зимний сезон
Возможность онлайн бронирования и подтверждение через сайт
Казань экскурсия — что входит и сколько длится
Автобус от центра Казани (чаще всего — район метро «Кремлёвская»)
Гид ведет экскурсию как в автобусе, так и при остановках
Основные объекты: Кремль, мечеть Кул-Шариф, улица Баумана, озеро Кабан, Старо-Татарская слобода, теcатр Камала
В вечерних версиях: мост Миллениум, дворец земледельцев, стадион «Казань Арена» ночью; плюс колесо обозрения
Сколько стоят экскурсии в Казани.
BrianRhype
8 Oct 25 at 3:49 pm
новости олимпиады [url=http://www.sportivnye-novosti-1.ru]новости олимпиады[/url] .
sportivnie novosti_cypi
8 Oct 25 at 3:51 pm
Статья посвящена анализу текущих трендов в медицине и их влиянию на жизнь людей. Мы рассмотрим новые технологии, методы лечения и значение профилактики в обеспечении долголетия и здоровья.
Эксклюзивная информация – https://mediry.ru/category/zdorove/page/5
DamonDouse
8 Oct 25 at 3:51 pm
Minotaurus ICO’s whitepaper highlights balanced token release. $MTAUR holders shape via DAO—democratic and cool. Casual market entry is spot on.
minotaurus coin
WilliamPargy
8 Oct 25 at 3:52 pm
Wow, this paragraph is pleasant, my younger sister is analyzing these things, thus I am going to convey her.
hotel with free WiFi Staffordshire
8 Oct 25 at 3:52 pm
Great article! I’ve been using Nitric Boost for a while now,
and it has made a noticeable difference in my energy levels.
If you’re serious about your fitness goals, you should definitely check it out.
Visit https://nitricboost–usa.us for details!
Nitric Boost Ultra
8 Oct 25 at 3:52 pm
Hello! Quick question that’s completely off topic. Do you know
how to make your site mobile friendly? My weblog looks weird when viewing
from my iphone4. I’m trying to find a template or plugin that might
be able to correct this issue. If you have any recommendations, please share.
With thanks!
Texas Strong | Air Conditioning & Heating | Houston
8 Oct 25 at 3:53 pm
прогноз игр по футболу [url=https://kompyuternye-prognozy-na-futbol24.ru/]kompyuternye-prognozy-na-futbol24.ru[/url] .
komputernie prognozi na fytbol_fssl
8 Oct 25 at 3:54 pm
I loved as much as you’ll receive carried out right here.
The sketch is tasteful, your authored material stylish.
nonetheless, you command get bought an nervousness over that you wish be delivering the
following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this increase.
천안온라인마케팅
8 Oct 25 at 3:54 pm
diamondescort18 – I keep coming back, this layout just feels right somehow.
Fonda Sopha
8 Oct 25 at 3:55 pm
Excited for $MTAUR coin’s role in personalizing characters—fancy outfits via tokens? Yes please. Presale’s low barrier ($10 min) opens it to everyone. Community events sound epic.
mtaur coin
WilliamPargy
8 Oct 25 at 3:56 pm
Ahaa, its pleasant conversation regarding this article
here at this webpage, I have read all that,
so at this time me also commenting here.
Opulatrix
8 Oct 25 at 3:56 pm
На стороне Чилийского государства у подножия вулкана находится Национальный парк с аналогичным названием – Льюльяйльяко, поэтому окрестности горы
очень живописные.
вулкан играть
8 Oct 25 at 3:58 pm
Great post! Thanks for sharing your thoughts on this topic.
I found it particularly interesting how you discussed the impact of community engagement
in today’s world. You might also like to explore [Completethyroid](https://info-completethyroid.us/)
info-completethyroid.us
8 Oct 25 at 3:59 pm
100 прогнозы на футбол [url=www.kompyuternye-prognozy-na-futbol24.ru/]www.kompyuternye-prognozy-na-futbol24.ru/[/url] .
komputernie prognozi na fytbol_gusl
8 Oct 25 at 4:00 pm
https://t.me/s/win_1_casino_play
Richardwap
8 Oct 25 at 4:00 pm
Если домашние методы не помогают, в «Частном Медике 24» обеспечат профессиональный вывод из запоя в стационаре.
Получить больше информации – [url=https://vyvod-iz-zapoya-v-stacionare21.ru/]нарколог вывод из запоя в стационаре нижний новгород[/url]
CharlesElage
8 Oct 25 at 4:03 pm
For the reason that the admin of this web page is working, no hesitation very shortly it will be renowned, due to its quality contents.
mua bán vũ khí
8 Oct 25 at 4:03 pm
купить диплом института [url=https://rudik-diplom4.ru/]купить диплом института[/url] .
Diplomi_qpOr
8 Oct 25 at 4:03 pm
купить диплом в сарове [url=https://rudik-diplom3.ru]купить диплом в сарове[/url] .
Diplomi_ilei
8 Oct 25 at 4:03 pm
Игрок может выводить в неделю до RUB, а сформировать заявку получится только после пополнения депозитного баланса на сумму от тысячи рублей.
азино 777 официальный сайт онлайн казино азино777
8 Oct 25 at 4:05 pm
Контроль
Подробнее тут – https://narkologicheskaya-klinika-lyubercy0.ru/
JerryIcelf
8 Oct 25 at 4:07 pm
самые точные прогнозы на футбол [url=www.kompyuternye-prognozy-na-futbol23.ru/]www.kompyuternye-prognozy-na-futbol23.ru/[/url] .
komputernie prognozi na fytbol_fiPi
8 Oct 25 at 4:08 pm
This piece of writing gives clear idea designed for the new viewers of blogging, that
genuinely how to do blogging and site-building.
Homecoming of American Soldiers
8 Oct 25 at 4:10 pm
анонимный. вывод. из. запоя. москва. [url=https://vyvod-iz-zapoya-9.ru/]vyvod-iz-zapoya-9.ru[/url] .
vivod iz zapoya_ykEl
8 Oct 25 at 4:11 pm
Формат
Ознакомиться с деталями – https://narkologicheskaya-klinika-serpuhov0.ru/platnaya-narkologicheskaya-klinika-v-serpuhove/
Donaldgug
8 Oct 25 at 4:11 pm
купить диплом в нижневартовске [url=https://rudik-diplom7.ru/]https://rudik-diplom7.ru/[/url] .
Diplomi_jlPl
8 Oct 25 at 4:13 pm
This post is really a good one it assists new
net people, who are wishing for blogging.
luxury custom home builders Gulf Shores
8 Oct 25 at 4:14 pm
прогнозы на футбол сегодня с анализом [url=https://kompyuternye-prognozy-na-futbol23.ru/]https://kompyuternye-prognozy-na-futbol23.ru/[/url] .
komputernie prognozi na fytbol_nxPi
8 Oct 25 at 4:15 pm
Гарантия качества на все купленные велосипеды kraken официальные ссылки кракен онион тор кракен онион зеркало кракен даркнет маркет
RichardPep
8 Oct 25 at 4:16 pm
как купить диплом в колледже [url=http://frei-diplom9.ru]как купить диплом в колледже[/url] .
Diplomi_ryea
8 Oct 25 at 4:18 pm
вывод из запоя бесплатно москва [url=http://www.vyvod-iz-zapoya-9.ru]http://www.vyvod-iz-zapoya-9.ru[/url] .
vivod iz zapoya_efEl
8 Oct 25 at 4:18 pm
[url=https://gracie.digital/]digital агентство[/url]
SpencerDal
8 Oct 25 at 4:19 pm
Пациентам в Самаре предлагается анонимное лечение в стационаре с круглосуточным уходом и безопасными условиями.
Выяснить больше – [url=https://vyvod-iz-zapoya-v-stacionare-samara24.ru/]нарколог вывод из запоя в стационаре в самаре[/url]
AndrewGoape
8 Oct 25 at 4:19 pm
Выездная наркологическая помощь в Нижнем Новгороде — капельница от запоя с выездом на дом. Мы обеспечиваем быстрое и качественное лечение без необходимости посещения клиники.
Получить дополнительную информацию – [url=https://vyvod-iz-zapoya-nizhnij-novgorod11.ru/]врач вывод из запоя[/url]
CarsonNed
8 Oct 25 at 4:20 pm
Экстренная помощь при запое в Нижнем Новгороде — капельница на дому от опытных врачей-наркологов.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-nizhnij-novgorod13.ru/]вывод из запоя капельница на дому в нижний новгороде[/url]
AaronDum
8 Oct 25 at 4:22 pm