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!
J’adore la fraicheur de ViggoSlots Casino, ca vibre avec une energie de casino digne d’un blizzard. L’eventail de jeux du casino est une toundra de delices, offrant des sessions de casino en direct qui scintillent comme la neige. Les agents du casino sont rapides comme un vent de toundra, joignable par chat ou email. Les gains du casino arrivent a une vitesse polaire, quand meme des bonus de casino plus frequents seraient arctiques. Globalement, ViggoSlots Casino promet un divertissement de casino glacial pour les amoureux des slots modernes de casino ! En plus l’interface du casino est fluide et eclatante comme un glacier, donne envie de replonger dans le casino sans fin.
viggoslots auszahlung dauer|
zanyglitterwalrus4zef
15 Sep 25 at 5:59 am
ordina cipro generico senza ricetta medica
si puГІ acquistare cipro senza ricetta medica compri le compresse di cipro
15 Sep 25 at 6:04 am
купить диплом о среднем образовании с занесением в реестр [url=www.educ-ua15.ru]купить диплом о среднем образовании с занесением в реестр[/url] .
Diplomi_vmmi
15 Sep 25 at 6:05 am
We are a group of volunteers and opening a new scheme in our community.
Your website offered us with helpful information to work on. You’ve done an impressive process and our whole
community will probably be thankful to you.
scam
15 Sep 25 at 6:06 am
купить легально диплом [url=https://arus-diplom34.ru]купить легально диплом[/url] .
Diplomi_dyer
15 Sep 25 at 6:07 am
1вин. [url=https://www.1win12012.ru]https://www.1win12012.ru[/url]
1win_qnmr
15 Sep 25 at 6:07 am
Стоимость детоксикации от алкоголя в владимире варьируется в связи с различными аспектами. Нарколог на дом анонимно предлагает услуги по детоксикации и лечению при алкоголизме. Консультация нарколога поможет выбору оптимальной программы детокса. Стоимость на программы лечения могут включать стоимость анонимную детоксикацию и реабилитацию от алкоголя. Услуги нарколога, такие как поддержка пациентам с зависимостямитакже играют важную роль. В наркологической клинике владимир предлагают различные программыкоторые могут влиять на общую стоимость. Важно помнить о предупреждении алкоголизма и своевременной помощи.
alkogolizmvladimirNeT
15 Sep 25 at 6:10 am
cheap dilantin without prescription
where to get generic dilantin price
15 Sep 25 at 6:11 am
What’s up, of course this paragraph is in fact nice and I have learned lot of things from it regarding blogging.
thanks.
my site … تلفن نمایندگی بوش در تهران
تلفن نمایندگی بوش در تهران
15 Sep 25 at 6:12 am
Раскройте потенциал вашего кабинета с профессиональными решениями Cosmo Tech: диодные и пикосекундные лазеры, SMAS и RF-лифтинг, прессотерапия и другие топ-направления. Сертификация, обучение и сервис — в одном месте. Подробнее на https://cosmo-tech.ru Подберем оборудование под ваш бюджет и задачи, предложим рассрочку без банков, дадим гарантию и быстро запустим работу. Улучшайте качество услуг и повышайте средний чек — клиенты увидят эффект уже после первых процедур.
magezsmale
15 Sep 25 at 6:15 am
https://curabharatusa.shop/# best online pharmacy
CarlosPreom
15 Sep 25 at 6:17 am
мостбет войти в личный кабинет [url=https://mostbet12012.ru]мостбет войти в личный кабинет[/url]
mostbet_esSl
15 Sep 25 at 6:17 am
Hello there! This article could not be written much better!
Looking at this article reminds me of my previous roommate!
He continually kept talking about this. I’ll forward
this information to him. Fairly certain he’ll have
a great read. Thanks for sharing!
The Water Heater Warehouse Hot Water Heater Inspection company
15 Sep 25 at 6:18 am
of course like your website however you have to take a
look at the spelling on quite a few of your posts. Many of them are rife
with spelling issues and I to find it very bothersome to tell the truth however I’ll certainly come back again.
five.us.com lừa đảo công an truy quét cấm người chơi tham gia
15 Sep 25 at 6:21 am
кто купил диплом с занесением в реестр [url=www.educ-ua11.ru]кто купил диплом с занесением в реестр[/url] .
Diplomi_ujPi
15 Sep 25 at 6:22 am
обслуживание дизель генераторов
Rolandmow
15 Sep 25 at 6:23 am
CuraBharat USA: how to order medicine from india to usa – CuraBharat USA
Teddyroowl
15 Sep 25 at 6:25 am
TrueNorth Pharm [url=https://truenorthpharm.shop/#]TrueNorth Pharm[/url] canadian pharmacies
Michaelphype
15 Sep 25 at 6:26 am
1win букмекерская контора официальный сайт зеркало [url=http://1win12007.ru/]http://1win12007.ru/[/url]
1win_bkpn
15 Sep 25 at 6:26 am
mostbet com login [url=https://www.mostbet12012.ru]https://www.mostbet12012.ru[/url]
mostbet_imSl
15 Sep 25 at 6:29 am
aviator 1win игра [url=https://1win12012.ru]aviator 1win игра[/url]
1win_sfmr
15 Sep 25 at 6:30 am
диплом купить с занесением в реестр отзывы [url=https://arus-diplom34.ru]https://arus-diplom34.ru[/url] .
Diplomi_imer
15 Sep 25 at 6:30 am
This site was… how do I say it? Relevant!!
Finally I have found something which helped me.
Kudos!
AffinexisAgent
15 Sep 25 at 6:31 am
диплом реестр купить [url=https://educ-ua11.ru]диплом реестр купить[/url] .
Diplomi_ckPi
15 Sep 25 at 6:34 am
Mega darknet Мега даркнет Мега сайт Мега онион Мега ссылка Mega даркнет Mega сайт Mega онион Mega ссылка Mega darknet Mega onion
RichardPep
15 Sep 25 at 6:34 am
Узнайте всю информацию об площадке Авто-Фокус — большой каталог запасных частей, дополнительного оборудования и комплектующих для всех моделей! Посетите [url=https://avto-fokus.ru]https://avto-fokus.ru[/url] и ознакомьтесь доступные позиции. Нужна защита от угона или апгрейд звука? — интернет-ресурс предлагает оборудование безопасности, аудио-системы и аксессуары. Требуется комплектующая часть — здесь найдёте проверенные марки и хорошие условия заказа.
Spravkijha
15 Sep 25 at 6:35 am
My relatives all the time say that I am killing my time here at web, except I know I
am getting experience all the time by reading such
good articles or reviews.
Fatvim Weight Loss Formula
15 Sep 25 at 6:36 am
диплом с проведением купить [url=http://www.arus-diplom34.ru]диплом с проведением купить[/url] .
Diplomi_tler
15 Sep 25 at 6:37 am
купить диплом с занесением в реестры [url=https://educ-ua15.ru/]купить диплом с занесением в реестры[/url] .
Diplomi_ncmi
15 Sep 25 at 6:39 am
https://rylanshfz540.mystrikingly.com/
Enfrentar un control sorpresa puede ser un momento critico. Por eso, se desarrollo un metodo de enmascaramiento probada en laboratorios.
Su receta unica combina minerales, lo que sobrecarga tu organismo y neutraliza temporalmente los trazas de THC. El resultado: una prueba sin riesgos, lista para ser presentada.
Lo mas notable es su accion rapida en menos de 2 horas. A diferencia de detox irreales, no promete milagros, sino una solucion temporal que te respalda en situaciones criticas.
Miles de personas en Chile ya han experimentado su efectividad. Testimonios reales mencionan resultados exitosos en pruebas preocupacionales.
Si necesitas asegurar tu resultado, esta solucion te ofrece tranquilidad.
JuniorShido
15 Sep 25 at 6:40 am
купить аттестат цена [url=http://www.educ-ua18.ru]купить аттестат цена[/url] .
Diplomi_uuPi
15 Sep 25 at 6:41 am
Wow, awesome blog format! How lengthy have you been blogging for?
you make running a blog glance easy. The entire look of your web site
is excellent, let alone the content material!
https://win678.my/
15 Sep 25 at 6:44 am
Hello to every body, it’s my first visit of this web site; this weblog includes amazing and truly
excellent data in support of visitors.
webpage
15 Sep 25 at 6:47 am
кто купил диплом с занесением в реестр [url=https://educ-ua15.ru/]кто купил диплом с занесением в реестр[/url] .
Diplomi_cqmi
15 Sep 25 at 6:48 am
Нужна площадка, где вы быстро и бесплатно начнете продавать или искать нужные товары и услуги? https://razmestitobyavlenie.ru/ – современная бесплатная доска объявлений России, которая помогает частным лицам и бизнесу находить покупателей и клиентов без лишних затрат. Здесь вы можете подать объявление бесплатно и без регистрации, выбрать релевантную категорию из десятков рубрик и уже сегодня получить первые отклики.
alivatWaymn
15 Sep 25 at 6:51 am
Quality posts is the secret to attract the viewers to pay a visit the website, that’s what this web site is
providing.
Helder Flowdex
15 Sep 25 at 6:54 am
Wow, wonderful weblog layout! How lengthy have you ever been running
a blog for? you make running a blog look easy.
The whole glance of your web site is wonderful, as smartly as the content!
https://win678.nl/
15 Sep 25 at 6:56 am
mostbet.com казино скачать [url=https://mostbet12010.ru/]mostbet.com казино скачать[/url]
mostbet_bsPt
15 Sep 25 at 6:57 am
как зайти на сайт мостбет [url=www.mostbet12012.ru]www.mostbet12012.ru[/url]
mostbet_inSl
15 Sep 25 at 6:58 am
Купить диплом техникума в Ивано-Франковск [url=educ-ua7.ru]Купить диплом техникума в Ивано-Франковск[/url] .
Diplomi_cjEr
15 Sep 25 at 7:04 am
Mega darknet Мега даркнет Мега сайт Мега онион Мега ссылка Mega даркнет Mega сайт Mega онион Mega ссылка Mega darknet Mega onion
RichardPep
15 Sep 25 at 7:05 am
You really make it seem so easy along with your presentation but I find this matter to be actually something that I feel I would never understand.
It sort of feels too complicated and extremely broad for me.
I’m taking a look forward to your next submit,
I’ll attempt to get the cling of it!
Here is my blog; کادوساز
کادوساز
15 Sep 25 at 7:06 am
Hey there! Someone in my Facebook group shared this site
with us so I came to check it out. I’m definitely loving the information. I’m bookmarking
and will be tweeting this to my followers! Fantastic blog and excellent design and style.
WIN678
15 Sep 25 at 7:06 am
Мега даркнет Мега даркнет Мега сайт Мега онион Мега ссылка Mega даркнет Mega сайт Mega онион Mega ссылка Mega darknet Mega onion
RichardPep
15 Sep 25 at 7:07 am
купил диплом легально [url=www.arus-diplom34.ru]купил диплом легально[/url] .
Diplomi_rger
15 Sep 25 at 7:08 am
Feringer.shop — специализированный магазин печей для бань и саун с официальной гарантией и доставкой по РФ. В каталоге — решения для русской бани, финской сауны и хаммама, с каменной облицовкой и VORTEX для стремительного прогрева. Перейдите на сайт https://feringer.shop/ для выбора печи и консультации. Ждём в московском шоу руме: образцы в наличии и помощь в расчёте мощности.
GijizeHug
15 Sep 25 at 7:08 am
зайти в мостбет [url=https://mostbet12013.ru/]https://mostbet12013.ru/[/url]
mostbet_nika
15 Sep 25 at 7:09 am
Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any feed-back would be greatly appreciated.
WIN678
15 Sep 25 at 7:11 am
https://jlt-dubai.ae/
DerekneolE
15 Sep 25 at 7:13 am
Jumeirah Lakes Towers
DerekneolE
15 Sep 25 at 7:16 am