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!
When some one searches for his essential thing, therefore
he/she wishes to be available that in detail, thus that thing is maintained over here.
دانشگاه غیرانتفاعی خیام مشهد
5 Sep 25 at 4:44 pm
продвижение сайтов интернет магазины в москве [url=http://internet-prodvizhenie-moskva.ru]продвижение сайтов интернет магазины в москве[/url] .
internet prodvijenie moskva_igOr
5 Sep 25 at 4:46 pm
https://say.la/read-blog/131133
Eddiebib
5 Sep 25 at 4:46 pm
I’m gone to tell my little brother, that he should also pay a visit this website on regular
basis to take updated from latest information.
bs2web.at
5 Sep 25 at 4:47 pm
компании занимающиеся продвижением сайтов [url=https://www.internet-agentstvo-prodvizhenie-sajtov-seo.ru]компании занимающиеся продвижением сайтов[/url] .
internet agentstvo prodvijenie saitov seo_ezot
5 Sep 25 at 4:47 pm
фабрика по пошиву одежды [url=https://nitkapro.ru]https://nitkapro.ru[/url] .
shveinoe proizvodstvo_vnea
5 Sep 25 at 4:50 pm
nexus market nexus onion dark markets [url=https://darkmarketsdirectory.com/ ]nexus market url [/url]
BrianWeX
5 Sep 25 at 4:50 pm
заказать продвижение сайта в москве [url=www.internet-prodvizhenie-moskva.ru]заказать продвижение сайта в москве[/url] .
internet prodvijenie moskva_wcOr
5 Sep 25 at 4:50 pm
продвижение сайтов интернет магазины в москве [url=www.kompanii-zanimayushchiesya-prodvizheniem-sajtov.ru]продвижение сайтов интернет магазины в москве[/url] .
kompanii zanimaushiesya prodvijeniem saitov_ygoi
5 Sep 25 at 4:51 pm
продвинуть сайт в москве [url=internet-prodvizhenie-moskva.ru]продвинуть сайт в москве[/url] .
internet prodvijenie moskva_thOr
5 Sep 25 at 4:52 pm
https://rafting-morvan-passion.fr/
rafting-397
5 Sep 25 at 4:53 pm
Hey! Do you know if they make any plugins to protect against hackers?
I’m kinda paranoid about losing everything
I’ve worked hard on. Any recommendations?
تسهیلات طرح سپید بانک مسکن
5 Sep 25 at 4:56 pm
Saya sangat terkesan dengan artikel ini karena penjelasannya mengenai
KUBET dan Situs Judi Bola Terlengkap terasa lengkap dan terstruktur.
Banyak orang sering mencari informasi seputar topik ini, namun tidak semua tulisan mampu memberikan gambaran yang
jelas seperti artikel ini.
Menurut saya, pembaca yang baru mengenal topik ini akan sangat terbantu dengan penjelasan yang diberikan.
Hal lain yang saya apresiasi adalah bagaimana penulis menyajikan konten dengan bahasa sederhana.
Topik mengenai KUBET dan Situs Judi Bola Terlengkap mungkin terdengar rumit, tetapi
cara penulisan yang ringan membuat siapa saja bisa memahami.
Ini adalah salah satu poin yang membuat artikel ini berbeda dari banyak
tulisan sejenis.
Saya juga merasa bahwa artikel ini memiliki nilai referensi
yang tinggi.
KUBET dan Situs Judi Bola Terlengkap dijelaskan bukan hanya dari
sisi popularitasnya, melainkan juga dari sisi kualitas yang ditawarkan.
Hal ini membuat artikel terasa lebih seimbang dan memberikan perspektif
yang objektif.
Bagi saya pribadi, artikel ini berhasil menambah wawasan sekaligus menjadi bahan bacaan yang bermanfaat.
Saya yakin banyak pembaca lain yang akan merasa hal yang
sama setelah membacanya.
Tulisan yang disusun dengan cara seperti ini tentu
akan lebih mudah diterima.
Semoga di kemudian hari akan semakin banyak artikel yang memiliki kualitas serupa.
Konten informatif yang membahas KUBET dan Situs Judi Bola
Terlengkap secara mendalam jelas sangat dibutuhkan.
Terima kasih kepada penulis karena telah menghadirkan artikel yang begitu
lengkap dan informatif.
Situs Parlay Resmi
5 Sep 25 at 4:57 pm
купить диплом о среднем образовании [url=www.educ-ua18.ru]купить диплом о среднем образовании[/url] .
Diplomi_ktPi
5 Sep 25 at 4:57 pm
https://rafting-morvan-passion.fr/
rafting-645
5 Sep 25 at 4:57 pm
І’ve been browsing online more than 2 hours today, yеt I never found
any interesting article liҝe yours. It is prеtty worth enough for me.
In my view, if all ԝebmasters and bloggerѕ made good content as yоu did, the
net will be much morе useful than ever before.
Also visit my blоɡ post; Shower Towels
Shower Towels
5 Sep 25 at 4:58 pm
Pretty! This was an extremely wonderful post.
Many thanks for providing this information.
زمان برگزاری آزمون های سازمان آموزش فنی و حرفه ای ۱۴۰۴
5 Sep 25 at 4:59 pm
Simply want to say your article is as astounding.
The clearness in your post is simply nice and i could assume you are an expert on this subject.
Well with your permission allow me to grab your feed to keep
updated with forthcoming post. Thanks a million and please
continue the rewarding work.
Anonymous
5 Sep 25 at 5:01 pm
продвижение по трафику [url=https://internet-prodvizhenie-moskva.ru/]продвижение по трафику[/url] .
internet prodvijenie moskva_lpOr
5 Sep 25 at 5:01 pm
купить свидетельство о расторжении брака [url=http://educ-ua3.ru]http://educ-ua3.ru[/url] .
Diplomi_tuki
5 Sep 25 at 5:07 pm
Hi there! I’m at work surfing around your blog from my new iphone!
Just wanted to say I love reading through your blog
and look forward to all your posts! Keep up the outstanding work!
Yupoo Chanel
5 Sep 25 at 5:08 pm
блэкспрут зеркало blacksprut, блэкспрут, black sprut, блэк спрут, blacksprut вход, блэкспрут ссылка, blacksprut ссылка, blacksprut onion, блэкспрут сайт, blacksprut вход, блэкспрут онион, блэкспрут дакрнет, blacksprut darknet, blacksprut сайт, блэкспрут зеркало, blacksprut зеркало, black sprout, blacksprut com зеркало, блэкспрут не работает, blacksprut зеркала, как зайти на blacksprutd
RichardPep
5 Sep 25 at 5:09 pm
https://www.betterplace.org/en/organisations/67820
Eddiebib
5 Sep 25 at 5:10 pm
интернет агентство продвижение сайтов сео [url=https://internet-prodvizhenie-moskva.ru/]интернет агентство продвижение сайтов сео[/url] .
internet prodvijenie moskva_ymOr
5 Sep 25 at 5:11 pm
Amazing issues here. I am very happy to look your article. Thanks a lot and I’m having a look ahead to touch you. Will you kindly drop me a e-mail?
интерент магазин посуды
GichardMam
5 Sep 25 at 5:12 pm
Казино Pokerdom слот Aztec Sun
WilliamDeers
5 Sep 25 at 5:14 pm
интернет продвижение москва [url=internet-prodvizhenie-moskva.ru]интернет продвижение москва[/url] .
internet prodvijenie moskva_yfOr
5 Sep 25 at 5:15 pm
швейная фабрика [url=http://www.nitkapro.ru]http://www.nitkapro.ru[/url] .
shveinoe proizvodstvo_qzea
5 Sep 25 at 5:16 pm
seo аудит веб сайта [url=https://internet-agentstvo-prodvizhenie-sajtov-seo.ru/]https://internet-agentstvo-prodvizhenie-sajtov-seo.ru/[/url] .
internet agentstvo prodvijenie saitov seo_zdot
5 Sep 25 at 5:19 pm
купить диплом магистра цены [url=https://educ-ua19.ru/]купить диплом магистра цены[/url] .
Diplomi_emml
5 Sep 25 at 5:20 pm
интернет агентство продвижение сайтов сео [url=https://www.internet-prodvizhenie-moskva.ru]интернет агентство продвижение сайтов сео[/url] .
internet prodvijenie moskva_rqOr
5 Sep 25 at 5:20 pm
dark market onion darknet markets onion dark websites [url=https://darknetmarketsgate.com/ ]nexus dark [/url]
Donaldfup
5 Sep 25 at 5:21 pm
We’re a group of volunteers and opening a new scheme in our community.
Your web site provided us with valuable info to work
on. You’ve done an impressive job and our whole community will be thankful to you.
akun demo pg soft
5 Sep 25 at 5:22 pm
เนื้อหานี้ อ่านแล้วได้ความรู้เพิ่ม ค่ะ
ผม ไปอ่านเพิ่มเติมเกี่ยวกับ มุมมองที่คล้ายกัน
สามารถอ่านได้ที่ singha89 slot online
น่าจะช่วยให้เข้าใจเรื่องนี้มากขึ้น
มีการยกตัวอย่างที่เข้าใจง่าย
ขอบคุณที่แชร์ สิ่งที่มีคุณค่า นี้
และหวังว่าจะได้เห็นโพสต์แนวนี้อีก
singha89 slot online
5 Sep 25 at 5:22 pm
фабрика пошива одежды на заказ [url=www.nitkapro.ru/]www.nitkapro.ru/[/url] .
shveinoe proizvodstvo_hnea
5 Sep 25 at 5:22 pm
Magnetic North Technology is a leading enterprise dedicated to the field of intelligent
transportation, integrating R&D, production, and sales.
As a total solution provider, the company specializes
in developing comprehensive systems for Intelligent Transportation (ITS),
with a particular focus on Intelligent Bus solutions and Mobile Payment technologies.
By combining cutting-edge hardware, advanced firmware,
and cloud-based platforms, Magnetic North Technology
delivers innovative, efficient, and seamless transportation experiences for modern cities.
Magnetic North Technology
5 Sep 25 at 5:22 pm
Appreciate it for sharing this — super handy.
Great article! Concise and to the point.
Neat overview. Useful takeaways.
Nicely written. Saved me time.
Cheers for the tips. Bookmarked.
Short yet useful. Well done.
Loved the breakdown. Very clear.
Nicely done! Hope to see more like this.
Value the effort that went into this. Good quality.
Readable formatting and straightforward language — great job.
Finans
5 Sep 25 at 5:23 pm
глубокий комлексный аудит сайта [url=https://internet-agentstvo-prodvizhenie-sajtov-seo.ru]https://internet-agentstvo-prodvizhenie-sajtov-seo.ru[/url] .
internet agentstvo prodvijenie saitov seo_ytot
5 Sep 25 at 5:25 pm
продвижение сайтов интернет магазины в москве [url=http://poiskovoe-prodvizhenie-sajta-v-internete-moskva.ru/]http://poiskovoe-prodvizhenie-sajta-v-internete-moskva.ru/[/url] .
poiskovoe prodvijenie saita v internete moskva_dcMa
5 Sep 25 at 5:25 pm
производство и пошив одежды [url=https://nitkapro.ru]https://nitkapro.ru[/url] .
shveinoe proizvodstvo_znea
5 Sep 25 at 5:26 pm
bitcoin dark web darknet market links nexus darknet market [url=https://darknetmarketgate.com/ ]dark web drug marketplace [/url]
DwayneAricE
5 Sep 25 at 5:26 pm
Вывод из запоя на дому возможен при легкой или средней степени тяжести абстинентного синдрома, отсутствии противопоказаний и сопутствующих заболеваний. Этот вариант лечения предпочтителен, так как пациент находится в комфортной и привычной обстановке, что снижает уровень стресса. Кроме того, сохраняется анонимность, поскольку вызов врача на дом не требует обращения в медицинское учреждение и постановки на учет. Также этот вариант более экономичен, так как стоимость вывода из запоя на дому обычно ниже, чем в стационаре.
Подробнее можно узнать тут – [url=https://nadezhnyj-vyvod-iz-zapoya.ru/vyvod-iz-zapoya-na-domu-v-sankt-peterburge.ru/]вывод из запоя на дому санкт-петербург[/url]
RichardDum
5 Sep 25 at 5:27 pm
Казино Ramenbet
JamesSog
5 Sep 25 at 5:29 pm
купить аттестат за 11 класс в богданович [url=www.arus-diplom25.ru]купить аттестат за 11 класс в богданович[/url] .
Diplomi_wlot
5 Sep 25 at 5:29 pm
продвижение сайта [url=internet-prodvizhenie-moskva.ru]продвижение сайта[/url] .
internet prodvijenie moskva_utOr
5 Sep 25 at 5:29 pm
интернет продвижение москва [url=http://internet-agentstvo-prodvizhenie-sajtov-seo.ru]интернет продвижение москва[/url] .
internet agentstvo prodvijenie saitov seo_hsot
5 Sep 25 at 5:30 pm
Aurum Codex mostbet AZ
JohnnieRag
5 Sep 25 at 5:30 pm
технического аудита сайта [url=https://poiskovoe-prodvizhenie-sajta-v-internete-moskva.ru/]https://poiskovoe-prodvizhenie-sajta-v-internete-moskva.ru/[/url] .
poiskovoe prodvijenie saita v internete moskva_bnMa
5 Sep 25 at 5:32 pm
Новости мира майл: пользователи
обсуждают новости в комментариях.
my site … teesside university jobs
teesside university jobs
5 Sep 25 at 5:32 pm
https://rant.li/ecofihlecudn/kupit-ne-zapreshchennyi-kokain-sinteticheskii
Eddiebib
5 Sep 25 at 5:34 pm