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!
kraken официальные ссылки kraken onion, kraken onion ссылка, kraken onion зеркала, kraken рабочая ссылка onion, сайт kraken onion, kraken darknet, kraken darknet market, kraken darknet ссылка, сайт kraken darknet, kraken актуальные ссылки, кракен ссылка kraken, kraken официальные ссылки, kraken ссылка тор, kraken ссылка зеркало, kraken ссылка на сайт, kraken онион, kraken онион тор, кракен онион, кракен онион тор, кракен онион зеркало, кракен даркнет маркет, кракен darknet, кракен onion, кракен ссылка onion, кракен onion сайт, kra ссылка, kraken сайт, kraken актуальные ссылки, kraken зеркало, kraken ссылка зеркало, kraken зеркало рабочее, актуальные зеркала kraken, kraken сайт зеркала, kraken маркетплейс зеркало, кракен ссылка, кракен даркнет
RichardPep
18 Sep 25 at 1:00 am
список займов онлайн [url=http://zaimy-12.ru]http://zaimy-12.ru[/url] .
zaimi_ftSt
18 Sep 25 at 1:02 am
фильмы hd 1080 смотреть бесплатно [url=http://www.kinogo-13.top]http://www.kinogo-13.top[/url] .
kinogo_onMl
18 Sep 25 at 1:03 am
I all the time used to study piece of writing in news papers
but now as I am a user of internet so from now I am using net for posts, thanks to web.
turkey visa for australian
18 Sep 25 at 1:03 am
[url=https://1deposit.net/slots/]1 deposit[/url]
CliftonPilky
18 Sep 25 at 1:05 am
взо [url=https://zaimy-13.ru]https://zaimy-13.ru[/url] .
zaimi_jqKt
18 Sep 25 at 1:06 am
Процесс лечения организован поэтапно, что делает его последовательным и результативным.
Разобраться лучше – http://narkologicheskaya-klinika-v-permi0.ru
RodolfoLup
18 Sep 25 at 1:06 am
https://xn--krken23-bn4c.com
Howardreomo
18 Sep 25 at 1:07 am
смотреть комедии онлайн [url=http://kinogo-13.top]http://kinogo-13.top[/url] .
kinogo_jbMl
18 Sep 25 at 1:08 am
https://www.wildberries.ru/catalog/171056105/detail.aspx
Curtissab
18 Sep 25 at 1:09 am
список займов онлайн на карту [url=www.zaimy-12.ru]www.zaimy-12.ru[/url] .
zaimi_jhSt
18 Sep 25 at 1:11 am
https://www.wildberries.ru/catalog/171056105/detail.aspx
Curtissab
18 Sep 25 at 1:12 am
список займов онлайн [url=https://zaimy-13.ru]https://zaimy-13.ru[/url] .
zaimi_rnKt
18 Sep 25 at 1:12 am
Hi, for all time i used to check blog posts here early in the break of day,
since i like to learn more and more.
koh management
18 Sep 25 at 1:14 am
https://kemono.im/aacudehyg/kakie-narkotiki-kupit
Timothyces
18 Sep 25 at 1:14 am
мфо займ [url=https://zaimy-13.ru]https://zaimy-13.ru[/url] .
zaimi_jfKt
18 Sep 25 at 1:16 am
code promo 1xbet 1xgames code promo 1xbet cote d’ivoire Le monde des paris sportifs en ligne est en constante evolution, et 1xbet s’est etabli comme un leader mondial, offrant une plateforme complete et diversifiee aux parieurs du monde entier. En Cote d’Ivoire, l’enthousiasme pour les paris sportifs est palpable, et 1xbet Cote d’Ivoire propose une gamme allechante de promotions pour ameliorer l’experience de pari de ses utilisateurs. Au c?ur de ces promotions se trouvent les codes promotionnels, des cles magiques qui debloquent des bonus exclusifs et des opportunites de pari ameliorees. Qu’est-ce qu’un code promo 1xbet Cote d’Ivoire? Un code promo 1xbet Cote d’Ivoire est une combinaison unique de lettres et de chiffres qui peuvent etre entres lors de l’inscription ou du depot pour activer un bonus specifique. Ces bonus peuvent inclure: Bonus de bienvenue: Un bonus offert aux nouveaux utilisateurs lors de leur premier depot. Bonus de depot: Un bonus accorde sur les depots ulterieurs. Paris gratuits: La possibilite de placer un pari sans risquer son propre argent. Cotes ameliorees: Des cotes plus elevees sur certains evenements sportifs. Ou trouver les codes promo 1xbet Cote d’Ivoire? Les codes promotionnels 1xbet Cote d’Ivoire sont disponibles via plusieurs canaux, notamment: Le site Web officiel de 1xbet: Consultez regulierement le site Web de 1xbet pour les dernieres offres. Les sites Web affilies: De nombreux sites Web affilies proposent des codes promotionnels exclusifs pour les joueurs ivoiriens. Les reseaux sociaux: Suivez les comptes de medias sociaux officiels de 1xbet pour rester informe des nouvelles versions de code promotionnel. Les newsletters par e-mail: Abonnez-vous aux newsletters par e-mail de 1xbet pour recevoir les codes promotionnels directement dans votre boite de reception. L’utilisation efficace des codes promotionnels 1xbet Cote d’Ivoire peut considerablement augmenter vos chances de gagner et rendre votre experience de pari plus agreable. Gardez un ?il sur les derniers codes et profitez des avantages qu’ils offrent.
Jeffreyengem
18 Sep 25 at 1:17 am
займер ру [url=zaimy-12.ru]zaimy-12.ru[/url] .
zaimi_hySt
18 Sep 25 at 1:18 am
Big discounts on offer for https://synthroidvslevothyroxine.com/ can help you buy it safely online.
EcrFlulk
18 Sep 25 at 1:21 am
микрозайм всем [url=http://www.zaimy-13.ru]http://www.zaimy-13.ru[/url] .
zaimi_lpKt
18 Sep 25 at 1:21 am
Heya just wanted to give you a brief heads up and let you know a few of the images aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different
browsers and both show the same results.
79club.ru.com
18 Sep 25 at 1:22 am
все микрозаймы онлайн [url=https://zaimy-12.ru/]https://zaimy-12.ru/[/url] .
zaimi_jySt
18 Sep 25 at 1:23 am
Когда вызывать нарколога на дом:
Узнать больше – [url=https://narkolog-na-dom-krasnogorsk6.ru/]нарколог на дом[/url]
Normanaburl
18 Sep 25 at 1:23 am
накрутка подписчиков в телеграм живые бесплатно
Stevenplolf
18 Sep 25 at 1:25 am
Wow, this piece of writing is fastidious, my younger
sister is analyzing these kinds of things, therefore I am going to let know
her.
turkey visa for australian
18 Sep 25 at 1:27 am
мфо займ онлайн [url=https://zaimy-12.ru]https://zaimy-12.ru[/url] .
zaimi_clSt
18 Sep 25 at 1:29 am
работа вахтой электрогазосварщик Условия работы на заводе – это совокупность факторов, определяющих характер и содержание труда работников на производственном предприятии. Условия работы на заводе могут включать уровень заработной платы, график работы, условия труда, социальные гарантии и другие аспекты.
Jameswendy
18 Sep 25 at 1:29 am
Thanks , I’ve recently been looking for info about this subject for ages and yours
is the greatest I have discovered so far. But, what
in regards to the conclusion? Are you certain concerning
the source?
1ИКСПЛЕЙ казино официальный
18 Sep 25 at 1:30 am
Folks, kiasu style engaged lah, strong primary maths гesults foг superior STEM
understanding аnd tech aspirations.
Wow, maths serves as thе groundwork pillar fоr primary learning, helping children fοr dimensional
analysis іn design routes.
Temasek Junior College inspires trendsetters tһrough extensive academics ɑnd ethical
worths, blending custom wіth development. Research centers
ɑnd electives in languages and arts promote deep knowing.
Dynamic со-curriculars develop teamwork ɑnd imagination. International collaborations improve
international competence. Alumni flourish іn distinguished
organizations, embodying excellence ɑnd service.
Victoria Junior College fires ᥙp creativity and promotes visionary leadership, empowering students t᧐ develop positive modification through a curriculum tһat sparks passions
ɑnd motivates bold thinking іn a picturesque seaside
campus setting. Ƭhe school’ѕ extensive facilities, consisting ᧐f humanities conversation spaces,
science research study suites, аnd arts performance venues,
assistance enriched programs іn arts, liberal arts, and sciences tһat promote interdisciplinary insights ɑnd academic
proficiency. Strategic alliances ԝith secondary schools throսgh integrated
programs ensure ɑ smooth instructional journey, offering
accelerated finding оut courses and specialized electives
tһat cater to individual strengths and inteгests.
Service-learning initiatives аnd global outreach jobs, ѕuch as global volunteer
explorations ɑnd leadership forums, develop caring dispositions, durability, аnd a dedication to neighborhood welfare.
Graduates lead ᴡith steady conviction and accomplish remarkable success іn universities аnd
professions, embodying Victoria Junior College’ѕ legacy of nurturing imaginative, principled,
and transformative people.
Alas, mіnus robust math in Junior College,
еvеn toр institution children may stumble in secondary calculations,
tһerefore develop tһiѕ immediatеly leh.
Oi oi, Singapore folks,mathematics гemains perhaps thе
highly important primary topic, promoting imagination tһrough
challenge-tackling to creative jobs.
Ꭰon’t mess around lah, link a goօd Junior College alongside maths proficiency fߋr assure
elevated Ꭺ Levels results and seamless chɑnges.
Hey hey, Singapore folks, math remains probably the
highly essential primary subject, promoting imagination іn issue-resolving t᧐
groundbreaking jobs.
Listen up, Singapore parents, maths іs likelʏ the highly
іmportant primary subject, promoting creativity іn challenge-tackling to innovative
professions.
Practicing Math papers religiously helps build resilience fⲟr real-world pгoblem-solving.
Aiyo, ѡithout solid mathematics in Junior College, no matter
leading establishment youngsters ⅽould falter witһ next-level
calculations, ѕo build that immeɗiately leh.
Feel free to surf to my website; Jason Math Tutor
Jason Math Tutor
18 Sep 25 at 1:30 am
kraken darknet market kraken onion, kraken onion ссылка, kraken onion зеркала, kraken рабочая ссылка onion, сайт kraken onion, kraken darknet, kraken darknet market, kraken darknet ссылка, сайт kraken darknet, kraken актуальные ссылки, кракен ссылка kraken, kraken официальные ссылки, kraken ссылка тор, kraken ссылка зеркало, kraken ссылка на сайт, kraken онион, kraken онион тор, кракен онион, кракен онион тор, кракен онион зеркало, кракен даркнет маркет, кракен darknet, кракен onion, кракен ссылка onion, кракен onion сайт, kra ссылка, kraken сайт, kraken актуальные ссылки, kraken зеркало, kraken ссылка зеркало, kraken зеркало рабочее, актуальные зеркала kraken, kraken сайт зеркала, kraken маркетплейс зеркало, кракен ссылка, кракен даркнет
RichardPep
18 Sep 25 at 1:31 am
internet apotheke: internet apotheke – eu apotheke ohne rezept
Israelpaync
18 Sep 25 at 1:31 am
kraken onion зеркала kraken onion, kraken onion ссылка, kraken onion зеркала, kraken рабочая ссылка onion, сайт kraken onion, kraken darknet, kraken darknet market, kraken darknet ссылка, сайт kraken darknet, kraken актуальные ссылки, кракен ссылка kraken, kraken официальные ссылки, kraken ссылка тор, kraken ссылка зеркало, kraken ссылка на сайт, kraken онион, kraken онион тор, кракен онион, кракен онион тор, кракен онион зеркало, кракен даркнет маркет, кракен darknet, кракен onion, кракен ссылка onion, кракен onion сайт, kra ссылка, kraken сайт, kraken актуальные ссылки, kraken зеркало, kraken ссылка зеркало, kraken зеркало рабочее, актуальные зеркала kraken, kraken сайт зеркала, kraken маркетплейс зеркало, кракен ссылка, кракен даркнет
RichardPep
18 Sep 25 at 1:33 am
No matter if some one searches for his vital thing, therefore he/she desires to
be available that in detail, therefore that thing is maintained
over here.
hay8856.com
18 Sep 25 at 1:33 am
раздвижной электрокарниз [url=http://www.razdvizhnoj-elektrokarniz.ru]http://www.razdvizhnoj-elektrokarniz.ru[/url] .
razdvijnoi elektrokarniz_qwei
18 Sep 25 at 1:34 am
список займов онлайн [url=www.zaimy-13.ru]www.zaimy-13.ru[/url] .
zaimi_swKt
18 Sep 25 at 1:36 am
https://rant.li/2t9dxhluft
Timothyces
18 Sep 25 at 1:36 am
микро займы онлайн [url=https://zaimy-13.ru/]микро займы онлайн[/url] .
zaimi_yjKt
18 Sep 25 at 1:40 am
https://xn--krken21-bn4c.com
Howardreomo
18 Sep 25 at 1:40 am
A better way to hold, manage, and grow crypto. Exodus Wallet makes it easy — without compromising on security or speed.
[url=https://www.bing.com/search?q=eg-exodus.cc%20exodus%20wallet%20&qs=n&form=QBRE&sp=-1&lq=0&pq=eg-exodus.cc%20exodus%20wallet%20&sc=12-27&sk=&cvid=3F5D9FA6756041529193632511E19C38]exodus wallet web3[/url]
Tonysok
18 Sep 25 at 1:40 am
Viagra 100 mg ohne Rezept [url=https://intimgesund.com/#]IntimGesund[/url] potenzmittel diskret bestellen
StevenTilia
18 Sep 25 at 1:40 am
займ всем [url=www.zaimy-13.ru]www.zaimy-13.ru[/url] .
zaimi_dxKt
18 Sep 25 at 1:42 am
codes promo 1xbet togo
code promo 1xbet rw
18 Sep 25 at 1:43 am
https://xn--krken23-bn4c.com
Howardreomo
18 Sep 25 at 1:43 am
микро займы онлайн [url=https://zaimy-12.ru]микро займы онлайн[/url] .
zaimi_wzSt
18 Sep 25 at 1:43 am
Каждый день запоя увеличивает риск для жизни. Не рискуйте — специалисты в Краснодаре приедут на дом и окажут экстренную помощь. Без боли, стресса и ожидания.
Изучить вопрос глубже – [url=https://vyvod-iz-zapoya-krasnodar15.ru/]vyvod-iz-zapoya-krasnodar15.ru[/url]
MichaelInink
18 Sep 25 at 1:44 am
These are really enormous ideas in concerning blogging. You have touched
some nice things here. Any way keep up wrinting.
turkey visa for australian
18 Sep 25 at 1:44 am
Greetings! Very useful advice within this post! It is the little
changes which will make the greatest changes.
Thanks a lot for sharing!
sleepcalculator.my
18 Sep 25 at 1:47 am
список займов онлайн [url=https://www.zaimy-12.ru]https://www.zaimy-12.ru[/url] .
zaimi_wgSt
18 Sep 25 at 1:47 am
все микрозаймы онлайн [url=https://zaimy-12.ru/]https://zaimy-12.ru/[/url] .
zaimi_qjSt
18 Sep 25 at 1:49 am
займ всем [url=https://zaimy-15.ru]https://zaimy-15.ru[/url] .
zaimi_ecpn
18 Sep 25 at 1:49 am