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!
Вот здесь можно найти больше примеров:
По теме “phenoma.ru”, есть отличная статья.
Смотрите сами:
[url=https://phenoma.ru]https://phenoma.ru[/url]
Обращайтесь, если что.
rusPoito
19 Aug 25 at 10:57 am
где купить аттестаты за 11 класс с занесением в реестр отзывы [url=http://arus-diplom23.ru]где купить аттестаты за 11 класс с занесением в реестр отзывы[/url] .
Diplomi_rdol
19 Aug 25 at 10:57 am
легально купить диплом о [url=https://arus-diplom35.ru/]легально купить диплом о[/url] .
Vigodno zakazat diplom ob obrazovanii!_fdot
19 Aug 25 at 10:59 am
Эта публикация завернет вас в вихрь увлекательного контента, сбрасывая стереотипы и открывая двери к новым идеям. Каждый абзац станет для вас открытием, полным ярких примеров и впечатляющих достижений. Подготовьтесь быть вовлеченными и удивленными каждый раз, когда продолжите читать.
Уточнить детали – https://my-energyco.com/standard-post
MatthewCof
19 Aug 25 at 11:00 am
cialis 20 milligram: buy cialis online from canada – Tadalify
PeterTEEFS
19 Aug 25 at 11:01 am
https://allmynursejobs.com/author/anettecrouse03011999/
WilliamTop
19 Aug 25 at 11:01 am
купить аттестат за 11 класс вологодская область [url=www.arus-diplom25.ru/]купить аттестат за 11 класс вологодская область[/url] .
Diplomi_udot
19 Aug 25 at 11:02 am
Testo Booster is a supplement made to naturally support healthy testosterone levels, which can help improve energy,
strength, and overall performance. It’s often used
by men looking to boost muscle growth, enhance stamina, and
maintain vitality as they age. Many users like it because
it offers a simple and natural way to feel stronger, more confident,
and more energized in daily life.
Testo Booster
19 Aug 25 at 11:03 am
otc viagra 2017 [url=https://sildenapeak.com/#]SildenaPeak[/url] average cost of viagra in canada
RobertCat
19 Aug 25 at 11:03 am
Эта статья для ознакомления предлагает читателям общее представление об актуальной теме. Мы стремимся представить ключевые факты и идеи, которые помогут читателям получить представление о предмете и решить, стоит ли углубляться в изучение.
Подробная информация доступна по запросу – https://www.servicioim.com/hello-world
RonnieBoype
19 Aug 25 at 11:06 am
continuously i used to read smaller articles or reviews that also clear their motive, and that is also happening with this paragraph which I am
reading now.
구글아이디판매
19 Aug 25 at 11:07 am
Мы предлагаем вам подробное руководство, основанное на проверенных источниках и реальных примерах. Каждая часть публикации направлена на то, чтобы помочь вам разобраться в сложных вопросах и применить знания на практике.
Лучшее решение — прямо здесь – https://zeldzaamheid.nl/koffie
EdwinWep
19 Aug 25 at 11:09 am
SildenaPeak: viagra paypal online – SildenaPeak
RichardTit
19 Aug 25 at 11:10 am
В статье представлены ключевые моменты по актуальной теме, дополненные советами экспертов и ссылками на дополнительные ресурсы. Цель материала — дать читателю инструменты для самостоятельного развития и принятия осознанных решений.
Это стоит прочитать полностью – https://dancingstarslinedance.dk/galleri/billeder/soendagsdans-januar-2018
MyronTrasE
19 Aug 25 at 11:11 am
Nice blog here! Also your site loads up fast! What web host are
you using? Can I get your affiliate link to your host?
I wish my site loaded up as fast as yours lol
جواب آزمون نمونه دولتی ۱۴۰۴
19 Aug 25 at 11:12 am
В этой публикации мы предлагаем подробные объяснения по актуальным вопросам, чтобы помочь читателям глубже понять их. Четкость и структурированность материала сделают его удобным для усвоения и применения в повседневной жизни.
Секреты успеха внутри – https://www.hotel-sugano.com/bbs/sugano.cgi/www.tovery.net/www.skitour.su/sinopipefittings.com/e_Feedback/www.hip-hop.ru/forum/id298234-worksale/www.tovery.net/sugano.cgi?page30=val
Francisetexy
19 Aug 25 at 11:12 am
Мы собрали для вас самые захватывающие факты из мира науки и истории. От малознакомых деталей до грандиозных событий — эта статья расширит ваш кругозор и подарит новое понимание того, как устроен наш мир.
Более того — здесь – https://ejv.co.nz/hello-world
MyronTrasE
19 Aug 25 at 11:13 am
Wow! At last I got a weblog from where I know how to actually take useful
facts concerning my study and knowledge.
Montgomery Roofing - Lorena Roofers
19 Aug 25 at 11:14 am
Предлагаем вашему вниманию интересную справочную статью, в которой собраны ключевые моменты и нюансы по актуальным вопросам. Эта информация будет полезна как для профессионалов, так и для тех, кто только начинает изучать тему. Узнайте ответы на важные вопросы и расширьте свои знания!
Что ещё? Расскажи всё! – https://www.littlemissmomma.com/2013/01/how-to-make-baby-rice-cereal-from-scratch.html
Cliftonjam
19 Aug 25 at 11:15 am
https://beteiligung.stadtlindau.de/profile/%D0%9A%D1%83%D0%BF%D0%B8%D1%82%D1%8C%20%D0%91%D0%BE%D1%88%D0%BA%D0%B8%20%D0%9C%D0%B0%D1%80%D0%B8%D1%85%D1%83%D0%B0%D0%BD%D1%83%20%D0%93%D0%B0%D1%88%D0%B8%D1%88%20%D0%9B%D0%B8%D0%B5%D0%BF%D0%B0%D1%8F/
Danielevemy
19 Aug 25 at 11:16 am
Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
Изучить вопрос глубже – https://marcelalozano.com/mar_m001_probabilidad_50x50cm_-2
RonnieBoype
19 Aug 25 at 11:17 am
SildenaPeak: viagra price online india – viagra online with paypal
RichardTit
19 Aug 25 at 11:18 am
Situs TESLATOTO menghadirkan koleksi demo slot
paling lengkap dari PG Soft dan Pragmatic Play. Mainkan tanpa deposit game populer
seperti Olympus, Bonanza, dan slot naga Mahjong Ways tanpa biaya.
Rasakan sensasi bermain slot gacor 100% tanpa bayar.
teslatoto login
19 Aug 25 at 11:19 am
https://kemono.im/yecacyyd/d-ior-kupit-gashish-boshki-marikhuanu
WilliamTop
19 Aug 25 at 11:22 am
ставки прогнозы [url=www.novosti-sporta-12.ru/]ставки прогнозы[/url] .
novosti sporta_lksl
19 Aug 25 at 11:25 am
Thanks for your marvelous posting! I genuinely enjoyed reading it, you
happen to be a great author. I will ensure that I bookmark your blog
and will come back sometime soon. I want to encourage yourself to continue your great job, have a nice evening!
تکمیل ظرفیت فرهنگیان ۱۴۰۴ کد ۶
19 Aug 25 at 11:26 am
Hi, I do believe your website may be having browser compatibility problems.
Whenever I take a look at your website in Safari, it looks fine but when opening
in I.E., it has some overlapping issues. I merely wanted to give you a quick heads up!
Aside from that, excellent website!
강남쩜오
19 Aug 25 at 11:26 am
можно ли купить аттестат за 9 и 11 класс [url=https://www.arus-diplom25.ru]https://www.arus-diplom25.ru[/url] .
Diplomi_spot
19 Aug 25 at 11:27 am
где можно купить аттестат за 11 класс сколько стоит красноярск [url=https://www.arus-diplom22.ru]где можно купить аттестат за 11 класс сколько стоит красноярск[/url] .
Diplomi_sesl
19 Aug 25 at 11:28 am
buy generic cialis 5mg: Tadalify – how much does cialis cost with insurance
RichardTit
19 Aug 25 at 11:28 am
It’s genuinely very difficult in this active life to listen news on Television, therefore I just use internet for that reason,
and get the most recent information.
swapz
19 Aug 25 at 11:28 am
Hi there colleagues, good article and nice arguments commented at this place,
I am genuinely enjoying by these.
https://rapidapi.com/b365api-b365api-default/api/betsapi2/discussions/152921
19 Aug 25 at 11:29 am
If you desire to improve your experience just keep visiting this web
page and be updated with the newest news update posted here.
فرهنگیان تکمیل ظرفیت
19 Aug 25 at 11:31 am
аттестат за 11 классов купить в красноярске [url=www.arus-diplom23.ru/]аттестат за 11 классов купить в красноярске[/url] .
Diplomi_saol
19 Aug 25 at 11:32 am
купить аттестат 11 классов нижний новгород [url=www.arus-diplom25.ru/]купить аттестат 11 классов нижний новгород[/url] .
Diplomi_zbot
19 Aug 25 at 11:33 am
купить диплом колледжа с занесением в реестр [url=http://www.arus-diplom35.ru]купить диплом колледжа с занесением в реестр[/url] .
Bistro i prosto kypit diplom o visshem obrazovanii!_gvot
19 Aug 25 at 11:34 am
I am genuinely glad to read this webpage posts which contains
lots of useful data, thanks for providing such statistics.
88aai.com
19 Aug 25 at 11:37 am
This text is invaluable. How can I find out more?
روش انتخاب رشته فرهنگیان ۱۴۰۴
19 Aug 25 at 11:40 am
https://pixelfed.tokyo/CindyJonesCin
WilliamTop
19 Aug 25 at 11:43 am
Your style is so unique in comparison to other people I
have read stuff from. Thanks for posting
when you’ve got the opportunity, Guess I
will just book mark this web site.
با تراز ۵۰۰۰ ریاضی چی قبول میشم نی نی سایت
19 Aug 25 at 11:55 am
Этот увлекательный информационный материал подарит вам массу новых знаний и ярких эмоций. Мы собрали для вас интересные факты и сведения, которые обогатят ваш опыт. Откройте для себя увлекательный мир информации и насладитесь процессом изучения!
Подробнее – http://www.forum.ethology.ru/showthread.php?t=3283&page=7
Kennethclorp
19 Aug 25 at 11:56 am
Affordable sildenafil citrate tablets for men: Safe access to generic ED medication – Sildenafil oral jelly fast absorption effect
ElijahKic
19 Aug 25 at 11:59 am
I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to create my own blog and would like to
find out where u got this from. thanks a lot
Épure Tradelin
19 Aug 25 at 11:59 am
Thanks for the marvelous posting! I truly enjoyed reading it, you’re a great
author. I will ensure that I bookmark your blog and definitely will come back someday.
I want to encourage you continue your great posts, have a nice holiday
weekend!
"natural beauty"
19 Aug 25 at 12:00 pm
https://b914c98ef23abc687c9067b14c.doorkeeper.jp/
Danielevemy
19 Aug 25 at 12:01 pm
He has had more cordial, more productive, meetings with US President Donald Trump since that now-notorious encounter on February 28.
[url=https://kraken2trfqodidvlh4aa337cpzfrhdlfldhve5nf7njhumwr7.com]kraken4qzqnoi7ogpzpzwrxk7mw53n5i56loydwiyonu4owxsh4g67yd onion[/url]
But for Ukrainian President Volodymyr Zelensky, today’s meeting at the White House will surely trigger awkward memories of that very public clash with the US President almost six months ago. Navigating the treacherous waters in which he finds himself today will be no easier.
[url=https://kraken5af44k24fwzohe6fvqfgxfsee4lgydb3ayzkfhlzqhuwlo33adonion.info]kraken7jmgt7yhhe2c4iyilthnhcugfylcztsdhh7otrr6jgdw667pqd onion[/url]
Increasingly, it appears likely he will be told to give up land in exchange for some sort of security guarantees.
The land side of that “deal” will be obvious. It can be drawn on a map. Crimea: gone, says Trump. Donetsk: give all of it up, says Putin, apparently with Trump’s blessing.
But the security guarantees? That’s where far more challenging ideas, like credibility, come into play. Could Zelensky rely on the US to deliver on some NATO Article 5-type promise, to defend Ukraine if Russia breaches any peace agreement?
Putin himself might even see an opportunity to further weaken the West, by testing any such guarantees, confident they are a bluff he could call. But all that would be for the future.
For now, it looks like Zelensky will have to weigh up whether he could bring his country with him if he were to cede territory to Russia – some of it still in Ukrainian hands – or whether he and his people could bear the costs of potentially defying Trump a Nobel Peace Prize, and say no.
If he chose the latter, would the US President immediately end all remaining American support for Ukraine, in terms of military aid and intelligence sharing, for instance?
If that happened, to what extent could Zelensky’s European allies really step in and fill in the gaps left by any full US retreat?
It is an almost impossibly hard choice before him.
kraken2trfqodidvlh4aa337cpzfrhdlfldhve5nf7njhumwr7instad onion
https://tor-kraken2trfqodidvlh4aa337cpzfrhdlfldhve5nf7njhumwr7instad.com
Thomasslete
19 Aug 25 at 12:02 pm
Mildcasino Arcade
PHP hook, building hooks in your application – Sjoerd Maessen blog at Sjoerd Maessen blog
Mildcasino Arcade
19 Aug 25 at 12:02 pm
https://shootinfo.com/author/hedulahemil/?pt=ads
WilliamTop
19 Aug 25 at 12:04 pm
купить аттестат об окончании 11 классов в нижнем новгороде [url=www.arus-diplom25.ru/]купить аттестат об окончании 11 классов в нижнем новгороде[/url] .
Diplomi_pcot
19 Aug 25 at 12:05 pm
Наша клиника предоставляет анонимный вывод из запоя и медицинскую помощь на высшем качественном уровне. Мы понимаем, как важно поддержать людей, страдающих от зависимости в сложные времена, и поэтому организуем круглосуточную службу поддержки и помощь в вопросах алкоголизма. После вывода из запоя начинается реабилитация, целью которой является восстановление и предотвращение рецидивов. Наша группа квалифицированных специалистов поддержит каждого пациента возвратиться к обычной жизни, обеспечивая индивидуальный подход к лечению. Не ждите, чтобы позаботиться о своем здоровье. Свяжитесь на vivod-iz-zapoya-krasnoyarsk009.ru за поддержкой прямо сейчас!
alkogolizmkrasnoyarskNeT
19 Aug 25 at 12:13 pm