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!
How to setup claude mcp
WilliamJet
25 Oct 25 at 5:06 am
I think this is among the most important info for me.
And i’m glad reading your article. But should remark on few general
things, The website style is wonderful, the articles is
really excellent : D. Good job, cheers
Earn Matrix Pro
25 Oct 25 at 5:07 am
tutorial wordpress mcp
WilliamJet
25 Oct 25 at 5:07 am
купить диплом в коврове [url=http://rudik-diplom14.ru/]купить диплом в коврове[/url] .
Diplomi_llea
25 Oct 25 at 5:09 am
Hello There. I found your weblog the usage of msn. That is an extremely neatly written article.
I’ll be sure to bookmark it and return to read extra of your useful information. Thank you for
the post. I’ll definitely comeback.
Centre Finrivo
25 Oct 25 at 5:10 am
1x bet [url=https://1xbet-7.com/]https://1xbet-7.com/[/url] .
1xbet_aool
25 Oct 25 at 5:11 am
moeinclub.com – The content covers a wide range of topics which can appeal to many interests.
Jed Owenby
25 Oct 25 at 5:11 am
xbet giri? [url=https://1xbet-9.com/]1xbet-9.com[/url] .
1xbet_ajSn
25 Oct 25 at 5:12 am
birxbet [url=http://1xbet-4.com]http://1xbet-4.com[/url] .
1xbet_olol
25 Oct 25 at 5:15 am
1xbet tr [url=http://1xbet-9.com]1xbet tr[/url] .
1xbet_udSn
25 Oct 25 at 5:18 am
tutorial wordpress mcp
WilliamJet
25 Oct 25 at 5:19 am
куплю диплом высшего образования [url=https://www.rudik-diplom14.ru]куплю диплом высшего образования[/url] .
Diplomi_frea
25 Oct 25 at 5:21 am
bahis sitesi 1xbet [url=http://1xbet-4.com]http://1xbet-4.com[/url] .
1xbet_lyol
25 Oct 25 at 5:21 am
https://t.me/bs_1xbet/12
https://t.me/s/bs_1xbet/32
25 Oct 25 at 5:22 am
Как купить Клад в Усть-Луге?Нашел сайт https://xbanners.ru
– судя по отзывам ок. Цены адекватные, доставка быстрая. Кто-нибудь заказывал? Как с чистотой?
Stevenref
25 Oct 25 at 5:24 am
https://t.me/bs_1xbet/43
https://t.me/bs_1xbet/14
25 Oct 25 at 5:24 am
Discover everyday swaps that cut plastic, save money, and help the planet https://ecofriendlystore.ru/
EcoFriendNeuct
25 Oct 25 at 5:26 am
https://t.me/bs_1xbet/43
https://t.me/bs_1xbet/41
25 Oct 25 at 5:29 am
1xbet lite [url=https://1xbet-9.com/]1xbet-9.com[/url] .
1xbet_caSn
25 Oct 25 at 5:30 am
https://t.me/s/bs_1xbet/30
https://t.me/bs_1xbet/3
25 Oct 25 at 5:30 am
1xbet yeni giri? [url=http://1xbet-4.com/]http://1xbet-4.com/[/url] .
1xbet_ziol
25 Oct 25 at 5:33 am
1xbet tr giri? [url=https://1xbet-7.com]https://1xbet-7.com[/url] .
1xbet_zdol
25 Oct 25 at 5:34 am
купить диплом косметолога [url=rudik-diplom14.ru]купить диплом косметолога[/url] .
Diplomi_eeea
25 Oct 25 at 5:35 am
Goodness, no matter ԝhether establishment proves fancy,
mathematics serves аs thе decisive topic іn developing assurance
in figures.
Οh no, primary maths teaches real-ԝorld implementations including money management, tһus makе sure yoᥙr
youngster masters tһis right starting уoung.
Millennia Institute оffers аn unique thгee-year
pathway tߋ A-Levels, offering flexibility ɑnd depth in commerce, arts, аnd sciences for varied students.
Іts centralised approach guarantees customised support ɑnd
holistic advancement thrоugh innovative programs.
Ⴝtate-of-the-art facilities and dedicated staff develop ɑn engaging environment for academic and personal growth.
Students benefit fгom partnerships ԝith industries for real-wоrld
experiences ɑnd scholarships. Alumni prosper іn universities аnd professions,
highlighting tһe institute’ѕ commitment to lifelong learning.
Anderson Serangoon Junior College, гesulting from
tһe strategic merger of Anderson Jubior College ɑnd Serangoon Junior College, produces ɑ
dynamic and inclusive learning community that prioritizes Ьoth academic
rigor ɑnd detailed individual development, ensuring students receive
individualized attention іn a nurturing atmosphere.
Τһe organization features аn range of advanced facilities, ѕuch as specialized science
labs equipped ѡith tһe most rеcent innovation,
interactive class developed fοr group partnership, ɑnd substantial libraries stocked ԝith digital resources, ɑll of ᴡhich empower trainees tߋ dig іnto innovative
projects іn science, innovation, engineering, ɑnd mathematics.
Bу positioning a strong focus oon management training аnd character education tһrough structured programs ⅼike
student councils and mentorship initiatives, students cultivate
іmportant qualities such ɑs strength, empathy, аnd efficient team effort
tһat extend bеyond scholastic achievements. Additionally, tһe college’s commitment to
promoting international awareness appears іn itѕ reputable global exchange programs аnd partnerships ԝith overseas institutions, enabling students tо gain invaluable cross-cultural
experiences аnd broaden tһeir worldview in preparation fօr a globally linked future.
Ꭺs a testimony tо its efficiency, graduates fгom Anderson Serangoon Junior College regularlyy acquire admission tߋ
distinguished universities ƅoth locally and internationally, embodying tһе institution’s unwavering commitment tо producing positive, adaptable, ɑnd complex individuals
ready tо master diverse fields.
Αpart Ьeyond school facilities, focus upon mathematics to prevent
common pitfalls including inattentive errors Ԁuring
tests.
Parents, fearful оf losing style engaged lah, robust primary mathematics leads іn ƅetter science grasp аnd tech dreams.
Oh, mathematics serves аs the groundwork stone for primary education, aiding children ԝith
geometric thinking іn architecture careers.
Parents, worry about the difference hor, mathematics groundwork proves vital Ԁuring Junior College t᧐
understanding figures, essential ᴡithin current digital economy.
Wah lao, no matter іf institution remains һigh-end, mathematics serves аs the critical topic tο cultivates confidence regarding numƄers.
Strong A-level performance leads to bettеr mental health post-exams,
knowing уοu’re ѕet.
Parents, competitive mode engaged lah, strong primary maths гesults for improved science understanding аs well aas tech dreams.
Feel free t᧐ visit my web sikte list of secondary school
list of secondary school
25 Oct 25 at 5:36 am
1xbet t?rkiye [url=https://1xbet-7.com/]https://1xbet-7.com/[/url] .
1xbet_wwol
25 Oct 25 at 5:37 am
В Нижнем Тагиле выездные бригады дежурят 24/7. Координатор уже при первичном звонке собирает ключевые сведения: аллергии, хронические заболевания, список постоянных препаратов, эпизоды судорог или алкогольных психозов в прошлом, последние значения давления и пульса, возможность обеспечить тишину на 2–3 часа, доступ к питьевой воде, особенности подъезда. Вся эта информация помогает врачу подготовить стартовую схему — от выбора профиля инфузии до темпа введения. По запросу организуем немаркированный визит и сдержанные формулировки в документах, чтобы не создавать лишнего внимания со стороны соседей и коллег.
Изучить вопрос глубже – [url=https://vyvod-iz-zapoya-nizhnij-tagil0.ru/]вывод из запоя нижний тагил[/url]
ClaudeTwilm
25 Oct 25 at 5:38 am
certainly like your website however you need to take
a look at the spelling on quite a few of your posts.
A number of them are rife with spelling problems and I in finding it very
troublesome to tell the reality then again I’ll certainly come again again.
excavation haul-out dirt service
25 Oct 25 at 5:39 am
Yes! Finally something about Montgomery Roofing – Waco Roofers.
roofers
25 Oct 25 at 5:39 am
1xbet [url=http://1xbet-9.com/]1xbet[/url] .
1xbet_rdSn
25 Oct 25 at 5:41 am
centensports.com – Would like to see more user-feedback or community stories in future.
Doyle Every
25 Oct 25 at 5:43 am
1xbet yeni giri? [url=https://1xbet-4.com]https://1xbet-4.com[/url] .
1xbet_heol
25 Oct 25 at 5:44 am
1xbet t?rkiye giri? [url=https://www.1xbet-7.com]https://www.1xbet-7.com[/url] .
1xbet_ufol
25 Oct 25 at 5:45 am
купить диплом в иркутске [url=rudik-diplom6.ru]купить диплом в иркутске[/url] .
Diplomi_bcKr
25 Oct 25 at 5:45 am
Hello, Neat post. There’s a problem together with your website in internet explorer, might
test this? IE still is the market leader and a huge element of folks
will omit your fantastic writing due to this problem.
painting contractor
25 Oct 25 at 5:47 am
tutorial wordpress mcp
WilliamJet
25 Oct 25 at 5:48 am
1xbet resmi [url=http://1xbet-9.com/]http://1xbet-9.com/[/url] .
1xbet_hpSn
25 Oct 25 at 5:50 am
MCP WordPress
WilliamJet
25 Oct 25 at 5:50 am
https://t.me/bs_1xbet/47
https://t.me/bs_1xbet/8
25 Oct 25 at 5:52 am
1xbet com giri? [url=https://www.1xbet-4.com]https://www.1xbet-4.com[/url] .
1xbet_vcol
25 Oct 25 at 5:52 am
Как купить Меф в Геленджике?Посоветуйте, стоит ли заказывать на https://lemoon89.ru
? Цены привлекательные, доставка есть. Но хочется узнать про фактическое качество.
Stevenref
25 Oct 25 at 5:53 am
https://t.me/s/bs_1xbet/36
https://t.me/s/bs_1xbet/6
25 Oct 25 at 5:53 am
Hey there! Do you use Twitter? I’d like to follow you if
that would be ok. I’m undoubtedly enjoying your blog and
look forward to new updates.
join jihad
25 Oct 25 at 5:53 am
Мы работаем без пауз и очередей, принимая пациентов в любое время суток. В распоряжении бригад — немаркированный транспорт для выезда на дом, портативная диагностика (ЭКГ, пульсоксиметрия, экспресс-анализаторы), а в стационаре — палаты с регулируемым микроклиматом и круглосуточный мониторинг жизненных показателей. Вся документация оформляется в нейтральной терминологии, доступ к данным разграничен по ролям, что защищает вашу анонимность.
Исследовать вопрос подробнее – [url=https://narcologicheskaya-klinika-stavropol0.ru/]наркологическая клиника клиника помощь[/url]
RonaldEralf
25 Oct 25 at 5:54 am
1xbet giri? adresi [url=http://1xbet-7.com]http://1xbet-7.com[/url] .
1xbet_qlol
25 Oct 25 at 5:55 am
Кровь на гепатит C Гепатит C, или, как его иногда называют, «ласковый убийца», – это вирусное заболевание печени, которое, к сожалению, часто протекает незаметно в течение многих лет, прежде чем проявиться серьезными осложнениями. Возбудителем является вирус гепатита C (HCV), поражающий клетки печени (гепатоциты) и вызывающий хроническое воспаление. Без своевременной диагностики и лечения хронический гепатит C может привести к циррозу печени, печеночной недостаточности и гепатоцеллюлярной карциноме (раку печени), что делает его одной из главных причин трансплантации печени во всем мире.
Alonzolep
25 Oct 25 at 5:58 am
https://t.me/bs_1xbet/49
https://t.me/s/bs_1xbet/9
25 Oct 25 at 5:58 am
https://t.me/s/bs_1xbet/51
https://t.me/s/bs_1xbet/11
25 Oct 25 at 5:59 am
1xbet yeni giri? [url=https://1xbet-9.com/]1xbet yeni giri?[/url] .
1xbet_niSn
25 Oct 25 at 5:59 am
seenministries.com – Visuals are modern and bright, which helps make the message more engaging.
Sherrell Tarascio
25 Oct 25 at 6:00 am
J’adore l’ame soul de BassBet Casino, c’est une plateforme qui groove comme une voix soul. La selection de jeux est harmonieuse, comprenant des jeux adaptes aux cryptos. Avec des depots ultra-rapides. L’assistance est rapide et pro, toujours pret a chanter. Les gains arrivent sans attendre, neanmoins des recompenses supplementaires seraient vibrantes. Pour conclure, BassBet Casino est une plateforme qui groove pour les joueurs en quete d’adrenaline ! Par ailleurs la navigation est simple et rythmee, ce qui rend chaque session plus groovy. A souligner les tournois reguliers pour la competition, offre des recompenses continues.
bassbetcasinopromocodefr.com|
SoulSpinnerJ2zef
25 Oct 25 at 6:00 am