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!
The Minotaurus presale vesting program is a game-changer for early birds. Extend for bonuses and avoid FOMO on post-TGE pumps. $MTAUR could be the dark horse in blockchain games.
minotaurus ico
WilliamPargy
22 Oct 25 at 9:25 pm
https://t.me/s/PlayCasino_1win
https://t.me/s/PlayCasino_1xbet
22 Oct 25 at 9:26 pm
https://t.me/s/PlayCasino_1win
https://t.me/s/PlayCasino_1win
22 Oct 25 at 9:27 pm
https://www.medtronik.ru официальный источник актуальных бонусных предложений
Aaronawads
22 Oct 25 at 9:27 pm
юридический перевод на английский [url=http://www.teletype.in/@alexd78/HN462R01hzy]http://www.teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_uest
22 Oct 25 at 9:28 pm
I’ve been browsing online more than three hours today, yet I never found
any interesting article like yours. It’s pretty worth enough for me.
In my opinion, if all site owners and bloggers made good
content as you did, the net will be a lot more useful than ever before.
https://888newport.com/
22 Oct 25 at 9:28 pm
Snagged more $MTAUR; referrals pay. Presale’s value jumps. Minotaur customizable.
minotaurus ico
WilliamPargy
22 Oct 25 at 9:28 pm
J’ai une affection pour Impressario Casino, ca transporte dans un monde gourmand. La variete des titres est raffinee, avec des slots aux designs elegants. Renforcant votre capital initial. Le service est disponible 24/7, joignable a toute heure. Les transactions sont fiables, cependant des recompenses additionnelles seraient royales. Dans l’ensemble, Impressario Casino garantit un plaisir constant pour ceux qui aiment parier en crypto ! A noter la navigation est simple et gracieuse, ajoute une touche d’elegance. Un plus les paiements securises en crypto, offre des recompenses continues.
Explorer maintenant|
VersaillesVibeR6zef
22 Oct 25 at 9:30 pm
hardman4schools – Design supports purpose perfectly, showcasing leadership and care for education.
Shon Baculpo
22 Oct 25 at 9:30 pm
Je suis epoustoufle par Monte Cryptos Casino, ca procure un frisson virtuel unique. Le choix de titres est eblouissant, incluant des paris en direct dynamiques. Boostant votre portefeuille initial. Le suivi est d’une efficacite redoutable, toujours pret a resoudre. Les retraits sont fulgurants, neanmoins des bonus plus varies seraient un atout. En resume, Monte Cryptos Casino garantit un plaisir constant pour ceux qui parient avec des cryptos ! Par ailleurs le design est elegant et intuitif, ce qui rend chaque session plus immersive. Un atout cle les tournois reguliers pour la competition, renforce la communaute.
Naviguer sur le site|
QuantumRiserB7zef
22 Oct 25 at 9:30 pm
Если вы ищете надежную клинику для вывода из запоя в Сочи, обратитесь в «Детокс». Здесь опытные специалисты окажут необходимую помощь в стационаре. Услуга доступна круглосуточно, анонимно и начинается от 2000 ?.
Узнать больше – [url=https://vyvod-iz-zapoya-sochi22.ru/]вывод из запоя цена в сочи[/url]
Jamesdug
22 Oct 25 at 9:32 pm
В обзоре возможностей для игроков мы кратко разбираем типы бонусов, включая фрибеты и депозитные поощрения; в соответствующем абзаце приведена ссылка на https://bergkompressor.ru/news/artcles/?1xbet_promokod_pri_registracii_bonus_5.html, который может и помочь новым пользователям при регистрации. Кроме того, даём советы по безопасности и ответственному подходу к ставкам.
Anthonykit
22 Oct 25 at 9:33 pm
Когда нужна срочная помощь, служба Stop-Alko в Екатеринбурге может выехать на дом, провести диагностику, капельницу и восстановление сил.
Получить больше информации – [url=https://vyvod-iz-zapoya-ekaterinburg26.ru/]врач вывод из запоя в екатеринбурге[/url]
ClydeLak
22 Oct 25 at 9:35 pm
I visited several web pages except the audio quality for audio songs existing at this web site is truly
marvelous.
Los Angeles AC installation
22 Oct 25 at 9:36 pm
рейтинг digital seo агентств [url=https://www.reiting-seo-kompaniy.ru]рейтинг digital seo агентств[/url] .
reiting seo kompanii_voon
22 Oct 25 at 9:37 pm
Капельница от запоя в Нижнем Новгороде — процедура, направленная на детоксикацию организма и восстановление нормального самочувствия. Она включает в себя введение препаратов, способствующих выведению токсинов и восстановлению функций органов.
Подробнее – [url=https://vyvod-iz-zapoya-nizhnij-novgorod13.ru/]вывод из запоя на дому недорого нижний новгород[/url]
DarrellUnsug
22 Oct 25 at 9:37 pm
купить диплом техникума услуга [url=www.frei-diplom7.ru]купить диплом техникума услуга[/url] .
Diplomi_gzei
22 Oct 25 at 9:41 pm
How to win in Calgary Lottery: Boost your chances by playing consistently, joining lottery pools, and choosing less popular combinations. Remember, winning requires luck and responsible play: how to win in Calgary lottery
GabrielLyday
22 Oct 25 at 9:41 pm
This is very interesting, You are a very skilled blogger.
I’ve joined your rss feed and look forward to
seeking more of your fantastic post. Also, I have shared your web site in my social networks!
Chandni Earring
22 Oct 25 at 9:41 pm
медицинский перевод на английский [url=https://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16]https://www.telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_esEr
22 Oct 25 at 9:41 pm
день захисника україни 2025 сценарій
JessieVinge
22 Oct 25 at 9:42 pm
В условиях клиники пациент находится под наблюдением медицинской сестры и врача 24/7, что особенно важно при тяжёлой интоксикации и риске острых осложнений. Быстрый доступ к расширенной диагностике — ЭКГ, УЗИ, анализы крови — обеспечивает точную корректировку терапии. Стационар подходит тем, у кого есть серьёзные сопутствующие заболевания или высокий риск алкогольных психозов.
Разобраться лучше – http://алко-избавление.рф/narkolog-vyvod-iz-zapoya-msk/
TrevorSah
22 Oct 25 at 9:43 pm
Лечение основывается на комплексном подходе, позволяющем учесть особенности организма и стаж злоупотребления алкоголем. Обычно процедура состоит из нескольких этапов:
Ознакомиться с деталями – [url=https://narko-reabcentr.ru/]анонимный вывод из запоя[/url]
JamesSworp
22 Oct 25 at 9:44 pm
особенности медицинского перевода [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/]telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_zgEr
22 Oct 25 at 9:45 pm
технический перевод в машиностроении [url=www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_dkml
22 Oct 25 at 9:45 pm
медицинский перевод на английский [url=teletype.in/@alexd78/HN462R01hzy]teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_hdst
22 Oct 25 at 9:45 pm
купить диплом физика [url=http://rudik-diplom7.ru]купить диплом физика[/url] .
Diplomi_qiPl
22 Oct 25 at 9:45 pm
thinkcreategrow.shop – Every article brings a new perspective, I genuinely enjoy visiting daily.
Ethel Karstensen
22 Oct 25 at 9:46 pm
veste trapstar
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
veste trapstar
22 Oct 25 at 9:47 pm
виды технического перевода [url=dzen.ru/a/aPFFa3ZMdGVq1wVQ]dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_hcml
22 Oct 25 at 9:48 pm
Нарколог рекомендует кодирование при наличии следующих признаков:
Разобраться лучше – [url=https://kodirovanie-ot-alkogolizma-pushkino4.ru/]kodirovanie ot alkogolizma[/url]
Judsonsak
22 Oct 25 at 9:51 pm
Услуга вывода из запоя от Stop-Alko в Екатеринбурге доступна в любое время суток — помощь приходит быстро.
Получить дополнительную информацию – [url=https://vyvod-iz-zapoya-ekaterinburg27.ru/]наркология вывод из запоя[/url]
RichardDaB
22 Oct 25 at 9:51 pm
staycuriousalways.shop – Timely delivery, received my order in perfect condition.
Carmina Delanoche
22 Oct 25 at 9:51 pm
трансы Новосибириск Локальные Сообщества: Поддержка и Информация Эти каналы служат важным ресурсом для транс-сообщества. Здесь можно найти информацию о врачах, психологах, юристах и других специалистах, оказывающих поддержку транс-персонам. Каналы помогают в поиске дружелюбных мест, где можно безопасно и комфортно проводить время. Они также предоставляют платформу для обмена опытом, обсуждения проблем и просто для общения с людьми, которые понимают.
ChrisVOisa
22 Oct 25 at 9:53 pm
Boost your play with the most trusted https://zenwriting.net/4njox9hxt2. Take advantage of free bets, cashback, and extra spins for a perfect start.
Justincit
22 Oct 25 at 9:54 pm
Наши врачи оперативно приезжают к пациенту, проводят осмотр, купируют симптомы абстиненции и приступают к восстановлению жизненно важных функций. Всё это — в привычной для пациента обстановке, без стресса и лишней огласки. Мы обеспечиваем не только медицинскую помощь, но и эмоциональную поддержку, создавая условия для начала пути к выздоровлению.
Ознакомиться с деталями – [url=https://narcolog-na-dom-v-moskve55.ru/]нарколог капельница на дом красногорск[/url]
Michaelwop
22 Oct 25 at 9:56 pm
Вызов нарколога на дом в Краснодаре — услуга клиники «Детокс». Специалисты оказывают квалифицированную помощь прямо у вас дома.
Получить больше информации – [url=https://narkolog-na-dom-krasnodar25.ru/]вызов нарколога на дом краснодар[/url]
AndrewENlit
22 Oct 25 at 9:56 pm
For latest information you have to pay a quick visit
web and on world-wide-web I found this site as a finest web site for
latest updates.
buôn bán nội tạng
22 Oct 25 at 9:56 pm
медицинский перевод на английский [url=https://telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16/]telegra.ph/Medicinskij-perevod-tochnost-kak-vopros-zhizni-i-zdorovya-10-16[/url] .
Medicinskii perevod_uxEr
22 Oct 25 at 9:56 pm
перевод технической документации [url=https://www.dzen.ru/a/aPFFa3ZMdGVq1wVQ]https://www.dzen.ru/a/aPFFa3ZMdGVq1wVQ[/url] .
Tehnicheskii perevod_ffml
22 Oct 25 at 9:59 pm
Стационар «Частного Медика 24» помогает безопасно и быстро восстановиться после длительного запоя.
Углубиться в тему – [url=https://vyvod-iz-zapoya-v-stacionare-voronezh23.ru/]вывод из запоя в стационаре воронеж[/url]
Charlesomido
22 Oct 25 at 9:59 pm
Если вы ищете надежную клинику для вывода из запоя в Сочи, обратитесь в «Детокс». Здесь опытные специалисты окажут необходимую помощь в стационаре. Услуга доступна круглосуточно, анонимно и начинается от 2000 ?.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-sochi24.ru/]вывод из запоя вызов город[/url]
ClintonEmpib
22 Oct 25 at 10:00 pm
Центр отдыха Термбург — это локация, где можно восстановиться телом и умом. Всё продумано для релакса и заботы о теле: современные бани и сауны, современные бассейны, термы и спа-зона с профессиональными массажистами. Атмосфера спокойствия и уюта помогает забыть о суете и почувствовать лёгкость уже после одного сеанса.
Особого внимания заслуживает [url=https://termburg.ru/swimming-school/]обучение плаванию детей в москве[/url] — это не просто эстетический ритуал, а способ восстановить баланс кожи, активировать кровоток и подарить коже свежесть и упругость. Опытные мастера подбирают техники индивидуально — от классического до тканевого массажа. Результат ощущается сразу — лёгкость, свежесть и ровный тон кожи.
Для тех, кто выбирает спорт и активность, работает [url=https://termburg.ru/price-list/]термы[/url] и массаж спины и шеи. Занятия проходят под руководством опытных тренеров, программы подходят для взрослых и детей. Здесь помогают освоить плавание с нуля, укрепляют мышцы и дарят удовольствие от каждого движения. Баланс тела и разума — всё, что нужно, чтобы восстановить силы и энергию.
Leviomaza
22 Oct 25 at 10:01 pm
купить диплом техникума [url=http://www.rudik-diplom7.ru]купить диплом техникума[/url] .
Diplomi_atPl
22 Oct 25 at 10:01 pm
discovernewworld.shop – User-friendly website, easy to navigate and find desired items.
Esteban Crumpler
22 Oct 25 at 10:02 pm
перевод английской научно технической литературы [url=www.teletype.in/@alexd78/HN462R01hzy]www.teletype.in/@alexd78/HN462R01hzy[/url] .
Vidi perevodov v buro Perevod i Pravo_test
22 Oct 25 at 10:04 pm
Адекватное лечение, комфорт и забота — так проходят дни в стационаре «Частного Медика 24» во время вывода из запоя.
Узнать больше – [url=https://vyvod-iz-zapoya-v-stacionare-samara23.ru/]наркология вывод из запоя в стационаре самара[/url]
DannyPowly
22 Oct 25 at 10:04 pm
After going over a number of the articles on your web site,
I truly like your way of blogging. I book marked it to my bookmark
site list and will be checking back in the near future.
Please visit my web site too and tell me your opinion.
Aluminium Drilling Tapping Cutting Machine
22 Oct 25 at 10:04 pm
Hi, yup this post is genuinely fastidious and I have learned lot of things from
it regarding blogging. thanks.
vapebrowsing
22 Oct 25 at 10:05 pm
Alas, don’t downplay hor, leading institutions build cultural knowledge, key
fօr worldwide relations roles.
Parents, kiasu mode fսll lah, elite institutions prepare f᧐r country-wide
assessments, guaranteeing seamless ⅽhanges to secs.
Օh no, primary math instructs practical implementations including budgeting, tһᥙѕ guarantee үοur kid grasps it rіght from
yοung age.
Ⲟh, arithmetic іs the groundwork block in primary education, helping youngsters
fоr spatial thinking fоr architecture paths.
Ɗon’t mess around lah, pair a excellent primary school alongside mathematics proficiency іn oгder tօ ensure high PSLE scores аѕ
well as smooth transitions.
Wah lao, no matter іf establishment іs һigh-end, mathematics acts ⅼike the make-or-break
discipline іn cultivates assurance іn numbеrs.
Parents, kiasu mode оn lah, solid primary math leads tߋ superior science grasp аnd tech dreams.
Ngee Ann Primary School սseѕ an іnteresting space for academic аnd individual development.
Committed personnel support confident аnd capable learners.
Jurong Primary School оffers quality education іn the west.
Tһe school develops strong structures f᧐r success.
It’s a dependable choice fоr local households.
mʏ web pagе – Junyuan Secondary School
Junyuan Secondary School
22 Oct 25 at 10:05 pm