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!
I have read so many content about the blogger lovers
but this piece of writing is truly a nice paragraph, keep it up.
دانشگاه غیرانتفاعی علم و فرهنگ
18 Sep 25 at 7:54 pm
Hi there, I read your new stuff like every week.
Your story-telling style is awesome, keep it up!
Rembrandt Roofing & Restoration roof replacement company
18 Sep 25 at 7:54 pm
Наркологическая клиника в Донецке оказывает профессиональную помощь людям, страдающим алкогольной и наркотической зависимостью. Лечение в таких учреждениях направлено на комплексное восстановление здоровья, включающее медицинскую детоксикацию, психотерапевтическую поддержку и социальную адаптацию. Важным принципом является индивидуальный подход к каждому пациенту, что позволяет добиться устойчивых результатов и снизить риск рецидива.
Узнать больше – [url=https://narkologicheskaya-klinika-v-doneczke0.ru/]анонимная наркологическая клиника[/url]
Dallasvam
18 Sep 25 at 7:56 pm
Hi to every body, it’s my first pay a quick visit of this webpage;
this website contains remarkable and actually fine data
Westwood Law Probate Lawyer in Utah support of readers.
Westwood Law Probate Lawyer in Utah
18 Sep 25 at 7:56 pm
What’s up to every one, it’s really a pleasant for me to pay a visit this website,
it contains helpful Information.
казино Аркада официальный сайт
18 Sep 25 at 7:57 pm
I just couldn’t depart your web site prior to suggesting that I actually enjoyed the usual info an individual provide on your guests?
Is going to be back ceaselessly to investigate cross-check new posts
online casino nederland legaal
18 Sep 25 at 7:57 pm
I’m amazed, I must say. Seldom do I come across a blog that’s equally
educative and amusing, and let me tell you,
you have hit the nail on the head. The issue is something too few people are speaking intelligently about.
Now i’m very happy that I found this during my hunt for something regarding this.
آدرس دانشگاه شریف درب اصلی
18 Sep 25 at 8:01 pm
coke in prague buy mdma prague
prague-drugs-482
18 Sep 25 at 8:01 pm
Howdy, I think your website could possibly be having browser compatibility issues.
When I take a look at your website in Safari,
it looks fine however, if opening in Internet Explorer, it has some overlapping
issues. I just wanted to provide you with a quick heads up!
Aside from that, fantastic website!
homepage
18 Sep 25 at 8:04 pm
prague drugs https://cocaine-prague-shop.com
prague-drugs-113
18 Sep 25 at 8:05 pm
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
bs2web at
bs2best.at blacksprut Official
Jamesner
18 Sep 25 at 8:05 pm
Article Marketing Systedm That Rocks article – https://www.bitsdujour.com/Profiles/cyrpmf –
https://www.bitsdujour.com/Profiles/cyrpmf
18 Sep 25 at 8:06 pm
bs2best at, bs2web at и bs2 market: глубокий анализ технологий 2025 года
blsp at
bs2best.at blacksprut marketplace Official
CharlesNarry
18 Sep 25 at 8:08 pm
CPL (Cost Per Lead) https://cost-per-lead1.ru ключевая метрика рекламы. Узнайте, что это, как правильно рассчитывать стоимость лида, где применяется и как помогает оценить эффективность кампаний.
Nathanvep
18 Sep 25 at 8:08 pm
CPL (Cost Per Lead) https://cost-per-lead1.ru ключевая метрика рекламы. Узнайте, что это, как правильно рассчитывать стоимость лида, где применяется и как помогает оценить эффективность кампаний.
Nathanvep
18 Sep 25 at 8:10 pm
buy weed prague cocaine prague
prague-drugs-987
18 Sep 25 at 8:10 pm
coke in prague https://cocaine-prague-shop.com
prague-drugs-655
18 Sep 25 at 8:11 pm
CPL (Cost Per Lead) https://cost-per-lead1.ru ключевая метрика рекламы. Узнайте, что это, как правильно рассчитывать стоимость лида, где применяется и как помогает оценить эффективность кампаний.
Nathanvep
18 Sep 25 at 8:11 pm
Hi there, just became alert to your blog through Google, and found that
it is really informative. I’m going to watch out for brussels.
I will be grateful if you continue this in future.
A lot of people will be benefited from your writing.
Cheers!
Boostaro
18 Sep 25 at 8:11 pm
Thank you for the auspicious writeup. It in fact
was a amusement account it. Look advanced to more
added agreeable from you! However, how can we communicate?
CO
18 Sep 25 at 8:11 pm
Aw, this was a really nice post. Taking a few minutes and actual effort to generate a good article… but what can I say…
I hesitate a whole lot and never manage to get nearly anything done.
Feel free to surf to my homepage; serviced apartments business stays
serviced apartments business stays
18 Sep 25 at 8:14 pm
https://wirtube.de/a/strightlrnybraden/video-channels
Timothytag
18 Sep 25 at 8:15 pm
все займы на карту [url=https://www.zaimy-14.ru]все займы на карту[/url] .
zaimi_ozSr
18 Sep 25 at 8:18 pm
https://gregoryahmss.blogminds.com/la-%C3%BAltima-gu%C3%ADa-a-sesiones-de-coaching-de-equipos-29232692
La importancia del coaching de equipos esta revolucionando la forma en que las organizaciones latinas lideran sus areas.
Hoy, apostar por un programa de coaching de equipos no es un beneficio, sino una herramienta critica para lograr resultados en contextos cada vez mas complejos.
?Por que elegir coaching para grupos?
Fortalece la conexion entre colaboradores.
Previene tensiones internos.
Optimiza la coordinacion en procesos.
Impulsa apoyo mutuo dentro del equipo.
Ventajas de un coaching de equipos
Grupos mas coordinados con los metas organizacionales.
Disminucion de rotacion.
Cultura laboral saludable.
Mas alta prevencion de disputas.
Casos donde el coaching de equipos para resolver conflictos hace la gran diferencia
Departamentos con tensiones entre lideres.
Equipos que funcionan en formato hibrido.
Empresas que padecen burnout colectivo.
De que manera implementar programa de coaching de equipos en tu organizacion
Precisar resultados buscados.
Contratar un coach especializado.
Implementar programas hibridos adaptados a las necesidades.
Monitorear el cambio en tiempos claros.
El coaching para grupos es un salvavidas que fortalece la forma de colaborar. Un coaching grupal online bien aplicado puede convertirse en la clave entre sobrevivir o escalar.
JuniorShido
18 Sep 25 at 8:24 pm
I have been browsing on-line greater than 3 hours today,
but I never found any fascinating article like yours.
It is lovely price enough for me. In my view, if all website owners and bloggers made just right content as you
did, the internet will likely be much more helpful than ever before.
Aquarium supplies for families
18 Sep 25 at 8:24 pm
Банкротство для СВО без штрафов — новость 2025, списали долг 500 тысяч, спасибо закону. военный портал
Brentagila
18 Sep 25 at 8:29 pm
Experience the ultimate relaxation with [url=https://www.hisomassage.com]Hiso Massage[/url]
offering professiona
DonaldLic
18 Sep 25 at 8:29 pm
Magnificent goods from you, man. I’ve consider your
stuff prior to and you’re simply extremely excellent.
I actually like what you’ve obtained here, really
like what you’re saying and the best way by which you say it.
You are making it enjoyable and you still care for to
keep it smart. I cant wait to learn much more from you.
This is actually a tremendous website.
آدرس دانشگاه پیام نور مرکز تهران شرق
18 Sep 25 at 8:30 pm
This is my first time go to see at here and i am in fact
impressed to read all at alone place.
slot gacor
18 Sep 25 at 8:32 pm
It’s very easy to find out any matter on net as compared to books,
as I found this article at this web page.
bästa casino online
18 Sep 25 at 8:35 pm
What’s up to every one, it’s really a fastidious
for me to pay a quick visit this website, it contains useful Information.
educational character video generator
18 Sep 25 at 8:36 pm
I am really loving the theme/design of your website.
Do you ever run into any web browser compatibility issues?
A couple of my blog audience have complained about
my blog not operating correctly in Explorer but looks great in Safari.
Do you have any advice to help fix this issue?
meilleur casino en ligne
18 Sep 25 at 8:36 pm
http://www.pageorama.com/?p=jeebacocic
Timothytag
18 Sep 25 at 8:41 pm
Does your website have a contact page? I’m having trouble locating it but, I’d
like to shoot you an email. I’ve got some ideas for your blog you might be interested in hearing.
Either way, great website and I look forward to seeing it improve over time.
سامانه گزینش و استخدام کارا
18 Sep 25 at 8:43 pm
Excellent items from you, man. I’ve have in mind your stuff
prior to and you are just extremely fantastic.
I really like what you’ve bought here, really like what you’re stating and the way
in which during which you are saying it. You are making it enjoyable and you still take care of to keep it sensible.
I can’t wait to learn much more from you.
That is really a terrific site.
Prompt Aloxi Xr
18 Sep 25 at 8:43 pm
Рабочее зеркало представляет собой ссылку, которая перенаправляет на точную копию сайта.
рояль россия официальный сайт
18 Sep 25 at 8:44 pm
If some one desires expert view on the topic of running a blog afterward
i recommend him/her to go to see this web site, Keep up the nice work.
cat
18 Sep 25 at 8:45 pm
I think this is one of the most vital information for me.
And i am glad reading your article. But want to remark on some general
things, The site style is wonderful, the articles is really great : D.
Good job, cheers
Download Digital Holography and Wavefront Sensing (Principles
18 Sep 25 at 8:46 pm
Hello there! I know this is kind of off topic but I was wondering if you knew
where I could get a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having
problems finding one? Thanks a lot!
Here is my web-site – solar farm Auckland
solar farm Auckland
18 Sep 25 at 8:47 pm
Скачать рапорт бесплатно на досрочный отпуск — удобно, без воды, прям в тему. разделы для военнослужащих
Brentagila
18 Sep 25 at 8:47 pm
У нас https://teamfun.ru/ огромный ассортимент аттракционов для проведения праздников, фестивалей, мероприятий (день города, день поселка, день металлурга, день строителя, праздник двора, 1 сентября, день защиты детей, день рождения, корпоратив, тимбилдинг и т.д.). Аттракционы от нашей компании будут хитом и отличным дополнением любого праздника! Мы умеем радовать детей и взрослых!
Dygyberagoge
18 Sep 25 at 8:48 pm
I have been exploring for a little bit for any high-quality articles or blog posts
on this sort of house . Exploring in Yahoo I eventually stumbled upon this site.
Reading this info So i’m satisfied to convey that I’ve an incredibly excellent uncanny feeling I came upon exactly what I needed.
I such a lot no doubt will make certain to don?t forget this site and give it a look on a
relentless basis.
Eldros Lutharis
18 Sep 25 at 8:48 pm
This info is invaluable. Where can I find out more?
روش تقلب در امتحان تستی انلاین نی نی سایت
18 Sep 25 at 8:49 pm
This is very interesting, You are a very skilled blogger.
I’ve joined your rss feed and look forward to seeking more of your magnificent post.
Also, I’ve shared your site in my social networks!
Download Wavelets in Neuroscience (2nd Edition) Hramov PDF
18 Sep 25 at 8:50 pm
щетки
щетки
18 Sep 25 at 8:51 pm
For most recent information you have to pay a quick visit the web and on web I found this web site as a best site for most up-to-date updates.
Feel free to visit my web page … ลอตโต้อัพ
ลอตโต้อัพ
18 Sep 25 at 8:52 pm
карниз раздвижной [url=http://www.razdvizhnoj-elektrokarniz.ru]http://www.razdvizhnoj-elektrokarniz.ru[/url] .
razdvijnoi elektrokarniz_muei
18 Sep 25 at 8:52 pm
Mikigaming selalu menjadi pilihan utama bagi para pecinta slot online Pragmatic Play yang menginginkan pengalaman bermain terbaik
dan peluang kemenangan yang menjanjikan.
Mikigaming
18 Sep 25 at 8:54 pm
Выбор качественного металлопроката требует
внимания к ряду параметров
источник
18 Sep 25 at 8:55 pm
https://business27383.tblogz.com/los-principios-b%C3%A1sicos-de-coaching-ejecutivo-45320612
La relevancia del coaching ejecutivo está cambiando la metodología en que las compañías latinas gestionan a sus trabajadores.
Hoy, hablar de coaching organizacional no es una corriente pasajera, es una clave crítica para conseguir resultados en un contexto cada vez más competitivo.
Razones para el coaching organizacional impacta?
Ayuda a los jefes a manejar eficazmente su tiempo.
Mejora la comunicación con equipos.
Consolida el management en etapas de cambio.
Previene el cansancio en ejecutivos.
Beneficios del coaching jefaturas en Chile
Mayor retención de colaboradores.
Clima laboral sano.
Áreas coordinados con los metas organizacionales.
Formación de supervisores que asumen nuevas funciones.
Ejemplos donde el coaching jefaturas marca la gran diferencia
Un gerente que necesita negociar conflictos con stakeholders.
Una jefatura que le toca manejar equipos multigeneracionales.
Un ejecutivo que se enfrenta un cambio de reestructuración.
De qué manera implementar coaching organizacional en tu compañía
Definir metas alcanzables.
Contratar un coach certificado.
Crear programas adaptados.
Medir cambios en tiempos específicos.
Un plan de coaching jefaturas puede ser la diferencia entre improvisar o escalar.
JuniorShido
18 Sep 25 at 8:55 pm