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=www.rudik-diplom11.ru]купить свидетельство о браке[/url] .
Diplomi_aoMi
25 Oct 25 at 4:05 pm
Hi there, yeah this piece of writing is truly pleasant and I have learned lot of things from it about blogging. thanks.
http://respectstroy.com.ua/yak-pravylno-vybraty-bi-led-moduli-dlya-vashoyi-av.html
LhaneDrync
25 Oct 25 at 4:07 pm
мостбет. [url=https://www.mostbet12031.ru]https://www.mostbet12031.ru[/url]
mostbet_kg_drMa
25 Oct 25 at 4:09 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra41c.cc]kra41 at[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra42-at.com]kra40 сс[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra36 cc
https://kra—41at.ru
KeithCrima
25 Oct 25 at 4:10 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra-41cc.net]kra38 cc[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra-42cc.net]kra41 сс[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra41 at
https://kra–41-cc.ru
Jamessew
25 Oct 25 at 4:11 pm
купить диплом с реестром отзывы [url=https://frei-diplom2.ru]купить диплом с реестром отзывы[/url] .
Diplomi_axEa
25 Oct 25 at 4:11 pm
Как получить приветственный бонус: краткая инструкция по регистрации и пополнению счёта, с указанием требований по отыгрышу; в середине параграфа мы указываем ссылку на 1xBet промокод при регистрации, для того чтобы пользователь мог перейти и узнать подробности. Помимо этого приводим примеры доступных типов акций.
PatrickDrymn
25 Oct 25 at 4:11 pm
Thanks for finally talking about > PHP hook, building hooks in your application –
Sjoerd Maessen blog at Sjoerd Maessen blog toko bunga Kendal
toko bunga Kendal
25 Oct 25 at 4:11 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra41-cc.com]kra40 at[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra-42-at.com]kra39 at[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
[url=https://kpa42.cc]kra41 cc[/url]
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
[url=https://kra-41–at.ru]kra42 cc[/url]
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra40 cc
https://kra41-at.com
Ronniefluem
25 Oct 25 at 4:12 pm
1xbet turkey [url=www.1xbet-12.com/]www.1xbet-12.com/[/url] .
1xbet_iwSr
25 Oct 25 at 4:14 pm
E2bet là nhà cái có kho game phong phú và đa dạng, bao gồm casino, thể thao, đá gà,… Bạn sẽ có những trải nghiệm
tuyệt vời nhất tại nhà cái khi được bảo mật
E2bet - Nhà Cái Đá Gà Thomo Hàng Đầu
25 Oct 25 at 4:14 pm
Ukrainian President Volodymyr Zelensky condemned Russian attacks on the Ukrainian regions of Kharkiv, Zaporizhzhia and Sumy on Monday, saying that the Kremlin intends to “humiliate diplomatic efforts” just hours before European leaders visit the White House.
[url=https://kra–42-cc.ru]kra40 сс[/url]
“The Russian war machine continues to destroy lives despite everything,” Zelensky said in a statement, hours before he’s due to meet US President Donald Trump in the Oval Office. “That is precisely why we are seeking assistance to put an end to the killings. That is why reliable security guarantees are required. That is why Russia should not be rewarded for its participation in this war.”
[url=https://kra42-at.net]kra40 cc[/url]
“Everyone seeks dignified peace and true security,” the Ukrainian president said. “And at this very moment, the Russians are attacking Kharkiv, Zaporizhzhia, the Sumy region, and Odesa, destroying residential buildings and our civilian infrastructure.”
At least seven people were killed in Russia’s attack? on Kharkiv and a further three killed in the ballistic missile strike on the city of Zaporizhzhia, with scores more injured, according to Ukrainian authorities.
“This was a demonstrative and cynical Russian strike,” Zelensky added.
kra40 at
https://kra—41-cc.ru
Kennethtip
25 Oct 25 at 4:16 pm
https://esocialmall.com/story5919384/code-promo-1xbet-paris-gratuit
rxitmxc
25 Oct 25 at 4:16 pm
Winpro129
adalah situs slot online terpercaya 2025 yang menawarkan pengalaman bermain interaktif, fitur terbaru, dan konten seru setiap hari.
Winpro129
25 Oct 25 at 4:16 pm
Unquestionably consider that that you said. Your favourite
reason appeared to be at the internet the easiest factor to consider of.
I say to you, I certainly get irked even as people consider concerns that they
just do not recognise about. You managed to hit the nail upon the top
and also outlined out the entire thing without having side-effects , other folks can take a signal.
Will likely be again to get more. Thank you
Avenir Bitrevo
25 Oct 25 at 4:17 pm
диплом купить с проводкой [url=https://frei-diplom2.ru/]диплом купить с проводкой[/url] .
Diplomi_bwEa
25 Oct 25 at 4:18 pm
Hurrah! In the end I got a blog from where I be able to truly obtain useful data
regarding my study and knowledge.
jihad group website
25 Oct 25 at 4:19 pm
мостбет мобильная версия скачать [url=https://mostbet12032.ru/]https://mostbet12032.ru/[/url]
mostbet_kg_lqmt
25 Oct 25 at 4:20 pm
geteventclipboard.com – I like how the functions are clearly outlined; it’s helpful for understanding the service.
Dania Vantull
25 Oct 25 at 4:20 pm
1xbet yeni adresi [url=https://1xbet-16.com/]1xbet yeni adresi[/url] .
1xbet_gqOn
25 Oct 25 at 4:22 pm
кракен маркет
кракен вход
JamesDaync
25 Oct 25 at 4:22 pm
1x lite [url=http://www.1xbet-15.com]1x lite[/url] .
1xbet_rrpl
25 Oct 25 at 4:23 pm
Цены на выведение тараканов выросли? Обсудим.
обработка от плесени в ванной
KennethceM
25 Oct 25 at 4:23 pm
что будет если купить диплом о высшем образовании с занесением в реестр [url=https://frei-diplom6.ru]что будет если купить диплом о высшем образовании с занесением в реестр[/url] .
Diplomi_ncOl
25 Oct 25 at 4:24 pm
Howdy are using WordPress for your blog platform?
I’m new to the blog world but I’m trying
to get started and set up my own. Do you need any coding expertise
to make your own blog? Any help would be greatly appreciated!
Zeker Fundiq
25 Oct 25 at 4:24 pm
birxbet [url=http://1xbet-10.com/]http://1xbet-10.com/[/url] .
1xbet_tpea
25 Oct 25 at 4:25 pm
долго гуляя по форуму, выбирал хороший магазин для себя. хотелось чтоб устраевало все! прежде всего меня интересовало качество товара и его цена! хотелось чтоб цена была доступной! так же есть большое желание всегда получать товар 100% т.к. в закладочных магазинах бывают случаи не находа, такие магазины для постоянных покупок рассматривать не стал! рассматривалось много вариантов! ну гдегде же всетаки заказать??? то предлогают сразу слишком много товара (ну как же брать если ты сам лично не знаешь за его качество)??? бывало чаще всего не устраевал сервис обслуживания! https://promo2025magnit.ru Молча !! Запрет вступает в силу 26го числа, если не ошибаюсь. Скидывай всё палево, жёсткий дист зарывай подальше и иди получай ))
LeonardHOX
25 Oct 25 at 4:28 pm
Jogue o caca-niqueis slot com egipcios: tumbas antigas, riscos e giros gratis com simbolos expansivos. Modo de demonstracao sem cadastro, RTP justo, simbolos Wild/Scatter, multiplicadores e rodadas bonus. Otimizado para dispositivos moveis, depositos e saques praticos, dicas para iniciantes.
jogolegacyofdead 137
25 Oct 25 at 4:28 pm
Любите классику? слот фрукты с семёрками, BAR и цитрусами дарит частые хиты, Wild, Scatter и бонусные вращения. Демо-режим бесплатно, честный RTP, адаптация под смартфоны. Простые правила и динамичный геймплей — отличный выбор для новичков и профи. Помните о разумных лимитах.
onewinner 517
25 Oct 25 at 4:29 pm
kraken market Kraken ссылка – это навигатор по бескрайним просторам даркнета, указывающий путь к заветной цели. Однако, доверять первой попавшейся ссылке равносильно прыжку в пропасть с завязанными глазами. Прежде чем сделать этот шаг, убедитесь в её подлинности, проверьте репутацию источника и подготовьтесь к возможным неожиданностям. Безопасность превыше всего, и только внимательность и осторожность помогут вам избежать неприятных сюрпризов.
Kevinmoots
25 Oct 25 at 4:30 pm
O caca-niqueis Caca-niquel Rio Gems combina a nostalgia classica com o dinamismo de um parque de diversoes: multiplicadores, giros gratis, re-spins e Wilds aleatorios. Apresenta graficos coloridos, uma interface responsiva e uma versao demo para praticar. Ao jogar, lembre-se de se manter seguro: estabeleca limites e faca pausas.
jogoriogem 744
25 Oct 25 at 4:30 pm
Explore Riyo Gems, a renowned 925 sterling silver jewelry producer in Jaipur, India.
We offer eco-friendly handmade gemstone jewelry, tailor-made collections,
and wholesale gemstone supply. Collaborate with our reliable factory for finest silver, semi-precious, and
loose gemstone jewelry production.
Silver Gemstones Jewelry
25 Oct 25 at 4:31 pm
I delight in, lead to I found just what I was having a look for.
You’ve ended my four day long hunt! God Bless you man. Have a great day.
Bye
dewascatter
25 Oct 25 at 4:31 pm
https://unsplash.com/@freespins65
ElbertFer
25 Oct 25 at 4:33 pm
Try elite entertainment at the Playamo casino with more than 3,000 premium slot games, classic games, and real dealer options from leading software developers. From the cutting-edge video slots to skillful blackjack play and engaging live dealer action, the site meets diverse player type. Boasting a sophisticated, easy-to-use system, the portal guarantees seamless access via all devices, permitting you experience beloved options at your convenience.
Playamo
AlfredLog
25 Oct 25 at 4:34 pm
1xbet t?rkiye [url=http://1xbet-13.com]1xbet t?rkiye[/url] .
1xbet_vwKa
25 Oct 25 at 4:34 pm
When I initially commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get several emails with the same comment.
Is there any way you can remove me from that service?
Thanks a lot!
black car to airport
25 Oct 25 at 4:36 pm
mostbet kyrgyzstan [url=http://mostbet12031.ru]mostbet kyrgyzstan[/url]
mostbet_kg_ncMa
25 Oct 25 at 4:37 pm
1xbet t?rkiye giri? [url=http://1xbet-12.com]1xbet t?rkiye giri?[/url] .
1xbet_gsSr
25 Oct 25 at 4:37 pm
мостбет вход в личный кабинет [url=https://mostbet12032.ru]https://mostbet12032.ru[/url]
mostbet_kg_tgmt
25 Oct 25 at 4:37 pm
This game looks amazing! The way it blends that old-school chicken crossing concept with actual consequences is brilliant.
Count me in!
Okay, this sounds incredibly fun! Taking that nostalgic chicken crossing gameplay and adding
real risk? I’m totally down to try it.
This is right up my alley! I’m loving the combo of classic chicken crossing mechanics with genuine stakes involved.
Definitely want to check it out!
Whoa, this game seems awesome! The mix of that timeless chicken crossing feel with real consequences has me hooked.
I need to play this!
This sounds like a blast! Combining that iconic chicken crossing gameplay
with actual stakes? Sign me up!
I’m so into this concept! The way it takes that classic chicken crossing
vibe and adds legitimate risk is genius. Really want to give it
a go!
This game sounds ridiculously fun! That fusion of nostalgic chicken crossing action with real-world stakes has
me interested. I’m ready to jump in!
Holy cow, this looks great! Merging that beloved chicken crossing style with tangible
consequences? I’ve gotta try this out!
spin mama casino welcome bonus
25 Oct 25 at 4:45 pm
1xbet ?yelik [url=www.1xbet-16.com/]1xbet ?yelik[/url] .
1xbet_xcOn
25 Oct 25 at 4:45 pm
mostbet kg [url=https://mostbet12031.ru/]https://mostbet12031.ru/[/url]
mostbet_kg_iwMa
25 Oct 25 at 4:46 pm
Результат после дезинсекция потрясающий!
дезинфекция после чесотки
KennethceM
25 Oct 25 at 4:47 pm
So if you’re feeling critical in your search that how
one can get rid of man boobs then it is best to begin using Gynexin since this will
definitely definitely assist you to get rid of man boobs.
The meta- bolically active visceral tissues (e.g.
magnum 0.50 viagra
25 Oct 25 at 4:47 pm
кракен qr код
кракен android
JamesDaync
25 Oct 25 at 4:49 pm
После уничтожение насекомых мыши пропали, рад!
санэпидемстанция отзывы
KennethceM
25 Oct 25 at 4:50 pm
Борьба с обработка от клопов в отеле удалась, фирма молодцы.
выведение клопов
KennethceM
25 Oct 25 at 4:52 pm
1xbet g?ncel giri? [url=http://1xbet-15.com]1xbet g?ncel giri?[/url] .
1xbet_ykpl
25 Oct 25 at 4:54 pm
What’s up to every , because I am really keen of reading this weblog’s post to be
updated daily. It consists of pleasant material.
đăng nhập kkwin
25 Oct 25 at 4:55 pm