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://лестницы-в-москве.рф
TravisGaf
24 Oct 25 at 1:27 pm
купить диплом в вольске [url=https://www.rudik-diplom1.ru]https://www.rudik-diplom1.ru[/url] .
Diplomi_mqer
24 Oct 25 at 1:29 pm
The best is right here: https://rationaltheme.com
Franksen
24 Oct 25 at 1:31 pm
купить диплом с проводкой моих [url=frei-diplom2.ru]купить диплом с проводкой моих[/url] .
Diplomi_opEa
24 Oct 25 at 1:31 pm
Je suis captive par Olympe Casino, ca offre un plaisir melodieux. La variete des titres est divine, proposant des jeux de table glorieux. 100% jusqu’a 500 € + tours gratuits. L’assistance est efficace et sage, garantissant un support celeste. Le processus est simple et glorieux, de temps a autre quelques tours gratuits en plus seraient divins. En resume, Olympe Casino est un incontournable pour les joueurs pour les amateurs de sensations mythiques ! De plus la navigation est simple comme un oracle, ajoute une touche de mythologie. Egalement remarquable les options de paris sportifs variees, offre des recompenses eternelles.
olympefr.com|
ApolloLyraC6zef
24 Oct 25 at 1:33 pm
All the best is here: https://radhavatika.ac.in
MarioCam
24 Oct 25 at 1:33 pm
Top picks for you: https://theshaderoom.com
BrendanIneva
24 Oct 25 at 1:34 pm
https://rosslestnica.ru/
Victorcrype
24 Oct 25 at 1:34 pm
Приветственные бонусы также различаются по формату. Определённые акции доступны новым клиентам. Во время регистрации на 1xBet, используйте промокод и оформите 100% бонус на первый депозит в размере 32500 рублей.Платформа 1xBet предлагает своим клиентам играть и делать ставки с использованием специальных бонусов. Это увеличивает вовлечённость к играм и обеспечивает безопасность и комфорт игры.Промокод 2026 года можно получить по этой ссылке: промокод для ставки 1xbet.
JesusJuize
24 Oct 25 at 1:34 pm
Las 10 juegos mas jugados acerca de 2023
ArnoldKam
24 Oct 25 at 1:35 pm
1xbet ?ye ol [url=http://www.1xbet-giris-10.com]1xbet ?ye ol[/url] .
1xbet giris_wyka
24 Oct 25 at 1:36 pm
J’ai une passion lacustre pour BassBet Casino, c’est une plateforme qui ondule avec elegance. Le catalogue est riche et varie, proposant des jeux de table elegants. Amplifiant le plaisir de jeu. Le service est disponible 24/7, toujours pret a pecher. Les paiements sont securises et rapides, neanmoins quelques tours gratuits supplementaires seraient bien venus. En bref, BassBet Casino garantit un plaisir constant pour les passionnes de jeux modernes ! Ajoutons que l’interface est fluide comme un lac, ajoute une touche de vitesse. Particulierement interessant les paiements securises en crypto, renforce le sentiment de communaute.
https://bassbetcasinobonusfr.com/|
RiverBassD2zef
24 Oct 25 at 1:36 pm
Je suis totalement illumine par BassBet Casino, c’est une plateforme qui scintille comme un neon. Le catalogue est riche en surprises, incluant des paris sportifs vibrants. 100% jusqu’a 500 € + tours gratuits. Le support client est au top, garantissant un service de haute qualite. Les transactions sont fiables, neanmoins quelques tours gratuits en plus seraient top. Pour conclure, BassBet Casino est un must pour les joueurs pour les joueurs en quete d’adrenaline ! De plus la plateforme est visuellement fluorescente, ajoute une touche de lumiere. Un atout les options de paris variees, propose des avantages sur mesure.
https://bassbetcasinopromocodefr.com/|
GlowTuneH7zef
24 Oct 25 at 1:37 pm
1xbet resmi giri? [url=https://1xbet-7.com/]1xbet-7.com[/url] .
1xbet_zxol
24 Oct 25 at 1:38 pm
1x bet [url=https://www.1xbet-giris-3.com]https://www.1xbet-giris-3.com[/url] .
1xbet giris_roMi
24 Oct 25 at 1:38 pm
https://лестницы-в-москве.рф
TravisGaf
24 Oct 25 at 1:39 pm
диплом техникума купить цены [url=www.frei-diplom8.ru/]диплом техникума купить цены[/url] .
Diplomi_aosr
24 Oct 25 at 1:39 pm
купить диплом энергетика [url=https://www.rudik-diplom3.ru]купить диплом энергетика[/url] .
Diplomi_hgei
24 Oct 25 at 1:40 pm
Je suis accro a Spinit Casino, on ressent une ambiance delicate. La selection de jeux est somptueuse, comprenant des jeux compatibles avec les cryptos. 100% jusqu’a 500 € + tours gratuits. Le suivi est irreprochable, offrant des reponses claires. Les gains arrivent sans delai, bien que des recompenses additionnelles seraient royales. En resume, Spinit Casino offre une experience memorable pour les fans de casino en ligne ! Ajoutons que le design est moderne et chic, ajoute une touche d’elegance. Egalement appreciable les paiements securises en crypto, assure des transactions fiables.
https://spinitcasinobonusfr.com/|
EnchantedReelF7zef
24 Oct 25 at 1:40 pm
Actually no matter if someone doesn’t know after that its up to other visitors that they will help,
so here it occurs.
Optima Fundrelix Scam
24 Oct 25 at 1:40 pm
купить диплом в одессе недорого [url=http://educ-ua7.ru/]http://educ-ua7.ru/[/url] .
Diplomi_siea
24 Oct 25 at 1:40 pm
где можно купить диплом медсестры [url=www.frei-diplom13.ru]где можно купить диплом медсестры[/url] .
Diplomi_uekt
24 Oct 25 at 1:40 pm
купить диплом инженера строителя [url=www.rudik-diplom10.ru]купить диплом инженера строителя[/url] .
Diplomi_euSa
24 Oct 25 at 1:41 pm
Artikel ini pantas diapresiasi! Cara penulis mengupas topik KUBET, Situs Judi Bola Terlengkap, dan Situs Mix Parlay terasa profesional dan informatif.
Saya suka bagaimana artikel ini juga menyinggung Situs Parlay Resmi dan Situs
Parlay Gacor secara detail. Selain itu, tambahan informasi mengenai kubet login, toto macau, dan situs parlay membuat artikel
ini semakin lengkap dan relevan untuk pembaca yang ingin tahu lebih banyak.
Situs Mix Parlay
24 Oct 25 at 1:41 pm
Great post.
Trang chủ 13win
24 Oct 25 at 1:42 pm
xbet giri? [url=www.1xbet-4.com]www.1xbet-4.com[/url] .
1xbet_clol
24 Oct 25 at 1:42 pm
купить диплом монтажника [url=www.rudik-diplom8.ru/]купить диплом монтажника[/url] .
Diplomi_lmMt
24 Oct 25 at 1:43 pm
https://t.me/s/bs_1xbet/49
https://t.me/s/bs_1xbet/25
24 Oct 25 at 1:43 pm
http://www.stes.tyc.edu.tw/xoops/modules/profile/userinfo.php?uid=3771215
lhuezks
24 Oct 25 at 1:43 pm
https://t.me/s/bs_1xbet/21
https://t.me/bs_1xbet/9
24 Oct 25 at 1:44 pm
bahis siteler 1xbet [url=http://1xbet-giris-3.com]http://1xbet-giris-3.com[/url] .
1xbet giris_qnMi
24 Oct 25 at 1:45 pm
можно ли купить диплом медсестры [url=http://frei-diplom14.ru/]можно ли купить диплом медсестры[/url] .
Diplomi_jooi
24 Oct 25 at 1:45 pm
купить диплом о среднем образовании [url=https://rudik-diplom1.ru/]купить диплом о среднем образовании[/url] .
Diplomi_bner
24 Oct 25 at 1:45 pm
купить диплом с занесением в реестр цена [url=https://frei-diplom6.ru/]купить диплом с занесением в реестр цена[/url] .
Diplomi_fvOl
24 Oct 25 at 1:45 pm
1x bet [url=http://1xbet-giris-7.com]1x bet[/url] .
1xbet giris_xqKn
24 Oct 25 at 1:46 pm
купить диплом в салавате [url=https://rudik-diplom3.ru]купить диплом в салавате[/url] .
Diplomi_roei
24 Oct 25 at 1:46 pm
Купить диплом техникума в Винница [url=https://www.educ-ua7.ru]https://www.educ-ua7.ru[/url] .
Diplomi_xlea
24 Oct 25 at 1:47 pm
купить диплом в самаре [url=https://rudik-diplom14.ru]купить диплом в самаре[/url] .
Diplomi_dkea
24 Oct 25 at 1:47 pm
где можно купить диплом медсестры [url=http://www.frei-diplom13.ru]где можно купить диплом медсестры[/url] .
Diplomi_jvkt
24 Oct 25 at 1:48 pm
купить диплом в бердске [url=http://www.rudik-diplom13.ru]http://www.rudik-diplom13.ru[/url] .
Diplomi_tgon
24 Oct 25 at 1:50 pm
Принцип прост – чем больше ставок делает игрок, тем выше его максимальный выигрыш.
слоты на деньги с выводом
24 Oct 25 at 1:50 pm
https://t.me/s/bs_1xbet/3
https://t.me/bs_1xbet/23
24 Oct 25 at 1:50 pm
https://t.me/s/bs_1xbet/26
https://t.me/bs_1xbet/16
24 Oct 25 at 1:50 pm
диплом техникума колледжа купить [url=www.educ-ua7.ru]www.educ-ua7.ru[/url] .
Diplomi_dnea
24 Oct 25 at 1:51 pm
купить диплом математика [url=https://www.rudik-diplom5.ru]купить диплом математика[/url] .
Diplomi_hima
24 Oct 25 at 1:51 pm
купить диплом менеджера [url=www.rudik-diplom1.ru/]купить диплом менеджера[/url] .
Diplomi_ueer
24 Oct 25 at 1:51 pm
купить дипломы о высшем [url=http://www.rudik-diplom4.ru]купить дипломы о высшем[/url] .
Diplomi_ydOr
24 Oct 25 at 1:53 pm
Please let me know if you’re looking for a article writer for
your blog. You have some really great posts and I believe I would be
a good asset. If you ever want to take some of the load off, I’d
love to write some articles for your blog in exchange for a
link back to mine. Please send me an email if interested.
Thanks!
888NEW
24 Oct 25 at 1:54 pm
купить диплом в биробиджане [url=www.rudik-diplom11.ru]купить диплом в биробиджане[/url] .
Diplomi_mjMi
24 Oct 25 at 1:54 pm
купить диплом в энгельсе [url=http://www.rudik-diplom10.ru]http://www.rudik-diplom10.ru[/url] .
Diplomi_hjSa
24 Oct 25 at 1:55 pm