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=http://frei-diplom5.ru]диплом занесен в реестр купить[/url] .
Diplomi_hzPa
19 Sep 25 at 8:41 pm
https://imageevent.com/jimljungstra/fhnqq
HarryPaync
19 Sep 25 at 8:41 pm
список займов онлайн [url=https://zaimy-16.ru/]список займов онлайн[/url] .
zaimi_ctMi
19 Sep 25 at 8:42 pm
купить диплом техникума образец [url=www.educ-ua7.ru]www.educ-ua7.ru[/url] .
Diplomi_poea
19 Sep 25 at 8:42 pm
Просто введите слово или фразу в
строку поиска, и сайт найдет все кадры
из фильмов, в которых используется данное выражение.
На сайте
19 Sep 25 at 8:44 pm
If some one wishes to be updated with latest technologies after that he must be go to see this
website and be up to date all the time.
steal brainrot script anti ban
19 Sep 25 at 8:44 pm
диплом государственного образца купить реестр [url=http://frei-diplom4.ru]диплом государственного образца купить реестр[/url] .
Diplomi_rtOl
19 Sep 25 at 8:44 pm
На сайте «Детский Класс» https://www.detskiyklass.ru нашим посетителям в любое время доступны материалы для приятного совместного досуга детей и их родителей: детские песни на разные тематики, которые можно разучивать и распевать в будни и праздники, интересные и познавательные легенды и мифы, раскраски различной сложности, а также волшебные и поучительные сказки.
detskiyklass-210
19 Sep 25 at 8:45 pm
We are a bunch of volunteers and starting a new scheme in our community.
Your website provided us with valuable information to work on. You’ve
done a formidable task and our entire neighborhood can be grateful to
you.
سئو سایت عربی
19 Sep 25 at 8:45 pm
купить диплом о высшем образовании с занесением в реестр цена [url=http://frei-diplom6.ru]купить диплом о высшем образовании с занесением в реестр цена[/url] .
Diplomi_auOl
19 Sep 25 at 8:45 pm
Когда состояние алкогольной интоксикации достигает критических уровней, своевременное вмешательство становится жизненно необходимым. В Мариуполе, Донецкая область, высококвалифицированные наркологи оказывают срочную помощь на дому, позволяя оперативно начать детоксикацию и стабилизировать состояние пациента. Такой формат лечения обеспечивает быстрый выход из кризиса в условиях комфорта и конфиденциальности, что особенно важно для тех, кто не может позволить себе задержки в стационарном лечении.
Детальнее – [url=https://narcolog-na-dom-mariupol00.ru/]нарколог на дом вывод из запоя[/url]
Rodneydig
19 Sep 25 at 8:48 pm
займы онлайн [url=www.zaimy-16.ru]займы онлайн[/url] .
zaimi_mnMi
19 Sep 25 at 8:48 pm
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2best at
bs2best.at blacksprut marketplace Official
CharlesNarry
19 Sep 25 at 8:48 pm
купить диплом с проводкой одной [url=https://frei-diplom3.ru/]купить диплом с проводкой одной[/url] .
Diplomi_pjKt
19 Sep 25 at 8:49 pm
Капельничное лечение от запоя – это современный метод детоксикации, который позволяет быстро и безопасно вывести токсины из организма. В Луганске ЛНР специалисты оказывают помощь на дому, предлагая профессиональное капельничное лечение для тех, кто столкнулся с тяжелой алкогольной интоксикацией. Такой подход обеспечивает оперативное вмешательство в привычной для пациента обстановке, гарантируя индивидуальный подход и полную конфиденциальность.
Получить больше информации – [url=https://kapelnica-ot-zapoya-lugansk-lnr0.ru/]капельница от запоя на дому круглосуточно луганск[/url]
MatthewShuth
19 Sep 25 at 8:49 pm
Получить диплом любого университета поможем. Купить диплом бакалавра в Астрахани – [url=http://diplomybox.com/kupit-diplom-bakalavra-v-astrakhani/]diplomybox.com/kupit-diplom-bakalavra-v-astrakhani[/url]
Cazrhrm
19 Sep 25 at 8:49 pm
купить диплом вуза занесением реестр [url=www.frei-diplom2.ru/]www.frei-diplom2.ru/[/url] .
Diplomi_dfEa
19 Sep 25 at 8:51 pm
купить диплом техникума с занесением в реестр [url=frei-diplom9.ru]купить диплом техникума с занесением в реестр[/url] .
Diplomi_hgea
19 Sep 25 at 8:51 pm
все займы рф [url=http://zaimy-16.ru]http://zaimy-16.ru[/url] .
zaimi_rwMi
19 Sep 25 at 8:52 pm
всезаймы [url=http://zaimy-16.ru]http://zaimy-16.ru[/url] .
zaimi_aoMi
19 Sep 25 at 8:53 pm
Черная футболка мокап и футболка принт бренд во Владивостоке. Свитшот однотонный и Bertholth бейсболка в Ижевске. Футболка принт город и хб футболки без рисунка в Набережных Челнах. Красивая спецодежда для мужчин и моды на одежду в майнкрафт в Ярославле. Модные принты на футболках оптом и футболки надпись https://futbolki-s-printom.ru/
Gregorysnisp
19 Sep 25 at 8:53 pm
Wow, fantastic blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is great, let alone the content!
بهترین دارو برای درمان لارنژیت
19 Sep 25 at 8:53 pm
Hey there! This is kind of off topic but I need some guidance from an established blog.
Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
I’m thinking about setting up my own but I’m not sure where to begin. Do you
have any tips or suggestions? Appreciate it
آمبولانس خصوصی پاکدشت
19 Sep 25 at 8:57 pm
микрозаймы все [url=https://zaimy-16.ru]https://zaimy-16.ru[/url] .
zaimi_egMi
19 Sep 25 at 8:58 pm
купить диплом о высшем образовании проведенный [url=http://frei-diplom5.ru]купить диплом о высшем образовании проведенный[/url] .
Diplomi_pePa
19 Sep 25 at 8:58 pm
pure cocaine in prague pure cocaine in prague
prague-drugs-454
19 Sep 25 at 8:58 pm
купить диплом в выборге [url=www.rudik-diplom7.ru/]www.rudik-diplom7.ru/[/url] .
Diplomi_nsPl
19 Sep 25 at 8:59 pm
как купить диплом о высшем образовании с занесением в реестр [url=frei-diplom4.ru]как купить диплом о высшем образовании с занесением в реестр[/url] .
Diplomi_hmOl
19 Sep 25 at 8:59 pm
купить диплом высшего образования с занесением в реестр [url=http://frei-diplom6.ru/]купить диплом высшего образования с занесением в реестр[/url] .
Diplomi_wjOl
19 Sep 25 at 8:59 pm
купить диплом слесаря [url=http://rudik-diplom11.ru/]купить диплом слесаря[/url] .
Diplomi_xaMi
19 Sep 25 at 8:59 pm
займы всем [url=https://zaimy-16.ru]займы всем[/url] .
zaimi_gfMi
19 Sep 25 at 9:02 pm
Enhance your online shopping decisions with FindQC.com, a platform that aggregates QC photos from shipping agents across major Chinese marketplaces.
Users can view detailed images of products
from Taobao, 1688, Weidian, and Tmall, ensuring quality assurance before purchase.
The site’s search capabilities by product link or image make
it a valuable resource for buyers on platforms like CNFans, Kakobuy, and
AllChinaBuy.
qc cnfans
19 Sep 25 at 9:03 pm
На данном этапе врач уточняет, сколько времени продолжается запой, какой тип алкоголя употребляется и имеются ли сопутствующие заболевания. Тщательный анализ этих данных позволяет подобрать оптимальные методы детоксикации и снизить риск осложнений.
Получить дополнительные сведения – https://narcolog-na-dom-mariupol0.ru/vyzov-narkologa-na-dom-mariupol/
Romanuteda
19 Sep 25 at 9:03 pm
купить диплом в нижнем новгороде [url=http://www.rudik-diplom1.ru]купить диплом в нижнем новгороде[/url] .
Diplomi_zeer
19 Sep 25 at 9:03 pm
купить диплом с регистрацией [url=http://frei-diplom2.ru]купить диплом с регистрацией[/url] .
Diplomi_geEa
19 Sep 25 at 9:04 pm
купить диплом с занесением в реестр в красноярске [url=http://frei-diplom3.ru]http://frei-diplom3.ru[/url] .
Diplomi_bvKt
19 Sep 25 at 9:04 pm
лучшие займы онлайн [url=http://zaimy-16.ru/]лучшие займы онлайн[/url] .
zaimi_jsMi
19 Sep 25 at 9:06 pm
купить диплом в архангельске с занесением в реестр [url=frei-diplom1.ru]купить диплом в архангельске с занесением в реестр[/url] .
Diplomi_jaOi
19 Sep 25 at 9:06 pm
https://muckrack.com/person-27906887
HarryPaync
19 Sep 25 at 9:06 pm
купить диплом во владивостоке [url=http://rudik-diplom1.ru/]купить диплом во владивостоке[/url] .
Diplomi_qwer
19 Sep 25 at 9:10 pm
займы все [url=https://zaimy-16.ru/]https://zaimy-16.ru/[/url] .
zaimi_pxMi
19 Sep 25 at 9:11 pm
I am extremely inspired along with your writing abilities and also with the layout to
your blog. Is this a paid subject or did you customize it yourself?
Anyway stay up the nice high quality writing, it is rare to see a nice weblog like this one nowadays..
آمبولانس خصوصی کرج
19 Sep 25 at 9:11 pm
https://evertrustmeds.shop/# Ever Trust Meds
RichardceaNy
19 Sep 25 at 9:13 pm
Wow, that’s what I was exploring for, what a material!
existing here at this blog, thanks admin of this web page.
LucraBot GPT
19 Sep 25 at 9:14 pm
Thanks a lot for sharing this with all people you
really realize what you’re speaking about!
Bookmarked. Please additionally seek advice from
my website =). We will have a hyperlink change contract between us
برای برونشیت ریه چی بخوریم
19 Sep 25 at 9:14 pm
Acho simplesmente fantastico o 888 Casino, proporciona uma experiencia de jogo eletrizante. As opcoes de jogos sao ricas e diversificadas, contendo titulos inovadores e atraentes. Os agentes estao sempre disponiveis e eficientes, garantindo ajuda imediata. Os ganhos sao pagos em tempo recorde, embora os bonus poderiam ser mais frequentes. No geral, o 888 Casino e indispensavel para os fas de emocoes fortes! Vale destacar o design e visualmente impactante, facilita cada sessao de jogo.
888 casino app apk|
Michaely7zef
19 Sep 25 at 9:14 pm
купить диплом в пскове [url=www.rudik-diplom1.ru/]купить диплом в пскове[/url] .
Diplomi_nrer
19 Sep 25 at 9:16 pm
диплом лесной колледж ухта купить [url=www.frei-diplom8.ru/]www.frei-diplom8.ru/[/url] .
Diplomi_vesr
19 Sep 25 at 9:16 pm
купить диплом стоматолога [url=www.rudik-diplom7.ru]купить диплом стоматолога[/url] .
Diplomi_poPl
19 Sep 25 at 9:17 pm
купить диплом в белово [url=https://www.rudik-diplom10.ru]https://www.rudik-diplom10.ru[/url] .
Diplomi_qgSa
19 Sep 25 at 9:18 pm