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!
“[url=https://peretyazhka-bel.ru/]Перетяжка угловых диванов[/url]”
Смена ткани помогает сохранить любимый диван или кресло, избежав покупки новой мебели.
Такой подход не только экономит бюджет, но и позволяет индивидуализировать интерьер. Это отличный способ адаптировать мебель под меняющиеся предпочтения в оформлении дома.
—
### **2. Какие материалы лучше использовать?**
Для перетяжки применяют различные ткани, отличающиеся износостойкостью и внешним видом. Хлопок и лён подойдут для помещений с невысокой нагрузкой.
Также важно учитывать наполнитель, который влияет на комфорт. Холофайбер и синтепон лучше сохраняют форму и упругость.
—
### **3. Этапы профессиональной перетяжки**
Процесс начинается с демонтажа старой обивки и оценки состояния каркаса. Мастер удаляет изношенную ткань и проверяет прочность конструкции.
Далее выбирают материал и производят раскрой. Новая ткань кроится точно по размерам мебели, чтобы избежать перекосов.
—
### **4. Преимущества профессионального подхода**
Обращение к специалистам гарантирует качество и долгий срок службы мебели. Профессионалы используют надёжные крепления и прочные швы.
Кроме того, экономится время и исключаются ошибки. Только специалист сможет точно воспроизвести первоначальную форму мебели.
—
### **Спин-шаблон:**
#### **1. Почему стоит выбрать перетяжку мебели?**
– Обновление обивки даёт мебели вторую жизнь, сохраняя её функциональность.
– Это отличный способ адаптировать мебель под меняющиеся предпочтения в оформлении дома.
#### **2. Какие материалы лучше использовать?**
– Кожа и экокожа придают мебели благородный вид и просты в уходе.
– Холофайбер и синтепон лучше сохраняют форму и упругость.
#### **3. Этапы профессиональной перетяжки**
– Мастер удаляет изношенную ткань и проверяет прочность конструкции.
– Раскрой выполняется с запасом для удобства последующего натяжения.
#### **4. Преимущества профессионального подхода**
– Мастера подбирают оптимальные методы перетяжки для разных типов мебели.
– Доверив работу профессионалам, вы избежите непредвиденных проблем.
peretyazhk_vtKi
2 Nov 25 at 8:50 pm
1xbet turkey [url=https://www.1xbet-giris-4.com]https://www.1xbet-giris-4.com[/url] .
1xbet giris_saSa
2 Nov 25 at 8:51 pm
1xbet mobil giri? [url=http://1xbet-giris-2.com]http://1xbet-giris-2.com[/url] .
1xbet giris_fsPt
2 Nov 25 at 8:55 pm
продвижение сайта в топ 10 профессионалами [url=https://www.reiting-kompanii-po-prodvizheniyu-sajtov.ru]https://www.reiting-kompanii-po-prodvizheniyu-sajtov.ru[/url] .
agentstvo poiskovogo prodvijeniya_zzKt
2 Nov 25 at 8:55 pm
1xbet t?rkiye [url=www.1xbet-giris-2.com]www.1xbet-giris-2.com[/url] .
1xbet giris_jcPt
2 Nov 25 at 8:57 pm
top-rated pharmacies in Ireland
Edmundexpon
2 Nov 25 at 8:58 pm
диплом настоящий купить с занесением в реестр [url=frei-diplom3.ru]frei-diplom3.ru[/url] .
Diplomi_hoKt
2 Nov 25 at 8:59 pm
1 x bet [url=http://1xbet-giris-5.com]http://1xbet-giris-5.com[/url] .
1xbet giris_jbSa
2 Nov 25 at 9:00 pm
потолочкин натяжные [url=natyazhnye-potolki-nizhniy-novgorod-1.ru]natyazhnye-potolki-nizhniy-novgorod-1.ru[/url] .
natyajnie potolki nijnii novgorod_ijma
2 Nov 25 at 9:02 pm
Australian pharmacy reviews [url=http://aussiemedshubau.com/#]pharmacy discount codes AU[/url] pharmacy discount codes AU
Hermanengam
2 Nov 25 at 9:08 pm
купить диплом в батайске [url=https://rudik-diplom13.ru/]https://rudik-diplom13.ru/[/url] .
Diplomi_tnon
2 Nov 25 at 9:09 pm
Uk Meds Guide: non-prescription medicines UK – cheap medicines online UK
Johnnyfuede
2 Nov 25 at 9:09 pm
top rated online pharmacies: best online pharmacy – best online pharmacy
Johnnyfuede
2 Nov 25 at 9:10 pm
xbet giri? [url=https://1xbet-giris-2.com]https://1xbet-giris-2.com[/url] .
1xbet giris_pwPt
2 Nov 25 at 9:10 pm
рейтинг агентств digital маркетинга [url=www.luchshie-digital-agencstva.ru/]www.luchshie-digital-agencstva.ru/[/url] .
lychshie digital agentstva_yyoi
2 Nov 25 at 9:10 pm
https://irishpharmafinder.shop/# top-rated pharmacies in Ireland
Haroldovaph
2 Nov 25 at 9:11 pm
Wow, marvelous blog format! How lengthy have you ever been blogging for?
you made blogging look easy. The full look of your website is great, as well as the content!
Hongkong Lotto
2 Nov 25 at 9:11 pm
https://brewbower.co.uk/1xbet-codigo-de-bonificacion-2026-e1950-150-fs-casino/
ykkfsob
2 Nov 25 at 9:15 pm
купить диплом техникума в реестре цена [url=www.frei-diplom3.ru/]www.frei-diplom3.ru/[/url] .
Diplomi_nnKt
2 Nov 25 at 9:15 pm
купить диплом в чайковском [url=http://rudik-diplom6.ru]купить диплом в чайковском[/url] .
Diplomi_brKr
2 Nov 25 at 9:15 pm
Спасибо за выведение клопов! Всё чисто и безопасно.
дезинфекция квартир
KennethceM
2 Nov 25 at 9:15 pm
I am regular visitor, how are you everybody? This post posted at this web site is actually pleasant.
teslabahis giriş
2 Nov 25 at 9:16 pm
https://t.me/s/ud_1xSlOtS
AlbertTeery
2 Nov 25 at 9:17 pm
1xbet resmi sitesi [url=https://1xbet-giris-5.com/]https://1xbet-giris-5.com/[/url] .
1xbet giris_uqSa
2 Nov 25 at 9:17 pm
потолочников натяжные потолки [url=natyazhnye-potolki-nizhniy-novgorod-1.ru]потолочников натяжные потолки[/url] .
natyajnie potolki nijnii novgorod_ktma
2 Nov 25 at 9:19 pm
https://t.me/s/ud_jeT
AlbertTeery
2 Nov 25 at 9:19 pm
https://articleshine.adseon.xyz/code-promo-melbet-2026-e130-bonus-pour-parier/
AnthonyGaw
2 Nov 25 at 9:20 pm
birxbet [url=https://1xbet-giris-4.com/]1xbet-giris-4.com[/url] .
1xbet giris_wbSa
2 Nov 25 at 9:21 pm
купить диплом о высшем образовании проведенный [url=http://frei-diplom3.ru]купить диплом о высшем образовании проведенный[/url] .
Diplomi_xzKt
2 Nov 25 at 9:21 pm
1xbet resmi giri? [url=https://1xbet-giris-4.com/]https://1xbet-giris-4.com/[/url] .
1xbet giris_cjSa
2 Nov 25 at 9:23 pm
verified pharmacy coupon sites Australia: cheap medicines online Australia – compare pharmacy websites
Johnnyfuede
2 Nov 25 at 9:23 pm
Tarz?n?z? 90’lar?n unutulmaz modas?ndan esinlenerek gunumuze tas?mak ister misiniz? Oyleyse bu yaz?m?z tam size gore!
По теме “Guzellik ve Kozmetik: 90’lar Modas?ndan Ipuclar?”, есть отличная статья.
Вот, делюсь ссылкой:
[url=https://aynakirildi.com]https://aynakirildi.com[/url]
Guzellik, gecmisle gunumuz aras?nda bir kopru kurmakt?r. 90’lar?n moda s?rlar?, bu koprunun onemli parcalar?ndan.
Josephassof
2 Nov 25 at 9:25 pm
Kaizenaire.ϲom aggregates Singapore’ѕ hottest deals, establishing іtself ɑѕ the
leading promotions internet site.
Singapore’ѕ shopping paradise status іs a magnet fοr residents
tһat adore discovering concealed promotions ɑnd scoring ցreat deals.
Singaporeans aρpreciate kite flying at Marina Barrage օn windy
days, ɑnd keep in mind to stay upgraded ᧐n Singapore’s newest promotions
and shopping deals.
Adidas օffers sports apparel and tennis shoes, treasured ƅу
Singaporeans for tһeir elegant activewear аnd recommendation by neighborhood
professional athletes.
Centuries Hotels оffers deluxe accommodations аnd hospitality services оne, cherished bү Singaporeans foг their comfy keeps and pгime locations mah.
Ichiban Boshi оffers conveyor belt sushi аnd bentos, valued for fun, family-friendly dishes ᴡith
authentic tastes.
Wah, а lot of brand names ѕia, check Kaizenaire.ϲom commonly
to snag tһose unique priuce cuts lor.
Мʏ web ρage – singapore promotions
singapore promotions
2 Nov 25 at 9:26 pm
https://hellochat.dhakadsahab.com/blogs/71110/Code-Bonus-Melbet-Offre-Sp%C3%A9ciale-130-Offre-Bonus
Robinkanty
2 Nov 25 at 9:28 pm
irishpharmafinder
Edmundexpon
2 Nov 25 at 9:30 pm
best Irish pharmacy websites: discount pharmacies in Ireland – best Irish pharmacy websites
HaroldSHems
2 Nov 25 at 9:31 pm
https://t.me/s/Ud_gAMa
AlbertTeery
2 Nov 25 at 9:32 pm
лучшие seo агентства [url=https://www.luchshie-digital-agencstva.ru]лучшие seo агентства[/url] .
lychshie digital agentstva_deoi
2 Nov 25 at 9:32 pm
I’m impressed, I have to admit. Seldom do I come across a blog that’s both
educative and engaging, and without a doubt, you’ve hit the nail on the head.
The issue is something that not enough people are speaking intelligently about.
I’m very happy that I came across this in my search for something relating to
this.
kèo nhà cái 5
2 Nov 25 at 9:33 pm
1xbet tr giri? [url=https://1xbet-giris-5.com]https://1xbet-giris-5.com[/url] .
1xbet giris_cgSa
2 Nov 25 at 9:35 pm
Scientists discovered something alarming seeping out from beneath the ocean around Antarctica
[url=https://360.ru/news/crime/sozdatelja-finansovoj-piramidy-life-is-good-objavili-v-rozysk/]жесткое порно[/url]
Planet-heating methane is escaping from cracks in the Antarctic seabed as the region warms, with new seeps being discovered at an “astonishing rate,” scientists have found, raising fears that future global warming predictions may have been underestimated.
Huge amounts of methane lie in reservoirs that have formed over millennia beneath the seafloor around the world. This invisible, climate-polluting gas can escape into the water through fissures in the sea floor, often revealing itself with a stream of bubbles weaving their way up to the ocean surface.
https://iz.ru/1813811/sergei-guranov/pafosnoe-ukrytie-na-kipre-nasli-beglogo-osnovatela-piramidy-life-good
гей секс порно
Relatively little is known about these underwater seeps, how they work, how many there are, and how much methane reaches the atmosphere versus how much is eaten by methane-munching microbes living beneath the ocean.
But scientists are keen to better understand them, as this super-polluting gas traps around 80 times more heat than carbon dioxide in its first 20 years in the atmosphere.
Methane seeps in Antarctica are among the least understood on the planet, so a team of international scientists set out to find them. They used a combination of ship-based acoustic surveys, remotely operated vehicles and divers to sample a range of sites in the Ross Sea, a bay in Antarctica’s Southern Ocean, at depths between 16 and 790 feet.
What they found surprised them. They identified more than 40 methane seeps in the shallow water of the Ross Sea, according to the study published this month in Nature Communications.
Bubbles rising from a methane seep at Cape Evans, Antarctica. Leigh Tate, Earth Sciences New Zealand
Many of the seeps were found at sites that had been repeatedly studied before, suggesting they were new. This may indicate a “fundamental shift” in the methane released in the region, according to the report.
Methane seeps are relatively common globally, but previously there was only one confirmed active seep in the Antarctic, said Sarah Seabrook, a report author and a marine scientist at Earth Sciences New Zealand, a research organization. “Something that was thought to be rare is now seemingly becoming widespread,” she told CNN.
Every seep they discovered was accompanied by an “immediate excitement” that was “quickly replaced with anxiety and concern,” Seabrook said.
The fear is these seeps could rapidly transfer methane into the atmosphere, making them a source of planet-heating pollution that is not currently factored into future climate change predictions.
The scientists are also concerned the methane could have cascading impacts on marine life.
DonaldCix
2 Nov 25 at 9:36 pm
В Екатеринбурге быстро подобрать безопасные лестницы и стремянки легко: официальный KRAUSE дает профессиональные решения для быта, сервиса и стройплощадок с запасом прочности. В ассортименте представлены Corda, Monto, Stabilo, телескопические платформы, вышки-туры и лестницы с площадкой — все по фирменным стандартам производителя с 30-летней историей в РФ. Эксперты бесплатно подберут высоту, конфигурацию и допустимую нагрузку, а доставка и гарантийная поддержка ускорят оформление. Ищете шарнирная лестница corda krause купить? Подробности и заказ на сайте — krause-ekb.ru
miwerlop
2 Nov 25 at 9:36 pm
code promo 1xbet cote d’ivoire
AbrahamERAME
2 Nov 25 at 9:36 pm
1x lite [url=1xbet-giris-4.com]1xbet-giris-4.com[/url] .
1xbet giris_qfSa
2 Nov 25 at 9:38 pm
1xbet giri? [url=https://1xbet-giris-2.com/]1xbet giri?[/url] .
1xbet giris_ulPt
2 Nov 25 at 9:39 pm
потолка [url=https://natyazhnye-potolki-nizhniy-novgorod-1.ru/]потолка[/url] .
natyajnie potolki nijnii novgorod_svma
2 Nov 25 at 9:41 pm
pharmacy online: Australian pharmacy reviews – Aussie Meds Hub
HaroldSHems
2 Nov 25 at 9:42 pm
https://hyperstartupss.com/1xbet-codigo-promocional-2026-e130-para-apuestas/
ckwtdhg
2 Nov 25 at 9:42 pm
1xbet mobi [url=https://1xbet-giris-2.com/]1xbet-giris-2.com[/url] .
1xbet giris_zoPt
2 Nov 25 at 9:42 pm
online pharmacy
Edmundexpon
2 Nov 25 at 9:43 pm