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!
промокод в 1win [url=http://1win5518.ru]http://1win5518.ru[/url]
1win_kg_xlkl
24 Oct 25 at 8:40 am
1xbet giri? g?ncel [url=1xbet-giris-8.com]1xbet giri? g?ncel[/url] .
1xbet giris_qzPn
24 Oct 25 at 8:43 am
Состоялось важное обновление XRumer 23.0.5 StrongAI, ключевое нововведение в котором — самообучение неизвестным полям и тексткапчам через GPT (при этом поддерживаются API аналогов GPT), а также повышение пробиваемости по форумам, контакт-формам и прочим платформам, расширение функционала.
[url=https://xrumer.ru/]Скачать Xrumer[/url]
ВАЖНО: Для получения действительно ощутимого прироста настоятельно рекомендуется:
[url=https://xrumer.ru/]Скачать Xrumer[/url]
Если работаете по форумам, обязательно скачайте и поставьте новый пак нейросетей под XEvil 6.0 (ссылка также есть в кабинете), подробности в мануале ниже
Используйте GPT для распознавания неизвестных тексткапч и полей, см. мануал ниже
В режиме “Только регистрация” (при работе с форумами) обязательно делайте несколько проходов, с использованием Инструмента “Сгенерировать базу непробитых ресурсов”
Наибольшую разницу с пред.версиями можно увидеть исключительно при работе с “сырыми” базами! В случае, если будете работать по проверенным базам от предыдущих версией, вы просто не будете охватывать вновь пробиваемые ресурсы
Обязательно внимательно прочтите мануалы, представленные в конце поста! От этого напрямую зависит отдача от софта, Вы можете получить как X так и 10X прироста по трафику — всё зависит сугубо от соблюдения рекомендаций
Скачать Xrumer
https://xrumer.ru/
Brucekeque
24 Oct 25 at 8:43 am
Актуальные тенденции 2026 года в бонусной политике букмекеров: анализ фрибетов, промо-купонов и программ лояльности; в тексте, в середине объяснения, даётся ссылка на https://bergkompressor.ru/news/artcles/?1xbet_promokod_pri_registracii_bonus_5.html как один из способов получить приветственный пакет. Пользователям рекомендуем внимательно читать условия акций.
Petercet
24 Oct 25 at 8:43 am
кракен онион
kraken market
кракен vk6
24 Oct 25 at 8:44 am
OMT’s bite-sized lessons ѕtop bewilder, enabling gradual love fоr math to bloom and motivate consistent examination prep ѡork.
Transform math challenges іnto triumphs ѡith OMT Math Tuition’s blend of
online and on-site choices, Ƅacked bү a performance history of student excellence.
Ꮤith math incorporated effortlessly іnto Singapore’s
class settings to benefit bߋth instructors and students, committed math tuition amplifies tһese gains by offering tailored support f᧐r continual accomplishment.
primary school math tuition іs imρortant f᧐r PSLE preparation aѕ it assists trainees master tһe fundamental concepts ⅼike portions and decimals, ᴡhich ɑre greatly checked in the exam.
Preѕenting heuristic methods еarly in secondary tuition prepares students fоr the non-routine
troubles tһаt often appeaг іn O Level analyses.
Individualized junior college tuition aids bridge tһе space from
O Level to A Level mathematics, mɑking certɑin pupils adjust tо
the increased rigor аnd depth called foг.
OMT’s customized curriculum uniquely lines սp ᴡith MOE structure Ьy
providing bridging modules for smooth ⅽhanges іn betweеn primary, secondary,
and JC mathematics.
OMT’ѕ on the internet neighborhood ɡives assistance leh, ԝhere ʏou can asҝ
questions and improve yօur knowing fоr far bеtter qualities.
Math tuition influences confidence νia success in smalⅼ milestones, driving Singapore trainees tⲟwards overaⅼl
exam victories.
Ⅿy webpage sec 1 math tuition
sec 1 math tuition
24 Oct 25 at 8:47 am
1xbet giri? yapam?yorum [url=https://www.1xbet-giris-7.com]https://www.1xbet-giris-7.com[/url] .
1xbet giris_wuKn
24 Oct 25 at 8:47 am
https://xnudes.ai/
ShawnSuilK
24 Oct 25 at 8:47 am
https://xnudes.ai/
ShawnSuilK
24 Oct 25 at 8:49 am
Эта статья предлагает уникальную подборку занимательных фактов и необычных историй, которые вы, возможно, не знали. Мы постараемся вдохновить ваше воображение и разнообразить ваш кругозор, погружая вас в мир, полный интересных открытий. Читайте и открывайте для себя новое!
Не упусти шанс – https://brjapon.com/1
RobertJek
24 Oct 25 at 8:50 am
Для юрлиц это отличный вариант — всё под ключ и вовремя https://tamozhenniiy-broker-moskva.ru/
NathanDax
24 Oct 25 at 8:51 am
Капитальный ремонт бензиновых двигателей в Москве [url=dzen.ru/a/aPkGhHYePCku9uSe]dzen.ru/a/aPkGhHYePCku9uSe[/url] .
Kapitalnii remont benzinovih dvigatelei v Moskve_ecEn
24 Oct 25 at 8:52 am
kraken vk5
кракен ссылка
JamesDaync
24 Oct 25 at 8:52 am
1 xbet giri? [url=www.1xbet-giris-9.com]www.1xbet-giris-9.com[/url] .
1xbet giris_rgon
24 Oct 25 at 8:53 am
1xbet [url=www.1xbet-giris-7.com/]1xbet[/url] .
1xbet giris_nxKn
24 Oct 25 at 8:53 am
Если вы ищете информацию о бонусах при регистрации, прочитайте наши рекомендации о видах вознаграждений; в одном из разделов статьи естественно упомянут 1хБет промокод для получения приветственного бонуса. Редакция объясняем, как вводить данные при регистрации и какие условия нужно выполнить для отыгрыша.
Petercet
24 Oct 25 at 8:54 am
Kent уделяет особое внимание защите персональных
данных, используя передовые технологии шифрования.
официальный сайт казино кент
24 Oct 25 at 8:54 am
В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
Смотри, что ещё есть – https://gnpsnalagarh.com/annual-result-day
Charlescep
24 Oct 25 at 8:55 am
1wi. [url=https://www.1win5518.ru]https://www.1win5518.ru[/url]
1win_kg_jmkl
24 Oct 25 at 8:56 am
1xbet t?rkiye giri? [url=www.1xbet-giris-8.com]1xbet t?rkiye giri?[/url] .
1xbet giris_jnPn
24 Oct 25 at 8:58 am
Attractive part of content. I just stumbled upon your website and in accession capital to assert that
I get actually loved account your blog posts.
Any way I will be subscribing to your feeds and even I fulfillment
you access constantly fast.
Akiho Yoshizawa
24 Oct 25 at 8:59 am
You are so interesting! I don’t think I’ve truly
read through anything like this before. So nice to discover another
person with some unique thoughts on this subject. Really..
thanks for starting this up. This site is something that is needed on the web, someone with a little originality!
NEA Design and Construction
24 Oct 25 at 9:00 am
1вин россия [url=www.1win5518.ru]www.1win5518.ru[/url]
1win_kg_wbkl
24 Oct 25 at 9:00 am
1xbet tr giri? [url=www.1xbet-giris-8.com/]www.1xbet-giris-8.com/[/url] .
1xbet giris_eqPn
24 Oct 25 at 9:00 am
куплю диплом кандидата наук [url=http://www.rudik-diplom9.ru]куплю диплом кандидата наук[/url] .
Diplomi_wzei
24 Oct 25 at 9:02 am
тусишка хороша, я как го грешил что нирвановская была какая то тёмная, так вот у чемикала она вообще практически бежевая 😀 качество порадовало, хорошая вещь )
Приобрести MEF GASH SHIHSKI ALFA – ОТЗЫВЫ, ГАРАНТИИ, КАЧЕСТВО
САБЖ САМ ПО СЕБЕ НЕ ПРЁТ !! МУТИТЬ НА НЁМ МИКСЫ СМЫСЛА НЕТ !!! ЭТО АНТИДЕПРЕССАНТ !!
Thomaspet
24 Oct 25 at 9:04 am
Надёжный партнёр по таможенному оформлению, никаких задержек: https://svobroker.ru/
Brianovalt
24 Oct 25 at 9:04 am
1xbet turkiye [url=http://1xbet-7.com/]http://1xbet-7.com/[/url] .
1xbet_ypol
24 Oct 25 at 9:04 am
В этой заметке мы представляем шаги, которые помогут в процессе преодоления зависимостей. Рассматриваются стратегии поддержки и чек-листы для тех, кто хочет сделать первый шаг к выздоровлению. Наша цель — вдохновить читателей на положительные изменения и поддержать их в трудных моментах.
Изучить вопрос глубже – https://fetch-soft.ru/7193568-narkologicheskaya-klinika-istochnik-zhizni-v-omske-59
LauraJic
24 Oct 25 at 9:05 am
кракен клиент
kraken обмен
кракен даркнет маркет
24 Oct 25 at 9:05 am
Ruleta online
Ruleta online
24 Oct 25 at 9:07 am
кракен клиент
кракен маркет
kraken ссылка
24 Oct 25 at 9:07 am
Этот информационный обзор станет отличным путеводителем по актуальным темам, объединяющим важные факты и мнения экспертов. Мы исследуем ключевые идеи и представляем их в доступной форме для более глубокого понимания. Читайте, чтобы оставаться в курсе событий!
Прочитать подробнее – https://yucedevlet.com/973.html
Bobbyfam
24 Oct 25 at 9:08 am
Как юридическое лицо, ценим скорость и точность — всё это здесь есть https://tamozhenniiy-broker-moskva11.ru/
NathanDax
24 Oct 25 at 9:08 am
1win букмекерская рабочее зеркало [url=https://www.1win5519.ru]1win букмекерская рабочее зеркало[/url]
1win_kg_iiEr
24 Oct 25 at 9:08 am
Ремонт двигателей Пежо [url=https://dzen.ru/a/aPkGhHYePCku9uSe/]https://dzen.ru/a/aPkGhHYePCku9uSe/[/url] .
Kapitalnii remont benzinovih dvigatelei v Moskve_qhEn
24 Oct 25 at 9:08 am
1xbet giri? linki [url=https://1xbet-giris-9.com/]1xbet giri? linki[/url] .
1xbet giris_xyon
24 Oct 25 at 9:08 am
1xbet giris [url=https://www.1xbet-giris-9.com]1xbet giris[/url] .
1xbet giris_cxon
24 Oct 25 at 9:10 am
When someone writes an paragraph he/she keeps the plan of a user in his/her mind that how a user can know it.
So that’s why this piece of writing is outstdanding. Thanks!
roof repair services
24 Oct 25 at 9:12 am
RENT A CAR DUBAI
BrockNeima
24 Oct 25 at 9:12 am
birxbet [url=http://1xbet-giris-8.com/]birxbet[/url] .
1xbet giris_hzPn
24 Oct 25 at 9:13 am
1xbet turkey [url=https://1xbet-giris-7.com/]1xbet turkey[/url] .
1xbet giris_vdKn
24 Oct 25 at 9:13 am
1win скачать с официального сайта [url=https://1win5518.ru]https://1win5518.ru[/url]
1win_kg_hrkl
24 Oct 25 at 9:14 am
В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
Это стоит прочитать полностью – https://harmony-communication.com/harmony-communication
Josephtep
24 Oct 25 at 9:15 am
Hmm is anyone else having problems with the images on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.
бонус Banda Casino
LhaneDrync
24 Oct 25 at 9:15 am
скачат 1 win [url=www.1win5518.ru]www.1win5518.ru[/url]
1win_kg_nkkl
24 Oct 25 at 9:17 am
1 x bet [url=1xbet-giris-9.com]1 x bet[/url] .
1xbet giris_byon
24 Oct 25 at 9:24 am
cuba automobiles
BrockNeima
24 Oct 25 at 9:25 am
1win xyz почта вход [url=https://1win5519.ru/]1win xyz почта вход[/url]
1win_kg_oeEr
24 Oct 25 at 9:27 am
где можно купить диплом медсестры [url=http://www.frei-diplom14.ru]где можно купить диплом медсестры[/url] .
Diplomi_fpoi
24 Oct 25 at 9:29 am