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!
Your means of describing the whole thing in this post is really good,
all be able to without difficulty be aware of it, Thanks a lot.
git.hsy.com
30 Oct 25 at 5:26 am
4M Dental Implant Center
3918 ᒪong Beach Blvd #200, Long Beach,
CA 90807, United Ⴝtates
15622422075
quality orthodontics
quality orthodontics
30 Oct 25 at 5:28 am
seo интенсив [url=http://kursy-seo-12.ru]http://kursy-seo-12.ru[/url] .
kyrsi seo_vzor
30 Oct 25 at 5:30 am
Spedra prezzo basso Italia: FarmaciaViva – Avanafil senza ricetta
ClydeExamp
30 Oct 25 at 5:32 am
I was recommended this web site by means of my cousin. I’m no longer
sure whether or not this publish is written by means of him as no one else understand such
particular about my trouble. You’re amazing!
Thank you!
small business
30 Oct 25 at 5:33 am
kamagra oral jelly: Kamagra 100mg prix France – Kamagra pas cher France
RobertJuike
30 Oct 25 at 5:37 am
uncommittednj.org – Great site if you’re looking for a grassroots voice and fresh perspective.
Claudio Clarkston
30 Oct 25 at 5:38 am
курс seo [url=http://kursy-seo-12.ru]http://kursy-seo-12.ru[/url] .
kyrsi seo_ueor
30 Oct 25 at 5:40 am
Hello, this weekend is good designed for me, since this moment i
am reading this wonderful informative post here at my residence.
pool abdeckplane bestellen
30 Oct 25 at 5:40 am
Нужно было выбрать инструменты генерации изображения для команды, и этот обзор очень помог. Все сервисы описаны честно, с реальными возможностями. Остановились на двух вариантах из подборки: сервисы для генерации картинок
MichaelPrion
30 Oct 25 at 5:41 am
seo базовый курc [url=https://kursy-seo-12.ru]https://kursy-seo-12.ru[/url] .
kyrsi seo_gdor
30 Oct 25 at 5:44 am
Hello! I could have sworn I’ve been to this blog
before but after reading through some of the post I realized it’s new to me.
Anyhow, I’m definitely delighted I found it and I’ll
be book-marking and checking back often!
game online
30 Oct 25 at 5:46 am
The application provides uninterrupted functioning with high-quality design and smooth gameplay, and [url=https://interknit.stokedev.au/how-to-download-the-1xbet-apk-a-comprehensive/]https://interknit.stokedev.au/how-to-download-the-1xbet-apk-a-comprehensive/[/url] confirms that participants at any time available there will be different configurations of entertainment.
BridgetMar
30 Oct 25 at 5:47 am
I’ve been surfing on-line more than 3 hours as of late,
yet I never discovered any interesting article like
yours. It’s beautiful price sufficient for me. In my view, if
all web owners and bloggers made just right content material as you did,
the web might be a lot more useful than ever before.
Christin
30 Oct 25 at 5:47 am
купить вкладыш в диплом техникума [url=http://www.frei-diplom12.ru]купить вкладыш в диплом техникума[/url] .
Diplomi_asPt
30 Oct 25 at 5:49 am
Откройте мир удобства с нашими [url=https://rulonnye-zhalyuzi-s-distantsionnym-upravleniem.ru/]автоматические рулонные жалюзи тканевые Prokarniz[/url], которые обеспечивают идеальное сочетание стиля и функциональности.
Кроме того, такие жалюзи доступны в различных материалах и цветах, что позволяет выбрать идеальный вариант для любого интерьера.
электрические жалюзи рулонные Прокарниз
30 Oct 25 at 5:49 am
Kamagra Wirkung und Nebenwirkungen: Kamagra Oral Jelly Deutschland – Kamagra Wirkung und Nebenwirkungen
ThomasCep
30 Oct 25 at 5:50 am
oneillforda.com – Thanks for the clear donation instructions, easy to navigate and secure.
Von Lundell
30 Oct 25 at 5:51 am
Pretty great post. I just stumbled upon your weblog and wanted to mention that I have truly enjoyed surfing around your weblog posts.
After all I’ll be subscribing to your feed and I hope you
write again soon!
snapfyn.com
30 Oct 25 at 5:54 am
Remarkable! Its truly amazing article, I have got much
clear idea on the topic of from this piece of writing.
Best liquidation pallets to buy
30 Oct 25 at 5:54 am
Ahaa, its nice dialogue regarding this post at
this place at this website, I have read all that, so at this time me also commenting at this place.
top online casinos
30 Oct 25 at 5:55 am
Spedra prezzo basso Italia: acquistare Spedra online – Spedra prezzo basso Italia
RichardImmon
30 Oct 25 at 5:55 am
обучение seo [url=www.kursy-seo-12.ru]обучение seo[/url] .
kyrsi seo_cwor
30 Oct 25 at 5:55 am
Если вы или ваши близкие нуждаетесь в выводе из запоя в Ростове-на-Дону, клиника «ЧСП№1» предлагает квалифицированную помощь. Врачи приедут на дом или вы сможете пройти лечение в стационаре. Цены на услуги начинаются от 3500 рублей.
Детальнее – [url=https://vyvod-iz-zapoya-rostov18.ru/]вывод из запоя на дому цена[/url]
CharlesNof
30 Oct 25 at 5:55 am
Hi there! This post couldn’t be written much better! Going through this
article reminds me of my previous roommate! He always kept preaching about this.
I most certainly will forward this information to him.
Pretty sure he’ll have a good read. Thanks for sharing!
https://jobs.kwintech.co.ke/companies/bizop/
30 Oct 25 at 5:59 am
You actually make it seem so easy with your presentation but
I find this topic to be really something
which I feel I might by no means understand.
It seems too complex and very wide for me. I am looking ahead for
your next put up, I will try to get the grasp of it!
homepage
30 Oct 25 at 6:01 am
differenza tra Spedra e Viagra: Avanafil senza ricetta – acquistare Spedra online
RichardImmon
30 Oct 25 at 6:06 am
seo с нуля [url=https://www.kursy-seo-12.ru]https://www.kursy-seo-12.ru[/url] .
kyrsi seo_mbor
30 Oct 25 at 6:08 am
VitaHomme: Vita Homme – kamagra oral jelly
RobertJuike
30 Oct 25 at 6:12 am
Kamagra 100mg prix France: Sildenafil générique – Kamagra sans ordonnance
RobertJuike
30 Oct 25 at 6:13 am
В Ростове-на-Дону мы гарантируем полную анонимность и конфиденциальность на всех этапах лечения, без постановки на учёт. Клиника в Ростове-на-Дону работает круглосуточно, обеспечивая доступность помощи в любое время дня и ночи.
Разобраться лучше – [url=https://vyvod-iz-zapoya-rostov112.ru/]вывод из запоя на дому цена[/url]
Jamiered
30 Oct 25 at 6:17 am
farmacia viva: pillole per disfunzione erettile – Avanafil senza ricetta
ClydeExamp
30 Oct 25 at 6:18 am
linebet официальный сайт
linebet login
30 Oct 25 at 6:21 am
купить диплом московского колледжа [url=http://www.frei-diplom12.ru]http://www.frei-diplom12.ru[/url] .
Diplomi_qnPt
30 Oct 25 at 6:22 am
An impressive share! I have just forwarded this onto
a co-worker who was doing a little research
on this. And he in fact ordered me breakfast because I found it for him…
lol. So allow me to reword this…. Thank YOU for the meal!!
But yeah, thanks for spending the time to discuss this topic here
on your web site.
https://loveis.app
30 Oct 25 at 6:25 am
обучение seo [url=http://kursy-seo-12.ru/]обучение seo[/url] .
kyrsi seo_ojor
30 Oct 25 at 6:25 am
gopakumarpillai.com
BQ
30 Oct 25 at 6:25 am
Эта познавательная публикация погружает вас в море интересного контента, который быстро захватит ваше внимание. Мы рассмотрим важные аспекты темы и предоставим вам уникальные Insights и полезные сведения для дальнейшего изучения.
Получить больше информации – https://bunyam.in/contact
Jimmyfex
30 Oct 25 at 6:27 am
kamagra: kamagra – kamagra
RobertJuike
30 Oct 25 at 6:27 am
Казино Dragon Money – разнообразие игр: слоты, рулетка, live-дилеры.
Бонусы за регистрацию, акции, быстрый вывод
драгон мани скачать
GeorgesaT
30 Oct 25 at 6:28 am
Hello There. I found your blog the usage of msn.
This is a very smartly written article. I will make sure to bookmark
it and come back to read extra of your useful information. Thank you for the post.
I’ll certainly comeback.
https://jtthuz.za.com/
30 Oct 25 at 6:28 am
Казино Dragon Money – разнообразие игр: слоты, рулетка, live-дилеры.
Бонусы за регистрацию, акции, быстрый вывод
dragon money
GeorgesaT
30 Oct 25 at 6:30 am
Мы предлагаем различные программы лечения в Ростове-на-Дону, включая стационарное и амбулаторное, чтобы выбрать оптимальный вариант для вас.
Узнать больше – [url=https://vyvod-iz-zapoya-rostov232.ru/]вывод из запоя на дому недорого ростов-на-дону[/url]
StevenSpulk
30 Oct 25 at 6:32 am
купить диплом техникума украина [url=https://www.frei-diplom12.ru]купить диплом техникума украина[/url] .
Diplomi_mtPt
30 Oct 25 at 6:32 am
Somebody necessarily lend a hand to make significantly articles I’d state.
That is the first time I frequented your web page and up to now?
I surprised with the analysis you made to create this particular
publish incredible. Fantastic process!
small business
30 Oct 25 at 6:33 am
Мы понимаем, как важно быстро и эффективно выйти из запоя, поэтому в Ростове-на-Дону предлагаем оперативную помощь в любое время.
Углубиться в тему – [url=https://vyvod-iz-zapoya-rostov238.ru/]вывод из запоя вызов на дом[/url]
Josephwam
30 Oct 25 at 6:35 am
Запой меняет сразу несколько систем организма: сдвигается водно-электролитный баланс, растёт нагрузка на печень и ЖКТ, «скачут» давление и пульс, усиливаются тревога и бессонница. Одна смесь «от всего» редко даёт устойчивый результат: днём краткий подъём, к ночи — откат и новая волна симптомов. Мы действуем иначе. Сначала — очная оценка и допуск к терапии с учётом того, что человек уже принимал за последние двое суток. Затем — инфузионная поддержка нужного объёма и темпа, симптом-контроль по показаниям, осторожная работа со сном, чтобы не маскировать риски тяжёлой седацией. После основного блока — переоценка, перевод на per os-схемы, правила режима и окно контрольной связи. За счёт последовательности результат становится не хрупким «здесь и сейчас», а устойчивым на сутки и неделю.
Получить дополнительные сведения – [url=https://vyvod-iz-zapoya-lobnya8.ru/]vyvoda-iz-zapoya-platno[/url]
CoreyItamn
30 Oct 25 at 6:36 am
Hi there! Would you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really enjoy your content.
Please let me know. Many thanks
ZR
30 Oct 25 at 6:37 am
Казино Dragon Money – разнообразие игр: слоты, рулетка, live-дилеры.
Бонусы за регистрацию, акции, быстрый вывод
драгон мани казино зеркало
GeorgesaT
30 Oct 25 at 6:37 am
купить вирт номер навсегда
купить вирт номер навсегда
30 Oct 25 at 6:38 am