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://kapelnica-ot-zapoya-lyubercy11.ru/]врач нарколог на дом[/url]
Davidaerop
19 Jul 25 at 6:00 pm
indian pharmacy paypal: IndiaMedsHub – IndiaMedsHub
BobbyAcany
19 Jul 25 at 6:02 pm
Наркологическая клиника в Краснодаре представляет собой специализированное медицинское учреждение, где оказывается профессиональная помощь пациентам с различными формами зависимости. Современные подходы к лечению позволяют обеспечить эффективную и безопасную терапию, направленную на восстановление физического и психического здоровья.
Углубиться в тему – [url=https://narkologicheskaya-klinika-krasnodar00.ru/]наркологическая клиника краснодар.[/url]
Andreslah
19 Jul 25 at 6:02 pm
В нашей клинике “Сила воли” в Иркутске вы можете получить помощь как в стационаре, так и на дому. Мы предлагаем два вида услуг:
Исследовать вопрос подробнее – [url=https://kapelnica-ot-zapoya-irkutsk3.ru/]капельница от запоя на дому цена[/url]
PatrickCrymn
19 Jul 25 at 6:03 pm
Продолжительный запой приводит к накоплению токсинов в организме, нарушению работы внутренних органов и развитию серьезных осложнений. Чем дольше человек находится в состоянии алкогольной интоксикации, тем выше риск повреждения сердца, печени и почек. Экстренное вмешательство позволяет оперативно вывести токсины, восстановить нормальные обменные процессы и предотвратить развитие хронических заболеваний.
Изучить вопрос глубже – http://vyvod-iz-zapoya-vladimir000.ru
MichaelBrees
19 Jul 25 at 6:06 pm
https://poverkaekb.ru
Lesteretedy
19 Jul 25 at 6:07 pm
E2bet Trang web trò chơi trực tuyến lớn nhất việt nam tham gia ngay và chơi có trách nhiệm.
Nền tảng này chỉ phù hợp với người từ
18 tuổi trở lên.
E2BET
19 Jul 25 at 6:09 pm
ai therapist chat [url=https://www.ai-therapist21.com]https://www.ai-therapist21.com[/url] .
AI Therapist_djPa
19 Jul 25 at 6:10 pm
Have you ever thought about including a little bit more than just your articles?
I mean, what you say is important and everything. But imagine if you added some great pictures
or videos to give your posts more, “pop”! Your
content is excellent but with images and videos,
this blog could definitely be one of the very best in its niche.
Awesome blog!
888starz
19 Jul 25 at 6:16 pm
мостбет демо игра [url=https://mostbet4075.ru]мостбет демо игра[/url]
mostbet_dpOr
19 Jul 25 at 6:18 pm
Запой — опасное состояние, которое стремительно приводит к тяжелым последствиям для организма. Обычные домашние методы восстановления при алкогольной интоксикации не только малоэффективны, но и небезопасны. Для быстрого и безопасного выведения токсинов требуется профессиональная помощь нарколога с применением капельницы от запоя.
Ознакомиться с деталями – [url=https://kapelnica-ot-zapoya-moskva000.ru/]врача капельницу от запоя москва[/url]
Williampeema
19 Jul 25 at 6:18 pm
Good day! Would you mind if I share your blog with my zynga group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Many thanks
Luxury limo near me
LhaneDrync
19 Jul 25 at 6:20 pm
купить проведенный аттестат за 11 класс [url=http://arus-diplom25.ru]купить проведенный аттестат за 11 класс[/url] .
Diplomi_zaot
19 Jul 25 at 6:21 pm
mostbet app android [url=http://mostbet-download-app-apk.com]mostbet app android[/url] .
mostbet apk_ljPi
19 Jul 25 at 6:24 pm
Мега сайт
RichardPep
19 Jul 25 at 6:25 pm
валютные платежи через агентов Оплата за границу может быть сложной задачей, требующей знания валютного законодательства и особенностей международных банковских операций. Платежные агенты берут на себя эту задачу, обеспечивая бесперебойный процесс оплаты товаров и услуг.
Jasondug
19 Jul 25 at 6:30 pm
kraken зеркало
DavidFoogs
19 Jul 25 at 6:32 pm
Nice blog right here! Also your website quite a bit up fast!
What web host are you using? Can I get your affiliate link to your host?
I want my site loaded up as fast as yours lol
Full Report
19 Jul 25 at 6:33 pm
Hello I am so thrilled I found your blog, I really found you by error, while I
was browsing on Aol for something else, Anyways I am here now and
would just like to say thank you for a incredible post and
a all round enjoyable blog (I also love the theme/design), I don’t have time to look over it all at the
moment but I have saved it and also included your RSS feeds, so when I have
time I will be back to read much more, Please do keep up the fantastic job.
margiela replica shoes
19 Jul 25 at 6:36 pm
Каждый пациент получает персонализированную программу, учитывающую стадию заболевания, сопутствующие патологии и социальный статус. На основании анализа состояния назначается оптимальная терапия, что значительно повышает эффективность лечения. Врач-нарколог подбирает препараты с учетом индивидуальной переносимости, а психотерапевт разрабатывает план психологической поддержки.
Детальнее – https://lechenie-alkogolizma-sochi00.ru/klinika-lecheniya-alkogolizma-v-sochi
MichaelTum
19 Jul 25 at 6:39 pm
доставка технической воды в спб [url=https://dostavka-tehnicheskoi-vodi.ru/]dostavka-tehnicheskoi-vodi.ru[/url] .
dostavka tehnicheskoi vodi_bmoa
19 Jul 25 at 6:50 pm
В клинике применяются различные методики, включая медикаментозную терапию, психотерапию и комплексную реабилитацию. Врачебная команда использует доказанные протоколы лечения, что подтверждается рекомендациями ведущих специалистов в области наркологии и психиатрии. Подробнее о современных методах можно узнать на сайте Министерства здравоохранения РФ.
Получить больше информации – https://narkologicheskaya-klinika-sochi0.ru/chastnaya-narkologicheskaya-klinika-sochi
DarwinGeada
19 Jul 25 at 6:54 pm
Does your website have a contact page? I’m having problems locating it but, I’d
like to shoot you an email. I’ve got some suggestions for
your blog you might be interested in hearing.
Either way, great website and I look forward to seeing it grow over time.
https://brookehofsess.com/
Warisan Budaya
19 Jul 25 at 6:54 pm
Онлайн-консультации с психологом экономят время. Детский психолог онлайн научит вашего ребенка справляться с эмоциями.
детский психолог калуга хороший отзывы
Ernestpoive
19 Jul 25 at 6:57 pm
онлайн оценка часов по фото для скупки [url=https://ocenka-chasov-onlajn10.ru/]https://ocenka-chasov-onlajn10.ru/[/url] .
ocenka chasov onlain_fbpr
19 Jul 25 at 6:59 pm
Основные показания для проведения капельницы на дому:
Детальнее – [url=https://kapelnica-ot-zapoya-moskva0.ru/]капельница от запоя анонимно[/url]
Michaelvoigo
19 Jul 25 at 7:00 pm
I have been surfing online more than three hours
today, yet I never found any interesting article like yours.
It is pretty worth enough for me. In my opinion, if all web owners and bloggers made good
content as you did, the web will be much more useful than ever
before.
residential locksmith
19 Jul 25 at 7:04 pm
платежный агент рф Агент валютных платежей – это компания или физическое лицо, которое оказывает услуги по осуществлению валютных платежей.
Jasondug
19 Jul 25 at 7:06 pm
http://indiamedshub.com/# IndiaMedsHub
RobertEmard
19 Jul 25 at 7:07 pm
melbet главный сайт [url=http://melbet3004.com/]http://melbet3004.com/[/url]
melbet_orMt
19 Jul 25 at 7:09 pm
Клиника «НаркоМед» в Екатеринбурге предоставляет полный спектр наркологической помощи круглосуточно и анонимно. Благодаря мобильным бригадам и выездам на дом, пациенты получают профессиональное лечение прямо в комфортной обстановке, без стресса, связанного с госпитализацией. В статье подробно рассматриваются услуги клиники, этапы терапии и ключевые преимущества обращения в «НаркоМед».
Получить дополнительную информацию – [url=https://narkologicheskaya-klinika-ekaterinburg0.ru/]частная наркологическая клиника екатеринбург.[/url]
RaymondHal
19 Jul 25 at 7:14 pm
I really like what you guys tend to be up too. This sort of
clever work and exposure! Keep up the great works
guys I’ve incorporated you guys to my personal blogroll.
https://jobs.alibeyk.com/employer/xn-80abdkqbhbf-2agfgrim-2k/
19 Jul 25 at 7:16 pm
melbet kz скачать [url=https://www.melbet3004.com]https://www.melbet3004.com[/url]
melbet_ewMt
19 Jul 25 at 7:16 pm
buy antibiotics from mexico [url=http://medimexicorx.com/#]order from mexican pharmacy online[/url] buy from mexico pharmacy
DavidRhinc
19 Jul 25 at 7:17 pm
доставка технической воды в спб [url=https://dostavka-tehnicheskoi-vodi.ru/]https://dostavka-tehnicheskoi-vodi.ru/[/url] .
dostavka tehnicheskoi vodi_ihoa
19 Jul 25 at 7:18 pm
sms bomber
GerardobaR
19 Jul 25 at 7:26 pm
kraken зеркало
DavidFoogs
19 Jul 25 at 7:26 pm
онлайн оценка наручных часов [url=ocenka-chasov-onlajn10.ru]ocenka-chasov-onlajn10.ru[/url] .
ocenka chasov onlain_bopr
19 Jul 25 at 7:28 pm
zithromax mexican pharmacy: zithromax mexican pharmacy – MediMexicoRx
BobbyAcany
19 Jul 25 at 7:30 pm
ai therapy bot [url=www.ai-therapist22.com]www.ai-therapist22.com[/url] .
AI Therapist_gmsr
19 Jul 25 at 7:33 pm
В обычных условиях цена услуги составляет от 1500 до 2500 рублей. Эта сумма включает выезд специалиста, первичный осмотр и базовую детоксикационную терапию.
Детальнее – http://vyvod-iz-zapoya-vladimir00.ru/vyvod-iz-zapoya-na-domu-vladimir/https://vyvod-iz-zapoya-vladimir00.ru
TimothyEvema
19 Jul 25 at 7:33 pm
1win [url=www.1win3072.ru]www.1win3072.ru[/url]
1win_bxkr
19 Jul 25 at 7:33 pm
Самостоятельно выйти из запоя — почти невозможно. В Люберцах врачи клиники проводят медикаментозный вывод из запоя с круглосуточным выездом. Доверяйте профессионалам.
Исследовать вопрос подробнее – [url=https://kapelnica-ot-zapoya-lyubercy12.ru/]капельница от запоя на дому люберцы[/url]
DewayneFausy
19 Jul 25 at 7:35 pm
Лечение алкоголизма в Краснодаре представляет собой комплекс мероприятий, направленных на полное восстановление физического и психологического состояния пациента. Этот процесс требует профессионального подхода, включающего диагностику, детоксикацию, терапию зависимости и реабилитацию. Информация о современных методах терапии доступна на официальном портале Минздрава РФ.
Детальнее – [url=https://lechenie-alkogolizma-krasnodar00.ru/]лечение наркомании и алкоголизма в краснодаре[/url]
Virgilcic
19 Jul 25 at 7:35 pm
Hello, after reading this amazing article i am too happy to share my
know-how here with mates.
ganha bet
19 Jul 25 at 7:41 pm
После завершения основного курса лечения в наркологической клинике в Краснодаре пациентам предлагаются программы социальной реабилитации. Они направлены на восстановление социальных навыков, адаптацию в обществе и предотвращение рецидивов. Ключевым элементом здесь является поддержка со стороны профессиональных социальных работников и участие в группах взаимопомощи.
Углубиться в тему – https://narkologicheskaya-klinika-krasnodar0.ru/anonimnaya-narkologicheskaya-klinika-v-krasnodare
ThomasIdeaf
19 Jul 25 at 7:41 pm
Психолог оценили 2204 раз
Психолог
19 Jul 25 at 7:41 pm
Во-вторых, реабилитация является неотъемлемой частью нашего подхода. Мы понимаем, что избавление от физической зависимости — это только первый шаг. Важной задачей является восстановление социального статуса, создание новых привычек и умение управлять своей жизнью без наркотиков или алкоголя. Наша клиника предлагает групповые и индивидуальные занятия, направленные на изменение поведения и мышления.
Подробнее можно узнать тут – https://kapelnica-ot-zapoya-irkutsk.ru/kapelnica-ot-zapoya-cena-v-irkutske
Josephshoow
19 Jul 25 at 7:43 pm
1win գրանցում [url=1win3072.ru]1win գրանցում[/url]
1win_skkr
19 Jul 25 at 7:45 pm
can you get lasix without dr prescription
cost lasix pills
19 Jul 25 at 7:47 pm