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!
https://mannvital.shop/# Mann Vital
Davidjealp
27 Oct 25 at 8:03 pm
куплю диплом медсестры в москве [url=https://frei-diplom15.ru]куплю диплом медсестры в москве[/url] .
Diplomi_qjoi
27 Oct 25 at 8:05 pm
услуги гидроизоляции подвала [url=www.gidroizolyaciya-cena-7.ru/]www.gidroizolyaciya-cena-7.ru/[/url] .
gidroizolyaciya cena_aoSi
27 Oct 25 at 8:07 pm
В городе и пригороде организован круглосуточный приём с возможностью выезда на дом. Координатор уточняет только клинически значимые данные: регулярные препараты и дозировки, аллергии, недавние эпизоды судорог/психозов, доступ к розетке, возможность обеспечить «тихое окно» на 2–3 часа. Если в квартире многолюдно или идёт ремонт, предложим «тихий» стационар с отдельным входом и тем же ведущим врачом; после стабилизации зеркально перенесём маршрут обратно домой, чтобы не терять темп и доверие.
Детальнее – [url=https://narkologicheskaya-klinika-v-nizhnem-novgorode16.ru/]наркологическая клиника цены нижний новгород[/url]
Prestonned
27 Oct 25 at 8:08 pm
кракен qr код
kraken зеркало
Henryamerb
27 Oct 25 at 8:11 pm
kraken vk5
kraken
Henryamerb
27 Oct 25 at 8:12 pm
В первые два часа после прибытия бригады важно не «сделать побольше», а принять наименьшее количество решений, которое реально меняет картину. Команда фиксирует базовую линию (ЧСС, АД, SpO?, температура, шкала тошноты/тремора), даёт антиеметический мост и запускает регидратацию. При тахикардии — добавляет антивегетативный контур. Семья получает сценарий ночи: когда приглушить свет, когда предложить воду, какие цифры на тонометре считать нормой и при каких — звонить. Такой протокол экономит время и силы, а главное — превращает вечер из хаотичного в управляемый, снижая риск ночных экстренных вызовов и «эмоциональных качелей».
Получить дополнительные сведения – [url=https://kapelnicza-ot-zapoya-v-chelyabinske16.ru/]капельница от запоя недорого в челябинске[/url]
Craigtic
27 Oct 25 at 8:12 pm
I absolutely love your blog and find almost all of your post’s
to be what precisely I’m looking for. Would you offer guest writers to write content in your case?
I wouldn’t mind writing a post or elaborating on a lot
of the subjects you write with regards to here. Again,
awesome weblog!
webpage
27 Oct 25 at 8:13 pm
купить номер телефона виртуальный для смс
MiguelActic
27 Oct 25 at 8:14 pm
Наркологическая клиника в клинике в Казани — это современный медицинский центр, специализирующийся на лечении алкогольной и наркотической зависимости. Основное направление деятельности заключается в проведении детоксикации, восстановлении работы нервной системы и формировании устойчивой мотивации к отказу от употребления психоактивных веществ. Все процедуры проводятся в условиях полной анонимности и медицинского контроля, что обеспечивает безопасность и доверие со стороны пациентов. Программа терапии строится по принципам доказательной медицины и индивидуального подбора лечебных средств.
Разобраться лучше – https://narkologicheskaya-clinika-v-kazani16.ru/chastnaya-narkologicheskaya-klinika-kazan/
WyattHag
27 Oct 25 at 8:16 pm
kraken официальный
кракен vpn
Henryamerb
27 Oct 25 at 8:17 pm
купить диплом в северодвинске [url=www.rudik-diplom1.ru/]купить диплом в северодвинске[/url] .
Diplomi_user
27 Oct 25 at 8:18 pm
наркологические клиники в москве частные [url=http://narkologicheskaya-klinika-25.ru/]http://narkologicheskaya-klinika-25.ru/[/url] .
narkologicheskaya klinika_soPl
27 Oct 25 at 8:19 pm
Капельницы в «ЧелМед Детокс» — это модульная конструкция, а не «микс на удачу». База — регидратация и коррекция электролитов. По показаниям добавляются антиэметики, антивегетативные средства, поддержка метаболизма, витамины группы B, магний. Отдельным блоком идёт ночной модуль: задача — собрать восстановительный сон, не «перелечивая» пациента. Каждый модуль имеет свою цель и стоп-критерий. Врач избегает одновременного запуска нескольких новых элементов, чтобы видеть причинно-следственную связь и точно понимать, что именно работает у конкретного человека.
Получить больше информации – [url=https://vyvod-iz-zapoya-v-chelyabinske16.ru/]вывод из запоя вызов[/url]
MichaelPix
27 Oct 25 at 8:19 pm
наркология клиника [url=https://narkologicheskaya-klinika-27.ru/]наркология клиника[/url] .
narkologicheskaya klinika_dspl
27 Oct 25 at 8:20 pm
Hey there I am so excited I found your webpage, I really found you by error, while I was looking
on Google for something else, Anyways I am here now and would just like to say cheers for a incredible post and a all round
entertaining blog (I also love the theme/design), I don’t have
time to look over it all at the minute but I have bookmarked
it and also included your RSS feeds, so when I have time I will be back
to read more, Please do keep up the fantastic work.
Visit This Link
27 Oct 25 at 8:21 pm
купить диплом техникума с реестром [url=www.frei-diplom10.ru]купить диплом техникума с реестром[/url] .
Diplomi_vuEa
27 Oct 25 at 8:21 pm
анонимная наркология [url=https://narkologicheskaya-klinika-25.ru/]анонимная наркология[/url] .
narkologicheskaya klinika_xgPl
27 Oct 25 at 8:23 pm
kraken онлайн
kraken 2025
Henryamerb
27 Oct 25 at 8:24 pm
наркологические услуги москвы [url=http://www.narkologicheskaya-klinika-27.ru]http://www.narkologicheskaya-klinika-27.ru[/url] .
narkologicheskaya klinika_jbpl
27 Oct 25 at 8:31 pm
Получить диплом о высшем образовании можем помочь. Куплю диплом техникума недорого в Волжском – [url=http://diplomybox.com/kupit-diplom-tekhnikuma-v-volzhskom/]diplomybox.com/kupit-diplom-tekhnikuma-v-volzhskom[/url]
Cazrsvm
27 Oct 25 at 8:31 pm
кракен онлайн
kraken зеркало
Henryamerb
27 Oct 25 at 8:32 pm
аренда номера телефона для смс
MiguelActic
27 Oct 25 at 8:33 pm
кракен обмен
кракен 2025
Henryamerb
27 Oct 25 at 8:33 pm
платная наркологическая клиника [url=http://narkologicheskaya-klinika-25.ru/]платная наркологическая клиника[/url] .
narkologicheskaya klinika_etPl
27 Oct 25 at 8:34 pm
Hi there, constantly i used to check website posts
here in the early hours in the daylight, for the reason that i like to
find out more and more.
GPT +9A Plus
27 Oct 25 at 8:35 pm
Quality posts is the secret to be a focus for the users to pay a visit the site,
that’s what this website is providing.
tax evasion company
27 Oct 25 at 8:38 pm
ремонт подвала в частном доме [url=gidroizolyaciya-cena-7.ru]gidroizolyaciya-cena-7.ru[/url] .
gidroizolyaciya cena_dcSi
27 Oct 25 at 8:38 pm
кракен даркнет
кракен vpn
Henryamerb
27 Oct 25 at 8:38 pm
платный наркологический стационар [url=https://narkologicheskaya-klinika-25.ru/]narkologicheskaya-klinika-25.ru[/url] .
narkologicheskaya klinika_ziPl
27 Oct 25 at 8:40 pm
Hello there! This post could not be written any
better! Reading this post reminds me of my previous room mate!
He always kept talking about this. I will forward this article to
him. Pretty sure he will have a good read.
Thanks for sharing!
pabipemkabluwutimur.org
27 Oct 25 at 8:40 pm
диплом мед колледжа купить москва [url=www.frei-diplom10.ru]www.frei-diplom10.ru[/url] .
Diplomi_apEa
27 Oct 25 at 8:40 pm
лечение зависимостей в москве [url=www.narkologicheskaya-klinika-25.ru/]www.narkologicheskaya-klinika-25.ru/[/url] .
narkologicheskaya klinika_naPl
27 Oct 25 at 8:44 pm
kraken
kraken
Henryamerb
27 Oct 25 at 8:45 pm
https://t.me/bs_1Win/509
Georgerah
27 Oct 25 at 8:47 pm
https://t.me/bs_1Win/1035
Georgerah
27 Oct 25 at 8:50 pm
Для наглядности приведена таблица, отражающая распространённые методы лечения и их цели:
Подробнее тут – [url=https://narkologicheskaya-clinika-v-samare16.ru/]наркологическая клиника лечение алкоголизма в самаре[/url]
ThomasMew
27 Oct 25 at 8:52 pm
conorjmurphy – Excellent resource, practical guides make home projects feel stress-free always.
Allen Bidstrup
27 Oct 25 at 8:52 pm
кракен обмен
kraken зеркало
Henryamerb
27 Oct 25 at 8:53 pm
наркологическая частная клиника [url=http://narkologicheskaya-klinika-25.ru]http://narkologicheskaya-klinika-25.ru[/url] .
narkologicheskaya klinika_ldPl
27 Oct 25 at 8:53 pm
Kamagra Oral Jelly Deutschland [url=https://vitalpharma24.shop/#]vitalpharma24[/url] Kamagra Oral Jelly Deutschland
MichaelRen
27 Oct 25 at 8:53 pm
кракен маркетплейс
kraken
Henryamerb
27 Oct 25 at 8:54 pm
виртуальный номер для смс
MiguelActic
27 Oct 25 at 8:56 pm
наркология лечение [url=www.narkologicheskaya-klinika-27.ru/]наркология лечение[/url] .
narkologicheskaya klinika_ezpl
27 Oct 25 at 8:56 pm
аренда номера для смс
MiguelActic
27 Oct 25 at 8:57 pm
You’re so interesting! I don’t believe I’ve read through anything like that before.
So wonderful to find someone with a few unique thoughts on this subject.
Seriously.. thank you for starting this up. This web site is something that is required on the web, someone with some originality!
our website
27 Oct 25 at 8:59 pm
kraken сайт
kraken vpn
Henryamerb
27 Oct 25 at 8:59 pm
Простая подготовка уменьшает стресс, повышает переносимость капельницы и делает оценку состояния более точной. Попросите одного ответственного взрослого быть на связи с врачом — так решения будут приниматься быстрее, а ночной интервал пройдёт спокойнее и предсказуемее.
Выяснить больше – http://vyvod-iz-zapoya-v-chelyabinske16.ru
MichaelPix
27 Oct 25 at 9:00 pm
прием смс онлайн купить
MiguelActic
27 Oct 25 at 9:00 pm
анонимная наркологическая помощь в москве [url=https://narkologicheskaya-klinika-25.ru/]анонимная наркологическая помощь в москве[/url] .
narkologicheskaya klinika_olPl
27 Oct 25 at 9:00 pm