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!
flmo1xt – Clean design makes it simple, everything looks neat and sharp.
Ezra Mate
3 Oct 25 at 6:16 pm
купить диплом института с реестром [url=www.frei-diplom4.ru]купить диплом института с реестром[/url] .
Diplomi_vdOl
3 Oct 25 at 6:16 pm
купить медицинский диплом медсестры [url=www.frei-diplom15.ru]купить медицинский диплом медсестры[/url] .
Diplomi_gaoi
3 Oct 25 at 6:16 pm
купить диплом в великом новгороде [url=www.rudik-diplom8.ru/]купить диплом в великом новгороде[/url] .
Diplomi_hlMt
3 Oct 25 at 6:18 pm
купить диплом врача [url=rudik-diplom15.ru]купить диплом врача[/url] .
Diplomi_yePi
3 Oct 25 at 6:19 pm
купить диплом в железногорске [url=www.rudik-diplom7.ru/]www.rudik-diplom7.ru/[/url] .
Diplomi_ubPl
3 Oct 25 at 6:19 pm
купить диплом с занесением в реестр цена [url=http://www.frei-diplom2.ru]купить диплом с занесением в реестр цена[/url] .
Diplomi_rbEa
3 Oct 25 at 6:20 pm
купить диплом колледжа [url=https://educ-ua7.ru/]https://educ-ua7.ru/[/url] .
Diplomi_ueea
3 Oct 25 at 6:20 pm
https://fdcexpress.ru
PatrickGop
3 Oct 25 at 6:21 pm
лучшие прогнозы на футбол [url=https://prognozy-na-futbol-10.ru]https://prognozy-na-futbol-10.ru[/url] .
prognozi na fytbol_cdOi
3 Oct 25 at 6:21 pm
Everyone loves what you guys tend to be up too.
This sort of clever work and coverage! Keep up the great
works guys I’ve you guys to blogroll.
слот-квест
3 Oct 25 at 6:22 pm
Заказывали раскрутку интернет сайта под Google и Яндекс, потому что в нашей сфере большая конкуренция. Команда подошла к делу комплексно: техническая оптимизация, контент и внешние ссылки. Результаты начали появляться постепенно, а сейчас сайт стабильно в топе по ключевым запросам. Очень довольны сотрудничеством и планируем продолжать дальше, https://mihaylov.digital/
Steventob
3 Oct 25 at 6:22 pm
Toutefois, utiliser ces ressources peut enlever une partie de la magie de
la chasse.
Korey
3 Oct 25 at 6:23 pm
купить диплом мастера маникюра и педикюра [url=http://rudik-diplom5.ru/]купить диплом мастера маникюра и педикюра[/url] .
Diplomi_kpma
3 Oct 25 at 6:24 pm
купить диплом агронома [url=www.rudik-diplom4.ru]купить диплом агронома[/url] .
Diplomi_gdOr
3 Oct 25 at 6:24 pm
прогнозы на футбол [url=https://prognozy-na-futbol-10.ru/]прогнозы на футбол[/url] .
prognozi na fytbol_anOi
3 Oct 25 at 6:25 pm
sildenafil: Buy sildenafil – sildenafil
BruceMaivy
3 Oct 25 at 6:26 pm
купить диплом с реестром в москве [url=frei-diplom5.ru]купить диплом с реестром в москве[/url] .
Diplomi_irPa
3 Oct 25 at 6:26 pm
купить диплом ветеринара [url=http://rudik-diplom11.ru]купить диплом ветеринара[/url] .
Diplomi_yxMi
3 Oct 25 at 6:26 pm
Заказывал продвижение сайта в поисковых системах и остался доволен. Всё сделали прозрачно, регулярно предоставляют отчёты и результаты. Сайт действительно начал приносить прибыль, https://mihaylov.digital/
Steventob
3 Oct 25 at 6:27 pm
После диагностики начинается активная фаза капельничного лечения. Современные препараты вводятся с помощью автоматизированных систем дозирования, что обеспечивает быстрое снижение уровня токсинов в крови и восстановление обменных процессов. Этот этап направлен на стабилизацию работы печени, почек и сердечно-сосудистой системы.
Подробнее тут – https://kapelnica-ot-zapoya-lugansk-lnr00.ru/kapelnicza-ot-zapoya-czena-lugansk-lnr/
Scottnal
3 Oct 25 at 6:27 pm
купить диплом в москве с занесением в реестр [url=frei-diplom1.ru]купить диплом в москве с занесением в реестр[/url] .
Diplomi_eiOi
3 Oct 25 at 6:27 pm
прогнозы на футбол на сегодня [url=https://prognozy-na-futbol-10.ru/]прогнозы на футбол на сегодня[/url] .
prognozi na fytbol_vqOi
3 Oct 25 at 6:27 pm
купить диплом ижевск с занесением в реестр [url=https://frei-diplom6.ru]купить диплом ижевск с занесением в реестр[/url] .
Diplomi_zsOl
3 Oct 25 at 6:27 pm
купить диплом в перми [url=www.rudik-diplom2.ru]купить диплом в перми[/url] .
Diplomi_ncpi
3 Oct 25 at 6:28 pm
купить диплом о техническом образовании с занесением в реестр [url=www.frei-diplom4.ru]www.frei-diplom4.ru[/url] .
Diplomi_biOl
3 Oct 25 at 6:28 pm
Lucky Mate is an online casino for Australian players, offering pokies, table games, and live dealer options. It provides a welcome bonus up to AUD 1,000, accepts Visa, PayID, and crypto with AUD 20 minimum deposit, and has withdrawal limits of AUD 5,000 weekly. Licensed, it promotes safe play https://harmonise.org.uk/fundraising/discovering-exciting-poker-and-pokie-games-at-lucky-mate-casino-today/
Edwardfrevy
3 Oct 25 at 6:30 pm
купить диплом в петрозаводске [url=http://www.rudik-diplom8.ru]купить диплом в петрозаводске[/url] .
Diplomi_wkMt
3 Oct 25 at 6:30 pm
Detivetra Кайт школа – это специализированное учебное заведение, предлагающее курсы обучения кайтсерфингу для начинающих и продвинутых райдеров. В кайт школах работают сертифицированные инструкторы, которые обучают основам управления кайтом, технике безопасности, правилам поведения на воде и основам кайтсерфинга. Кайт школы предоставляют все необходимое оборудование для обучения, включая кайты, доски, гидрокостюмы и шлемы.
Terryloogy
3 Oct 25 at 6:30 pm
купить диплом в троицке [url=https://rudik-diplom3.ru/]https://rudik-diplom3.ru/[/url] .
Diplomi_hwei
3 Oct 25 at 6:30 pm
wcbxhmsdo8nr – Navigation is straightforward, I didn’t run into any confusion.
Deadra Machain
3 Oct 25 at 6:31 pm
aaccc2 – The website opened without issues, loading fast and smooth.
Wendi Napoles
3 Oct 25 at 6:33 pm
купить диплом логиста [url=rudik-diplom4.ru]купить диплом логиста[/url] .
Diplomi_jbOr
3 Oct 25 at 6:34 pm
Ищешь аккумулятор? купить аккумулятор для авто в спб AKB SHOP занимает лидирующие позиции среди интернет-магазинов автомобильных аккумуляторов в Санкт-Петербурге. Наш ассортимент охватывает все категории транспортных средств. Независимо от того, ищете ли вы надёжный аккумулятор для легкового автомобиля, мощного грузовика, комфортного катера, компактного скутера, современного погрузчика или специализированного штабелёра
akb-shop-m
3 Oct 25 at 6:34 pm
With havin so much content and articles do you ever run into any issues of plagorism or copyright
violation? My site has a lot of exclusive content I’ve either authored
myself or outsourced but it appears a lot of it is popping it up all over the
internet without my agreement. Do you know any solutions to help prevent content from being ripped off?
I’d really appreciate it.
ketbilietai
3 Oct 25 at 6:34 pm
На [url=https://www.igor-scherbakov.ru]сайте Игоря Щерbakова[/url] каждый раздел это окно в мир профессионального искусства. Здесь переплетаются музыка, слово и изображение, создавая целостную картину творческой личности. Посетители могут не только ознакомиться с работами, но и понять философию их создания.
TimothyWap
3 Oct 25 at 6:34 pm
Инфузионная терапия подбирается индивидуально: растворы для регидратации и детоксикации, гепатопротекторы, кардиопротекторы, витамины и микроэлементы. Детокс — это не только «капельница», но и мониторинг жизненно важных показателей, оценка неврологического статуса, профилактика осложнений. Мы отслеживаем динамику самочувствия, корректируем дозировки и при необходимости подключаем дополнительные обследования. Задача — мягко вывести токсины, вернуть ясность сознания и работоспособность без резких «качелей».
Изучить вопрос глубже – http://narkologicheskaya-klinika-balashiha0.ru/anonimnaya-narkologicheskaya-klinika-v-balashihe/
Eduardofug
3 Oct 25 at 6:34 pm
купить проведенный диплом весь [url=http://frei-diplom5.ru]купить проведенный диплом весь[/url] .
Diplomi_pdPa
3 Oct 25 at 6:35 pm
https://tadalmedspharmacy.com/# tadalafil
Williamjib
3 Oct 25 at 6:35 pm
купить диплом в сызрани [url=https://www.rudik-diplom5.ru]купить диплом в сызрани[/url] .
Diplomi_qmma
3 Oct 25 at 6:36 pm
Казино ПинАп слот Fruits Royale
TommyCap
3 Oct 25 at 6:36 pm
купить диплом лаборанта [url=https://www.rudik-diplom2.ru]купить диплом лаборанта[/url] .
Diplomi_wvpi
3 Oct 25 at 6:36 pm
купить диплом о высшем образовании легально [url=www.frei-diplom1.ru]купить диплом о высшем образовании легально[/url] .
Diplomi_cfOi
3 Oct 25 at 6:36 pm
360view – Great overall impression, definitely worth coming back again soon.
Andre Kirchen
3 Oct 25 at 6:36 pm
легально купить диплом [url=www.frei-diplom4.ru/]легально купить диплом[/url] .
Diplomi_twOl
3 Oct 25 at 6:36 pm
купить диплом университета с занесением в реестр [url=frei-diplom6.ru]купить диплом университета с занесением в реестр[/url] .
Diplomi_kpOl
3 Oct 25 at 6:36 pm
Нужен надежный акб? магазин аккумуляторы в петербурге AKB STORE — ведущий интернет-магазин автомобильных аккумуляторов в Санкт-Петербурге! Мы специализируемся на продаже качественных аккумуляторных батарей для самой разнообразной техники. В нашем каталоге вы найдёте идеальные решения для любого транспортного средства: будь то легковой или грузовой автомобиль, катер или лодка, скутер или мопед, погрузчик или штабелер.
akb-store-508
3 Oct 25 at 6:37 pm
купить диплом хореографа [url=https://rudik-diplom8.ru]купить диплом хореографа[/url] .
Diplomi_kdMt
3 Oct 25 at 6:37 pm
легально купить диплом о [url=https://frei-diplom2.ru/]легально купить диплом о[/url] .
Diplomi_hbEa
3 Oct 25 at 6:37 pm
true vital meds [url=https://truevitalmeds.shop/#]true vital meds[/url] Buy sildenafil online usa
TimothyArrar
3 Oct 25 at 6:38 pm