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!
https://t.me/s/Ud_KEnT
MichaelPione
1 Nov 25 at 7:54 pm
https://t.me/s/uD_leoN
MichaelPione
1 Nov 25 at 7:55 pm
Hmm, whɑt iѕ this thing? Not rеally sure. Totally not reⅼated tօ anything.
Anyway tһanks.
My web site; dangerous site
dangerous site
1 Nov 25 at 7:56 pm
В Ростове-на-Дону клиника «ЧСП№1» предлагает профессиональный вывод из запоя. Услуга доступна на дому и в стационаре, а также включает капельницу от похмелья. Все процедуры проводятся анонимно и круглосуточно.
Узнать больше – [url=https://vyvod-iz-zapoya-rostov25.ru/]вывод из запоя с выездом в ростове-на-дону[/url]
Donnienow
1 Nov 25 at 7:57 pm
если вы желаете предоставить номер мобильного через в тендерах, розыгрышах или программах лояльности, [url=https://norefer.com/to/https://rozovajapantera.ru/arenda-virtualnogo-nomera-telefona-sovremennoe-reshenie-dlya-biznesa-i-lichnyh-zadach]https://norefer.com/to/https://rozovajapantera.ru/arenda-virtualnogo-nomera-telefona-sovremennoe-reshenie-dlya-biznesa-i-lichnyh-zadach[/url] купить временную симку точно стоит.
LisaDox
1 Nov 25 at 8:00 pm
скачать казино мостбет [url=http://mostbet12034.ru]http://mostbet12034.ru[/url]
mostbet_kg_dcPr
1 Nov 25 at 8:00 pm
Uk Meds Guide [url=https://ukmedsguide.shop/#]legitimate pharmacy sites UK[/url] best UK pharmacy websites
Hermanengam
1 Nov 25 at 8:01 pm
скачать мостбет кг [url=www.mostbet12034.ru]скачать мостбет кг[/url]
mostbet_kg_ybPr
1 Nov 25 at 8:02 pm
SafeMedsGuide: best pharmacy sites with discounts – best pharmacy sites with discounts
HaroldSHems
1 Nov 25 at 8:03 pm
top rated seo [url=http://reiting-seo-agentstv.ru]http://reiting-seo-agentstv.ru[/url] .
reiting seo agentstv_kvsa
1 Nov 25 at 8:04 pm
Undeniably believe that which you said. Your favorite justification appeared to be on the web the simplest thing to be
aware of. I say to you, I definitely get irked while people consider worries
that they plainly do not know about. You managed to
hit the nail upon the top and defined out the whole thing without having side
effect , people could take a signal. Will probably be back to get
more. Thanks
시알리스 구매 사이트
1 Nov 25 at 8:08 pm
Fastidious replies in return of this matter with real arguments and
telling all about that.
Also visit my web blog: http://www.roelabogados.Com
www.roelabogados.Com
1 Nov 25 at 8:08 pm
Je suis enthousiaste a propos de Ruby Slots Casino, ca pulse comme une soiree animee. Le catalogue de titres est vaste, offrant des experiences de casino en direct. Avec des transactions rapides. Le service client est de qualite. Les gains arrivent en un eclair, bien que plus de promotions frequentes boosteraient l’experience. Pour finir, Ruby Slots Casino est un endroit qui electrise. Pour completer la plateforme est visuellement captivante, apporte une touche d’excitation. Un atout les evenements communautaires engageants, garantit des paiements securises.
Ouvrir la page|
cybercodeon9zef
1 Nov 25 at 8:11 pm
TG @‌LINKS_DEALER | EFFECTIVE SEO LINKS FOR SPINBETTERAPP.COM
JamesTum
1 Nov 25 at 8:13 pm
shopthenexttrend – The collections feel curated and well-thought-out, liked that.
Ruben Bushner
1 Nov 25 at 8:14 pm
Обработка обработка от клопов частного дома эффективная, рекомендую.
санобработка
Wernermog
1 Nov 25 at 8:14 pm
online pharmacy australia: best Australian pharmacies – Australian pharmacy reviews
Johnnyfuede
1 Nov 25 at 8:14 pm
bestchangeru.com — Надежный Обменник Валют Онлайн
[url=https://bestchangeru.com/]bestchange обменник[/url]
Что такое BestChange?
bestchangeru.com является одним из наиболее популярных сервисов мониторинга обменников электронных валют в русскоязычном сегменте сети Интернет. Платформа была создана для упрощения процесса выбора надежного онлайн-обмена валюты среди множества предложений.
https://bestchangeru.com/
бестчендж обменник
Основные преимущества BestChange:
– Мониторинг лучших курсов: Лучшие курсы покупки и продажи криптовалют и электронных денег автоматически обновляются в режиме реального времени.
– Автоматическое сравнение: Удобный интерфейс позволяет мгновенно сравнить десятки предложений и выбрать оптимальное.
– Обзор отзывов пользователей: Пользователи оставляют отзывы и оценки, помогающие другим пользователям принять решение.
– Отсутствие скрытых комиссий: Информация о комиссиях отображается прозрачно и открыто.
¦ Как работает BestChange?
Пользователь вводит необходимые данные: валюту, которую хочет обменять, и желаемую сумму. После этого сервис генерирует список надежных обменных пунктов с лучшими условиями обмена.
Пример: Вы хотите обменять Bitcoin на рубли. Заходите на сайт bestchangeru.com, выбираете направление обмена («Bitcoin > Рубли»), вводите сумму и получаете таблицу проверенных обменных пунктов с наилучшими курсами.
¦ Почему выбирают BestChange?
1. Безопасность. Все обменники проходят строгую проверку перед добавлением в базу сервиса.
2. Удобство пользования. Простота интерфейса позволяет быстро находить нужную информацию даже новичкам.
3. Постоянное обновление базы данных. Курсы и условия регулярно проверяются и обновляются, обеспечивая актуальность информации.
4. Многоязычность. Помимо русского, доступна версия сайта на английском и украинском языках.
Таким образом, bestchangeru.com становится незаменимым помощником в мире цифровых финансов, позволяя легко и безопасно совершать операции обмена валют. Если вам нужен надежный и удобный способ обмена криптовалюты и электронных денег, обязательно обратите внимание на этот ресурс.
JamesHam
1 Nov 25 at 8:15 pm
Наши специалисты в Ростове-на-Дону обеспечат быстрое и безопасное восстановление после длительного употребления алкоголя, используя индивидуальные схемы лечения.
Ознакомиться с деталями – [url=https://vyvod-iz-zapoya-rostov117.ru/]вывод из запоя капельница в ростове-на-дону[/url][url=https://vyvod-iz-zapoya-rostov117.ru/]вывод из запоя недорого в ростове-на-дону[/url]
Anthonysmima
1 Nov 25 at 8:15 pm
Je suis totalement conquis par Sugar Casino, ca offre un plaisir vibrant. La bibliotheque de jeux est captivante, proposant des jeux de table sophistiques. Le bonus d’inscription est attrayant. Le support est fiable et reactif. Les paiements sont securises et instantanes, bien que des bonus diversifies seraient un atout. En conclusion, Sugar Casino offre une aventure inoubliable. Par ailleurs le design est moderne et energique, ce qui rend chaque session plus palpitante. Particulierement interessant le programme VIP avec des niveaux exclusifs, qui motive les joueurs.
DГ©couvrir davantage|
wilddripin6zef
1 Nov 25 at 8:17 pm
Скульптурно-пластическая коррекция лица: возвращение
свежести без инвазивных процедур
https://manual-plus.ru
1 Nov 25 at 8:17 pm
J’adore la vibe de Sugar Casino, c’est une plateforme qui pulse avec energie. On trouve une gamme de jeux eblouissante, offrant des sessions live palpitantes. Avec des transactions rapides. Les agents repondent avec rapidite. Les retraits sont lisses comme jamais, de temps a autre plus de promotions frequentes boosteraient l’experience. Pour finir, Sugar Casino est un incontournable pour les joueurs. En plus la navigation est intuitive et lisse, booste l’excitation du jeu. Particulierement cool les tournois reguliers pour la competition, qui stimule l’engagement.
DГ©couvrir la page|
stormsparkus2zef
1 Nov 25 at 8:20 pm
Каждый пациент проходит последовательный процесс лечения, где основное внимание уделяется безопасности, контролю состояния и постепенной адаптации. При поступлении проводится диагностика с оценкой степени зависимости, сопутствующих патологий и психологических факторов, влияющих на развитие заболевания. На основании полученных данных формируется персонализированный план терапии, включающий медикаментозные и немедикаментозные методы.
Узнать больше – [url=https://narkologicheskaya-klinika-v-spb16.ru/]наркологическая клиника цены[/url]
MichaelUninc
1 Nov 25 at 8:20 pm
Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the
website is very good.
سئوکار سایت فروشگاهی در تهران
1 Nov 25 at 8:24 pm
top-rated pharmacies in Ireland
Edmundexpon
1 Nov 25 at 8:24 pm
https://joyrulez.com/blogs/195226/Code-Promo-1xBet-2026-Bonus-1950-150-Tours-Gratuits
https://joyrulez.com/blogs/195226/Code-Promo-1xBet-2026-Bonus-1950-150-Tours-Gratuits
1 Nov 25 at 8:28 pm
seo топ 10 [url=https://reiting-seo-agentstv.ru/]https://reiting-seo-agentstv.ru/[/url] .
reiting seo agentstv_hbsa
1 Nov 25 at 8:29 pm
https://aboutnursepractitionerjobs.com/author/1xbetwelcomecodetz/
JeffreyAmize
1 Nov 25 at 8:30 pm
Портал про все https://version.com.ua новини, технології, здоров’я, будинок, авто, подорожі, фінанси та кар’єра. Щоденні статті, огляди, лайфхаки та інструкції. Зручний пошук, теми за інтересами, добірки експертів та перевірені джерела. Читайте, навчайтеся, заощаджуйте час.
version-986
1 Nov 25 at 8:30 pm
скачать бк осталось всего только выбрать подходящее [url=www.mostbet12033.ru]www.mostbet12033.ru[/url]
mostbet_kg_yjpa
1 Nov 25 at 8:32 pm
chery pro max chery tiggo 8 pro max
official-chery-614
1 Nov 25 at 8:32 pm
Sizi gecmisten ilham alan guzellik ipuclar?yla dolu bu nostaljik yolculuga davet ediyoruz. 90’lar modas?n?n s?rlar?na goz atal?m m??
Особенно понравился раздел про Ev Dekorasyonunda S?kl?k ve Fonksiyonellik.
Вот, можете почитать:
[url=https://evimsicak.com]https://evimsicak.com[/url]
Guzellik, gecmisle gunumuz aras?nda bir kopru kurmakt?r. 90’lar?n moda s?rlar?, bu koprunun onemli parcalar?ndan.
Josephassof
1 Nov 25 at 8:34 pm
www. mostbet. com [url=https://www.mostbet12033.ru]www. mostbet. com[/url]
mostbet_kg_cwpa
1 Nov 25 at 8:34 pm
Обновите интерьер вашего дома с помощью [url=https://csalon.ru/]Перетяжка мебели велюром[/url], которая придаст ей свежий и современный вид.
Важно разобрать мягкую мебель и снять старую обивку.
Перетяжка массажных столов
1 Nov 25 at 8:37 pm
Журнал для дачников https://www.amandine.ru и цветоводов: что посадить, когда поливать и чем подкармливать. Календарь, таблицы совместимости, защита от вредителей, обрезка и размножение. Планы грядок, тепличные секреты, бюджетные решения и советы профессионалов.
amandine-929
1 Nov 25 at 8:37 pm
Excelente resumen sobre las tragamonedas favoritas en Pin Up Casino México.
Me sorprendió ver cómo títulos como Gates
of Olympus y Sweet Bonanza siguen dominando entre los jugadores mexicanos.
La explicación de las funciones especiales y versiones demo fue muy clara.
Vale la pena visitar la publicación original y conocer en detalle
por qué estos títulos son tan jugados en 2025.
Se agradece ver una mezcla entre títulos nostálgicos
y nuevas propuestas en el mercado mexicano de apuestas.
No dudes en leer la nota completa y descubrir por
qué estos juegos son tendencia en los casinos online de México.
information
1 Nov 25 at 8:38 pm
Портал про все https://version.com.ua новини, технології, здоров’я, будинок, авто, подорожі, фінанси та кар’єра. Щоденні статті, огляди, лайфхаки та інструкції. Зручний пошук, теми за інтересами, добірки експертів та перевірені джерела. Читайте, навчайтеся, заощаджуйте час.
version-376
1 Nov 25 at 8:38 pm
chery pro chery tiggo t
official-chery-474
1 Nov 25 at 8:39 pm
Портал про все https://version.com.ua новини, технології, здоров’я, будинок, авто, подорожі, фінанси та кар’єра. Щоденні статті, огляди, лайфхаки та інструкції. Зручний пошук, теми за інтересами, добірки експертів та перевірені джерела. Читайте, навчайтеся, заощаджуйте час.
version-302
1 Nov 25 at 8:39 pm
mostbet скачать [url=http://mostbet12034.ru]http://mostbet12034.ru[/url]
mostbet_kg_ovPr
1 Nov 25 at 8:40 pm
chery 1.6 chery tiggo 9
official-chery-94
1 Nov 25 at 8:41 pm
Greetings! Very helpful advice in this particular post!
It’s the little changes which will make the biggest changes.
Thanks a lot for sharing!
ultra high vacuum systems
1 Nov 25 at 8:41 pm
With no login required, Trezor works independently of online systems.
trezor suite app usa
tr-suite-com.cc
TrezorAlexfd
1 Nov 25 at 8:42 pm
Truly many of fantastic advice!
taxi ischgl prices
1 Nov 25 at 8:43 pm
купить диплом историка [url=http://rudik-diplom14.ru/]купить диплом историка[/url] .
Diplomi_ccea
1 Nov 25 at 8:44 pm
Журнал для дачников https://www.amandine.ru и цветоводов: что посадить, когда поливать и чем подкармливать. Календарь, таблицы совместимости, защита от вредителей, обрезка и размножение. Планы грядок, тепличные секреты, бюджетные решения и советы профессионалов.
amandine-189
1 Nov 25 at 8:44 pm
Журнал для дачников https://www.amandine.ru и цветоводов: что посадить, когда поливать и чем подкармливать. Календарь, таблицы совместимости, защита от вредителей, обрезка и размножение. Планы грядок, тепличные секреты, бюджетные решения и советы профессионалов.
amandine-619
1 Nov 25 at 8:46 pm
https://t.me/s/ud_1Go
MichaelPione
1 Nov 25 at 8:47 pm
В Ростове-на-Дону клиника «ЧСП№1» оказывает услуги по выводу из запоя с полным медицинским сопровождением.
Узнать больше – [url=https://vyvod-iz-zapoya-rostov17.ru/]вывод из запоя круглосуточно в ростове-на-дону[/url]
Lamontdoubs
1 Nov 25 at 8:48 pm
SafeMedsGuide [url=https://safemedsguide.com/#]online pharmacy[/url] buy medications online safely
Hermanengam
1 Nov 25 at 8:49 pm