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!
Thanks for the good writeup. It in fact was a enjoyment account it.
Look complicated to far introduced agreeable from you! However, how could we keep up a correspondence?
секретные промокоды Shopping Live
29 Jul 25 at 9:12 am
Интересная новость: Влияние достатка на здоровье и долголетие
ua-pulse-363
29 Jul 25 at 9:12 am
It’s an awesome post in support of all the online viewers; they will get advantage
from it I am sure.
vitesli motosiklet
29 Jul 25 at 9:15 am
Предлагаем вашему вниманию интересную справочную статью, в которой собраны ключевые моменты и нюансы по актуальным вопросам. Эта информация будет полезна как для профессионалов, так и для тех, кто только начинает изучать тему. Узнайте ответы на важные вопросы и расширьте свои знания!
Выяснить больше – https://amylynette.com/2017/03/31/1-john-aprils-scripture-writing-challenge
Jasonpaisa
29 Jul 25 at 9:19 am
Greetings! Very helpful advice within this article!
It’s the little changes that make the biggest changes.
Thanks for sharing!
تمدید مهلت اعلام علاقه مندی به رشته های گروه آزمایشی هنر ۱۴۰۴
29 Jul 25 at 9:22 am
order amoxicillin without prescription: ClearMeds Direct – where can i get amoxicillin 500 mg
BrianTub
29 Jul 25 at 9:24 am
Write more, thats all I have to say. Literally, it seems as
though you relied on the video to make your point.
You obviously know what youre talking about, why throw away your
intelligence on just posting videos to your blog when you could be giving
us something enlightening to read?
play288
29 Jul 25 at 9:30 am
where to get tinidazole for sale
how to get generic tinidazole without insurance
29 Jul 25 at 9:33 am
Этот информационный материал собраны данные, которые помогут лучше понять текущие тенденции и процессы в различных сферах жизни. Мы предоставляем четкий анализ, графики и примеры, чтобы информация была не только понятной, но и практичной для принятия решений.
Узнать напрямую – http://www.mingwong.org/windows-on-the-world-part-2/80_uccamingwong2015installation22lores
JosephGot
29 Jul 25 at 9:36 am
Читать в подробностях: Овсяные котлеты: простой и полезный рецепт
novosti24-416
29 Jul 25 at 9:38 am
1win [url=https://www.1win3064.ru]https://www.1win3064.ru[/url]
1win_xfPn
29 Jul 25 at 9:38 am
Читать в подробностях: Квашеная капуста: польза для здоровья и почему стоит включить в рацион
novosti24-635
29 Jul 25 at 9:38 am
Интересная новость: Полезные советы будущим родителям о здоровье от врачей
ua-pulse-809
29 Jul 25 at 9:44 am
Читать новость: Полезное тыквенное печенье: рецепты и секреты приготовления
dailynewsbytes-816
29 Jul 25 at 9:44 am
Интересная новость: Заготовка дров в лесу: законно ли собирать сухостой и как это сделать
ua-pulse-560
29 Jul 25 at 9:44 am
Эта публикация дает возможность задействовать различные источники информации и представить их в удобной форме. Читатели смогут быстро найти нужные данные и получить ответы на интересующие их вопросы. Мы стремимся к четкости и доступности материала для всех!
Узнать напрямую – https://231disposal.com/blog/3-ways-to-get-rid-of-a-couch-in-traverse-city-michigan
RogerVah
29 Jul 25 at 9:47 am
Hello friends, nice post and good arguments commented here, I am genuinely enjoying
by these.
m98 bet
29 Jul 25 at 9:48 am
взломанные игры с модами — это замечательный способ расширить функциональность игры.
Особенно если вы пользуетесь устройствами на платформе Android, модификации открывают перед вами новые возможности.
Я нравится использовать модифицированные версии игр, чтобы развиваться быстрее.
Модификации игр дают невероятную персонализированный подход,
что погружение в игру гораздо захватывающее.
Играя с плагинами, я могу добавить дополнительные функции, что добавляет новые приключения и
делает игру более непредсказуемой.
Это действительно удивительно, как такие моды могут улучшить игровой процесс, а при этом с максимальной безопасностью использовать такие взломанные версии можно без особых
неприятных последствий, если быть внимательным и следить за обновлениями.
Это делает каждый игровой процесс уникальным, а возможности практически широкие.
Советую попробовать такие игры с модами для Android — это может вдохновит на
новые приключения
взломанные игры с модами
29 Jul 25 at 9:49 am
прогнозы и ставки на спорт [url=https://www.sportbets27.ru]https://www.sportbets27.ru[/url] .
sportbets_zcKa
29 Jul 25 at 9:49 am
Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
Перейти к статье – https://www.bambousushi.be/2015/07/22/eggs-bacon-mac-cheese
Charleslow
29 Jul 25 at 9:51 am
В этой статье вы найдете познавательную и занимательную информацию, которая поможет вам лучше понять мир вокруг. Мы собрали интересные данные, которые вдохновляют на размышления и побуждают к действиям. Открывайте новую информацию и получайте удовольствие от чтения!
Подробнее – https://lifebnf.bonificaoficial.com/2024/09/25/ola-mundo
Gilbertsep
29 Jul 25 at 9:54 am
Читать в подробностях: Секрет пышного цветения спатифиллума: простое удобрение для обильного цветения
novosti24-913
29 Jul 25 at 10:08 am
Мега ссылка
RichardPep
29 Jul 25 at 10:10 am
Читать в подробностях: Антибиотики: главные ошибки при приеме – советы врача
novosti24-989
29 Jul 25 at 10:13 am
Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
Нажмите, чтобы узнать больше – https://finalreview.in/index.php/2023/06/08/neelkanth-rockminerals-ltd
Lionelzef
29 Jul 25 at 10:14 am
Ставки на спорт могут добавить азарт в просмотр матчей. Бонусные предложения способны существенно увеличить стартовый банк. Платежные методы содержат карты, электронные кошельки и криптовалюты. Официальный ресурс вавада сайт открыт для новых пользователей. Игроки ценят удобство интерфейса и быструю обработку ставок.
Robertgew
29 Jul 25 at 10:14 am
Having read this I believed it was rather informative.
I appreciate you finding the time and effort to put this short article together.
I once again find myself personally spending way too much time both reading and leaving comments.
But so what, it was still worthwhile!
Profile
29 Jul 25 at 10:21 am
Читать в подробностях: Эффективные советы для здорового похудения: подготовьтесь к лету
novosti24-116
29 Jul 25 at 10:23 am
Drug prescribing information. What side effects?
how to buy generic pioglitazone no prescription
Everything information about meds. Read now.
how to buy generic pioglitazone no prescription
29 Jul 25 at 10:24 am
Читать новость: Бузина: целебные свойства для здоровья кожи
dailynewsbytes-983
29 Jul 25 at 10:25 am
Читать новость: Смартфон и здоровье: как сократить время использования и улучшить самочувствие
dailynewsbytes-646
29 Jul 25 at 10:26 am
В этой статье вы найдете познавательную и занимательную информацию, которая поможет вам лучше понять мир вокруг. Мы собрали интересные данные, которые вдохновляют на размышления и побуждают к действиям. Открывайте новую информацию и получайте удовольствие от чтения!
Узнать напрямую – https://www.podasarbolado.com/planes-de-gestion
Matthewepinc
29 Jul 25 at 10:30 am
Читать в подробностях: Признаки нелюбви к себе: советы психолога
novosti24-855
29 Jul 25 at 10:32 am
Этот текст призван помочь читателю расширить кругозор и получить практические знания. Мы используем простой язык, наглядные примеры и структурированное изложение, чтобы сделать обучение максимально эффективным и увлекательным.
Что ещё? Расскажи всё! – https://f5fashion.vn/chi-tiet-hon-53-ve-chocolate-tang-sinh-nhat
Charlessuelp
29 Jul 25 at 10:44 am
Эта статья предлагает живое освещение актуальной темы с множеством интересных фактов. Мы рассмотрим ключевые моменты, которые делают данную тему важной и актуальной. Подготовьтесь к насыщенному путешествию по неизвестным аспектам и узнайте больше о значимых событиях.
Продолжить изучение – https://f5fashion.vn/xac-dinh-cong-thuc-hinh-thanh-nen-kim-cuong-hong
Derrickdog
29 Jul 25 at 10:46 am
В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
Секреты успеха внутри – https://babi-beauty.fr/signature-styles-the-best-haircuts-at-our-barber-shop
Coreynax
29 Jul 25 at 10:51 am
купить диплом вуза с реестром [url=http://www.arus-diplom31.ru]купить диплом вуза с реестром[/url] .
Priobresti diplom ob obrazovanii!_wfOl
29 Jul 25 at 10:51 am
кайтинг Кайт школа: надёжный старт в мир кайтсёрфинга. Профессиональные инструкторы, современное оборудование, безопасные условия обучения – все это ждет вас в кайт школе.
RamonLiata
29 Jul 25 at 10:55 am
Бонусные предложения могут значительно увеличить стартовый банк. Регистрация занимает несколько минут благодаря простой форме. Официальный ресурс https://blunavycrociere.com/ открыт для новых пользователей. Ставки на спорт могут добавить адреналин в просмотр матчей. Платежные методы содержат карты, электронные кошельки и криптовалюты.
Robertgew
29 Jul 25 at 10:56 am
Мы предлагаем вам подробное руководство, основанное на проверенных источниках и реальных примерах. Каждая часть публикации направлена на то, чтобы помочь вам разобраться в сложных вопросах и применить знания на практике.
Читать дальше – https://www.rapanalysis.com/kickknowledge/2018/05/11/20-the-next-rhyme-i-write-might-be-about-you-w-martin-connor
JosephSic
29 Jul 25 at 10:59 am
купить программу 1с для ип [url=https://kupit-1s22.ru/]kupit-1s22.ru[/url] .
kypit 1s_udmt
29 Jul 25 at 11:01 am
Для тех, кто в теме, будет очень актуально:
Особенно понравился материал про cyq.ru.
Ссылка ниже:
[url=https://cyq.ru]https://cyq.ru[/url]
Надеюсь, смог помочь.
rusPoito
29 Jul 25 at 11:02 am
After I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I receive 4 emails
with the exact same comment. There has to be an easy method you can remove me from that service?
Thank you!
wordpress web development
29 Jul 25 at 11:06 am
Этот информационный материал собраны данные, которые помогут лучше понять текущие тенденции и процессы в различных сферах жизни. Мы предоставляем четкий анализ, графики и примеры, чтобы информация была не только понятной, но и практичной для принятия решений.
Узнать напрямую – https://komisite.ru
LutherVAP
29 Jul 25 at 11:07 am
Эта информационная статья содержит полезные факты, советы и рекомендации, которые помогут вам быть в курсе последних тенденций и изменений в выбранной области. Материал составлен так, чтобы быть полезным и понятным каждому.
Все материалы собраны здесь – http://www.shopsv.ru
Jorgevem
29 Jul 25 at 11:08 am
Читать новость: Эффективные способы борьбы с черной пятнистостью на розах: спасаем любимые цветы
dailynewsbytes-646
29 Jul 25 at 11:12 am
прогнозы на матчи [url=http://prognoz-na-segodnya-na-sport8.ru]http://prognoz-na-segodnya-na-sport8.ru[/url] .
prognoz na segodnya na sport_hjSa
29 Jul 25 at 11:16 am
Читать новость: Почему опадает листва у фикуса зимой: что делать и как спасти растение
dailynewsbytes-974
29 Jul 25 at 11:19 am
кайтсёрфинг Доска для кайтсерфинга: выбор в зависимости от стиля
RamonLiata
29 Jul 25 at 11:22 am
이건 정말 매혹적이고 것입니다, 귀하는 매우
전문적인 블로거입니다. 당신의 RSS 피드에 가입했고,
당신의 웅장한 포스트를 더 찾고 있습니다.
또한, 제 소셜 네트워크에서 당신의 웹사이트를
공유했습니다.
Nice answer back in return of this issue with genuine arguments and describing everything
about that.
정품 비아그라 판매
29 Jul 25 at 11:24 am