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!
Mums and Dads, fearful օf losing style activated lah, strong primary mathematics leads tⲟ superior science
comprehension рlus engineering aspirations.
Oh, maths serves aѕ the base block in primary learning, helping kids ᴡith spatial thinking for design careers.
Eunoia Junior College represents modern innovation іn education,
with its high-rise campus incorporating community spaces
fоr collaborative learning аnd development.
Ƭhe college’s emphasis оn lovely thinking promotes intellectual inteгest аnd goodwill, supported ƅy
vibrant programs in arts, sciences, аnd management.
Cutting edge centers, consisting օf carrying out arts venues, enable students to check out
passions and establish talents holistically. Partnerships ᴡith
ԝell-regarded organizations provide improving chances fօr research аnd global exposure.
Students emerge аs thoughtful leaders, ɑll set tо contribute
favorably tⲟ a varied worlԁ.
Eunoia Junior College embodies tһe pinnacle of contemporary educational innovation, housed іn a striking high-rise school tһat
perfectly incorporates communal knowing ɑreas, green locations, and advanced technological hubs t᧐ develop аn
motivating atmosphere fօr collaborative аnd experiential education. Τhe college’s distinct viewpoint оf ” lovely thinking” motivates students
tо blend intellectual іnterest with compassion аnd ethical reasoning,supported Ьy dynamic academic programs
іn the arts, sciences, and interdisciplinary research studies thаt promote innovative analytical ɑnd forward-thinking.
Equipped ԝith toρ-tier facilities ѕuch аѕ professional-grade performing arts theaters, multimedia studios,
аnd interactive science labs, students аre empowered tօ pursue their enthusiasms аnd establish remarkable skills іn a holistic
manner. Thгough strategic partnerships ᴡith leading universities аnd industry leaders, tһe college pгovides improving opportunities fߋr undergraduate-level
гesearch study, internships, аnd mentorship tһat bridge classroom
knowing with real-ԝorld applications. Аѕ а result, Eunoia Junior College’ѕ trainees progress іnto thoughtful, durable
leaders ԝho are not ϳust academically accomplished Ƅut ⅼikewise deeply devoted
to contributing favorably t᧐ a varied аnd eѵer-evolving worldwide society.
Beѕides from school amenities, focus оn math for prevent
common mistakes ⅼike inattentive errors in assessments.
Folks, kiasu approach engaged lah, strong primary maths guides
fⲟr improved scientific grasp ρlus engineering dreams.
Ɗon’t mess around lah, pair ɑ excellent Junior College with mathematics excellence
t᧐ assure elevated A Levels scores plus effortless ϲhanges.
Oh dear, minus robust math during Junior College,
even top school children ϲould struggle ѡith high school
calculations, ѕο cultivate tһis promptly leh.
Kiasu study marathons fߋr Math pay οff with university acceptances.
Ᏼesides to school amenities, concentrate սpon maths to prevent typical pitfalls ⅼike inattentive errors Ԁuring assessments.
Mums аnd Dads, competitive approach activated lah, strong primary
maths leads fߋr superior science comprehension and tech goals.
Feel free t᧐ surf t᧐ mү web site :: math tuition bugis
math tuition bugis
28 Oct 25 at 10:00 pm
мелбет официальный [url=http://www.melbetofficialsite.ru]мелбет официальный[/url] .
bk melbet_myEa
28 Oct 25 at 10:00 pm
Важно отметить, что для входа в аккаунт Gama требуется стабильное интернет-соединение и наличие учетной записи.
гама официальный сайт казино зеркало
28 Oct 25 at 10:01 pm
kraken android
кракен официальный сайт
Henryamerb
28 Oct 25 at 10:02 pm
частный seo оптимизатор [url=www.optimizaciya-i-seo-prodvizhenie-sajtov-moskva-1.ru]частный seo оптимизатор[/url] .
optimizaciya i seo prodvijenie saitov moskva_vjPi
28 Oct 25 at 10:02 pm
Наша выездная бригада приезжает без маркировки, в гражданской одежде, согласовывает «тихое окно» связи с пациентом или близкими и начинает не с капельницы, а с настройки среды: тёплая приглушённая подсветка, проветривание без сквозняков, отключение уведомлений, подготовка воды комнатной температуры. Затем — быстрый осмотр, экспресс-метрики и старт индивидуального плана. Каждая корректировка — ровно одна за раз, чтобы сохранить причинно-следственную связь и не провоцировать полипрагмазию.
Узнать больше – [url=https://narkolog-na-dom-saratov0.ru/]нарколог на дом круглосуточно в саратове[/url]
Brucescuse
28 Oct 25 at 10:05 pm
cd playing clock radio [url=alarm-radio-clocks.com]alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_jlOa
28 Oct 25 at 10:07 pm
кракен маркет
кракен ссылка
Henryamerb
28 Oct 25 at 10:08 pm
Прошу прощения, что я Вас прерываю, мне тоже хотелось бы высказать своё мнение.
Instantly switch between {demo|demo} and real #file_links[“C:\Users\Admin\Desktop\file\gsa+en+100ksmdkteam5639990511URLBB.txt”,1,N]. It {has|has} {simple|understandable|uncomplicated} and {simple|intuitive|uncomplicated|elementary} {interface|search engine} created for {“seduction”|satisfaction|hoarding|seduction} {needs|demand|needs} {the most|most{widespread|popular|in-demand|famous|famous|eminent} {demanding|picky|moody} traders.
Alexles
28 Oct 25 at 10:08 pm
It’s amazing to visit this web site and reading the views of all colleagues
concerning this paragraph, while I am also zealous of getting experience.
Bluewall Revobit
28 Oct 25 at 10:10 pm
changan uni размеры https://changan-v-spb.ru
changan-154
28 Oct 25 at 10:10 pm
комплексное продвижение сайтов москва [url=https://optimizaciya-i-seo-prodvizhenie-sajtov-moskva-1.ru/]комплексное продвижение сайтов москва[/url] .
optimizaciya i seo prodvijenie saitov moskva_kuPi
28 Oct 25 at 10:11 pm
clock radio alarm clock cd player [url=www.alarm-radio-clocks.com/]www.alarm-radio-clocks.com/[/url] .
Cd Player Radio Alarm Clocks_pcOa
28 Oct 25 at 10:12 pm
changan cs75plus https://changan-v-spb.ru
changan-52
28 Oct 25 at 10:13 pm
https://t.me/s/Official_mellstroy_casino/58
Calvindreli
28 Oct 25 at 10:14 pm
changan 55 plus https://changan-v-spb.ru
changan-294
28 Oct 25 at 10:14 pm
alarm clock with usb music player [url=www.alarm-radio-clocks.com/]www.alarm-radio-clocks.com/[/url] .
Cd Player Radio Alarm Clocks_ftOa
28 Oct 25 at 10:16 pm
https://t.me/s/Official_mellstroy_casino/22
Calvindreli
28 Oct 25 at 10:16 pm
кракен тор
кракен vk2
Henryamerb
28 Oct 25 at 10:17 pm
кракен актуальная ссылка
кракен ссылка
Henryamerb
28 Oct 25 at 10:17 pm
обзор changan uni https://changan-v-spb.ru
changan-433
28 Oct 25 at 10:17 pm
Где купить Меф в Жукове?Обратите внимание сайт https://shkaf2.ru
– положительные отзывы и цены. Доставка в день заказа. Кто-то тестил их в деле?
Stevenref
28 Oct 25 at 10:18 pm
Отличный сайт! Всем рекомендую![url=https://dataslon.ru]Проверка контрагентов[/url]
DataslonRu3Nic
28 Oct 25 at 10:20 pm
Trong bối cảnh thị trường cá cược trực tuyến ngày càng cạnh tranh, GO99 đã nhanh chóng khẳng
định vị thế nhờ sự uy tín, minh bạch
và dịch vụ chuyên nghiệp. Với kho game đa dạng, tỷ lệ trả thưởng hấp dẫn cùng hệ thống giao dịch siêu tốc, GO99 trở thành
điểm đến lý tưởng cho cộng đồng bet thủ
tại Việt Nam. https://www.futureworks.uk.com/
go99
28 Oct 25 at 10:20 pm
Как получить приветственный бонус: пошаговая инструкция по регистрации и пополнению счёта, с указанием требований по отыгрышу; в середине параграфа мы указываем ссылку на https://www.apelsin.su/wp-includes/articles/promokod_240.html, для того чтобы пользователь мог перейти и узнать подробности. Помимо этого приводим примеры доступных типов акций.
EltonCep
28 Oct 25 at 10:21 pm
Извините, я удалил это сообщение
а поэтому мы подходим к предмету, касательно безопасности личных информации, [url=https://1win-q2f5n.top/]https://1win-q2f5n.top[/url] с максимальной ответственностью. так, другие не испускают к им доступа.
JeremiahNag
28 Oct 25 at 10:22 pm
кракен онион
кракен онион
Henryamerb
28 Oct 25 at 10:23 pm
продвижение сайтов во франции [url=https://optimizaciya-i-seo-prodvizhenie-sajtov-moskva-1.ru]продвижение сайтов во франции[/url] .
optimizaciya i seo prodvijenie saitov moskva_nlPi
28 Oct 25 at 10:24 pm
alarm clocks with cd players [url=https://alarm-radio-clocks.com/]https://alarm-radio-clocks.com/[/url] .
Cd Player Radio Alarm Clocks_srOa
28 Oct 25 at 10:24 pm
Terrific article! That is the kind of information that are meant to
be shared around the net. Shame on Google for not positioning
this publish upper! Come on over and visit my site .
Thank you =)
Gordon Law
28 Oct 25 at 10:27 pm
https://t.me/Official_mellstroy_casino/56
Calvindreli
28 Oct 25 at 10:27 pm
I’m really enjoying the design and layout of your website.
It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire
out a developer to create your theme? Exceptional work!
نمایندگی بکو در تهران
28 Oct 25 at 10:28 pm
I am regular visitor, how are you everybody? This piece of
writing posted at this web page is genuinely good.
web site
28 Oct 25 at 10:28 pm
https://t.me/s/Official_mellstroy_casino/38
Calvindreli
28 Oct 25 at 10:28 pm
Подробная пошаговая инструкция по активации бонусов и типам предложений — от фрибетов до подарков на День Рождения; в тексте естественно вставлена ссылка на https://www.apelsin.su/wp-includes/articles/promokod_240.html, дабы читатель мог быстро перейти к источнику. Материал также рассказывает о верификации и минимальных требованиях для вывода средств.
EltonCep
28 Oct 25 at 10:29 pm
Appreciating the commitment you put into your website and in depth information you present.
It’s nice to come across a blog every once in a while that isn’t
the same out of date rehashed information. Excellent read!
I’ve saved your site and I’m adding your RSS feeds to my Google account.
Buy Adderall 20mg Overnight Shipping
28 Oct 25 at 10:29 pm
Подробная инструкция по активации бонусов и типам предложений — от фрибетов до подарков на День Рождения; в тексте естественно вставлена ссылка на https://sushikim.ru/image/pgs/1xbet-besplatnuy-promokod-pri-registracii.html, чтобы читатель мог быстро перейти к источнику. Статья также рассказывает о верификации и минимальных требованиях для вывода средств.
FrankWeism
28 Oct 25 at 10:29 pm
kraken
kraken ссылка
Henryamerb
28 Oct 25 at 10:29 pm
Crowngreen is a leading gaming platform that provides thrilling games for players. Crowngreen Casino stands out in the online gaming world and has achieved trust among visitors.
Every user at https://dblanks.com/get-ready-for-crowngreen-casino-and-claim-daily/
has the chance to enjoy top-rated slots and take advantage of generous promotions. Crowngreen Casino provides secure gameplay, smooth transactions, and round-the-clock customer support for every player.
With , gamers discover a huge selection of slots, including live dealer options. The platform focuses on entertainment and guarantees a secure gaming environment.
Whether you are a beginner or a pro, Crowngreen Casino offers exciting gameplay for everyone. Sign up at Crowngreen Casino today and experience exciting games, generous bonuses, and a reliable gaming environment.
Crowngreen Warneratort
28 Oct 25 at 10:30 pm
alarm cd player [url=http://www.alarm-radio-clocks.com]http://www.alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_tjOa
28 Oct 25 at 10:30 pm
This package is our way of thanking you for your deposits. [url=https://ste-bmms.com/1xbet-thailand-download-app-your-ultimate-betting-20/]https://ste-bmms.com/1xbet-thailand-download-app-your-ultimate-betting-20/[/url], and the bonus amount increases with each deposit.
EllaStums
28 Oct 25 at 10:33 pm
wettbüro krefeld
Review my web-site – über Unter wette strategie
über Unter wette strategie
28 Oct 25 at 10:35 pm
купить диплом в великих луках [url=http://www.rudik-diplom6.ru]купить диплом в великих луках[/url] .
Diplomi_zpKr
28 Oct 25 at 10:35 pm
технического аудита сайта [url=https://optimizaciya-i-seo-prodvizhenie-sajtov-moskva-1.ru/]optimizaciya-i-seo-prodvizhenie-sajtov-moskva-1.ru[/url] .
optimizaciya i seo prodvijenie saitov moskva_fzPi
28 Oct 25 at 10:35 pm
купить диплом в стерлитамаке [url=www.rudik-diplom7.ru]купить диплом в стерлитамаке[/url] .
Diplomi_irPl
28 Oct 25 at 10:35 pm
При регистрации в БК Melbet игрокам предлагается самостоятельно выбрать стартовую награду: фрибет на $30 или 100% бонус на депозит до 7 000 RUB. Предлагаем Melbet промокод, благодаря которому вы получите не только повышенный до 50 000 RUB бонус на первое пополнение счета, но и право на бесплатный прогноз. Приветствовать новых клиентов бонусами стало хорошей традицией букмекерской конторы. На протяжении нескольких лет награда на депозит радовала сотни тысяч новичков и стремительно увеличивалась в размере. В 2026 году вознаграждение за пополнение счета достигло суммы в 7 000 российских рублей. Мы делаем более выгодное предложение. Игроки, которые введут в промокод мелбет при регистрации — RS777, не просто увеличат максимальный размер бонуса на депозит до 50 000 RUB, но и получат возможность воспользоваться фрибетом на 400 российских рублей.
Georgeduh
28 Oct 25 at 10:35 pm
tabletop cd player and radio [url=http://alarm-radio-clocks.com]http://alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_dhOa
28 Oct 25 at 10:36 pm
kraken darknet market
kraken вход
Henryamerb
28 Oct 25 at 10:37 pm
kraken darknet market
кракен ссылка
Henryamerb
28 Oct 25 at 10:38 pm
am fm cd clock radio [url=http://www.alarm-radio-clocks.com]http://www.alarm-radio-clocks.com[/url] .
Cd Player Radio Alarm Clocks_jeOa
28 Oct 25 at 10:39 pm