Roulette Wiel: Wed liefde om u een mooie gemakkelijke manier om een overwinning te garanderen wanneer u klikt om te draaien.
  • Gratis Casino I Mobilen - Rekening houdend met alles, heeft dit Grosvenor beoordeling denk dat deze operator heeft het recht om zichzelf te labelen als de meest populaire casino in het Verenigd Koninkrijk.
  • Wat Heb Je Nodig Om Bingo Te Spelen: Jagen prooi groter dan zichzelf, terwijl heimelijk negeren van hun vijand early warning systeem is slechts een van de vele coole combinaties in het spel.
  • Winkans bij loterijen

    Wild Spells Online Gokkast Spelen Gratis En Met Geld
    We hebben deze download online casino's door middel van een strenge beoordeling proces om ervoor te zorgen dat u het meeste uit uw inzetten wanneer u wint.
    Nieuwe Gokkasten Gratis
    Dit betekent dat het hangt af van wat inkomstenbelasting bracket je in, en of de winst zal duwen u in een andere bracket.
    The delight is de geanimeerde banner met de welkomstpromotie bij de eerste duik je in.

    Pokersites voor Enschedeers

    Nieuw Casino
    De reel set is 7x7, met een totaal van 49 symbolen in het spel.
    Casigo Casino 100 Free Spins
    Holland Casino Eindhoven is een vestiging waar veel georganiseerd op het gebied van entertainment..
    Casino Spel Gratis Slots

    Sjoerd Maessen blog

    PHP and webdevelopment

    PHP hook, building hooks in your application

    with 7,044 comments

    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!

    Written by Sjoerd Maessen

    May 23rd, 2011 at 8:02 pm

    Posted in API

    Tagged with , , ,

    7,044 Responses to 'PHP hook, building hooks in your application'

    Subscribe to comments with RSS or TrackBack to 'PHP hook, building hooks in your application'.

    1. Thanks for the good writeup. It in fact was a enjoyment account it.
      Look complicated to far introduced agreeable from you! However, how could we keep up a correspondence?

    2. ua-pulse-363

      29 Jul 25 at 9:12 am

    3. It’s an awesome post in support of all the online viewers; they will get advantage
      from it I am sure.

    4. Предлагаем вашему вниманию интересную справочную статью, в которой собраны ключевые моменты и нюансы по актуальным вопросам. Эта информация будет полезна как для профессионалов, так и для тех, кто только начинает изучать тему. Узнайте ответы на важные вопросы и расширьте свои знания!
      Выяснить больше – https://amylynette.com/2017/03/31/1-john-aprils-scripture-writing-challenge

      Jasonpaisa

      29 Jul 25 at 9:19 am

    5. Greetings! Very helpful advice within this article!
      It’s the little changes that make the biggest changes.

      Thanks for sharing!

    6. order amoxicillin without prescription: ClearMeds Direct – where can i get amoxicillin 500 mg

      BrianTub

      29 Jul 25 at 9:24 am

    7. Write more, thats all I have to say. Literally, it seems as
      though you relied on the video to make your point.
      You obviously know what youre talking about, why throw away your
      intelligence on just posting videos to your blog when you could be giving
      us something enlightening to read?

      play288

      29 Jul 25 at 9:30 am

    8. Этот информационный материал собраны данные, которые помогут лучше понять текущие тенденции и процессы в различных сферах жизни. Мы предоставляем четкий анализ, графики и примеры, чтобы информация была не только понятной, но и практичной для принятия решений.
      Узнать напрямую – http://www.mingwong.org/windows-on-the-world-part-2/80_uccamingwong2015installation22lores

      JosephGot

      29 Jul 25 at 9:36 am

    9. novosti24-416

      29 Jul 25 at 9:38 am

    10. 1win [url=https://www.1win3064.ru]https://www.1win3064.ru[/url]

      1win_xfPn

      29 Jul 25 at 9:38 am

    11. novosti24-635

      29 Jul 25 at 9:38 am

    12. ua-pulse-809

      29 Jul 25 at 9:44 am

    13. ua-pulse-560

      29 Jul 25 at 9:44 am

    14. Эта публикация дает возможность задействовать различные источники информации и представить их в удобной форме. Читатели смогут быстро найти нужные данные и получить ответы на интересующие их вопросы. Мы стремимся к четкости и доступности материала для всех!
      Узнать напрямую – https://231disposal.com/blog/3-ways-to-get-rid-of-a-couch-in-traverse-city-michigan

      RogerVah

      29 Jul 25 at 9:47 am

    15. Hello friends, nice post and good arguments commented here, I am genuinely enjoying
      by these.

      m98 bet

      29 Jul 25 at 9:48 am

    16. взломанные игры с модами — это замечательный способ расширить функциональность игры.
      Особенно если вы пользуетесь устройствами на платформе Android, модификации открывают перед вами новые возможности.
      Я нравится использовать модифицированные версии игр, чтобы развиваться быстрее.

      Модификации игр дают невероятную персонализированный подход,
      что погружение в игру гораздо захватывающее.
      Играя с плагинами, я могу добавить дополнительные функции, что добавляет новые приключения и
      делает игру более непредсказуемой.

      Это действительно удивительно, как такие моды могут улучшить игровой процесс, а при этом с максимальной безопасностью использовать такие взломанные версии можно без особых
      неприятных последствий, если быть внимательным и следить за обновлениями.
      Это делает каждый игровой процесс уникальным, а возможности практически широкие.

      Советую попробовать такие игры с модами для Android — это может вдохновит на
      новые приключения

    17. прогнозы и ставки на спорт [url=https://www.sportbets27.ru]https://www.sportbets27.ru[/url] .

      sportbets_zcKa

      29 Jul 25 at 9:49 am

    18. Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
      Перейти к статье – https://www.bambousushi.be/2015/07/22/eggs-bacon-mac-cheese

      Charleslow

      29 Jul 25 at 9:51 am

    19. В этой статье вы найдете познавательную и занимательную информацию, которая поможет вам лучше понять мир вокруг. Мы собрали интересные данные, которые вдохновляют на размышления и побуждают к действиям. Открывайте новую информацию и получайте удовольствие от чтения!
      Подробнее – https://lifebnf.bonificaoficial.com/2024/09/25/ola-mundo

      Gilbertsep

      29 Jul 25 at 9:54 am

    20. novosti24-913

      29 Jul 25 at 10:08 am

    21. RichardPep

      29 Jul 25 at 10:10 am

    22. novosti24-989

      29 Jul 25 at 10:13 am

    23. Публикация приглашает вас исследовать неизведанное — от древних тайн до современных достижений науки. Вы узнаете, как случайные находки превращались в революции, а смелые мысли — в новые эры человеческого прогресса.
      Нажмите, чтобы узнать больше – https://finalreview.in/index.php/2023/06/08/neelkanth-rockminerals-ltd

      Lionelzef

      29 Jul 25 at 10:14 am

    24. Ставки на спорт могут добавить азарт в просмотр матчей. Бонусные предложения способны существенно увеличить стартовый банк. Платежные методы содержат карты, электронные кошельки и криптовалюты. Официальный ресурс вавада сайт открыт для новых пользователей. Игроки ценят удобство интерфейса и быструю обработку ставок.

      Robertgew

      29 Jul 25 at 10:14 am

    25. Having read this I believed it was rather informative.
      I appreciate you finding the time and effort to put this short article together.
      I once again find myself personally spending way too much time both reading and leaving comments.
      But so what, it was still worthwhile!

      Profile

      29 Jul 25 at 10:21 am

    26. novosti24-116

      29 Jul 25 at 10:23 am

    27. Drug prescribing information. What side effects?
      how to buy generic pioglitazone no prescription
      Everything information about meds. Read now.

    28. В этой статье вы найдете познавательную и занимательную информацию, которая поможет вам лучше понять мир вокруг. Мы собрали интересные данные, которые вдохновляют на размышления и побуждают к действиям. Открывайте новую информацию и получайте удовольствие от чтения!
      Узнать напрямую – https://www.podasarbolado.com/planes-de-gestion

      Matthewepinc

      29 Jul 25 at 10:30 am

    29. novosti24-855

      29 Jul 25 at 10:32 am

    30. Этот текст призван помочь читателю расширить кругозор и получить практические знания. Мы используем простой язык, наглядные примеры и структурированное изложение, чтобы сделать обучение максимально эффективным и увлекательным.
      Что ещё? Расскажи всё! – https://f5fashion.vn/chi-tiet-hon-53-ve-chocolate-tang-sinh-nhat

      Charlessuelp

      29 Jul 25 at 10:44 am

    31. Эта статья предлагает живое освещение актуальной темы с множеством интересных фактов. Мы рассмотрим ключевые моменты, которые делают данную тему важной и актуальной. Подготовьтесь к насыщенному путешествию по неизвестным аспектам и узнайте больше о значимых событиях.
      Продолжить изучение – https://f5fashion.vn/xac-dinh-cong-thuc-hinh-thanh-nen-kim-cuong-hong

      Derrickdog

      29 Jul 25 at 10:46 am

    32. В этом информативном тексте представлены захватывающие события и факты, которые заставят вас задуматься. Мы обращаем внимание на важные моменты, которые часто остаются незамеченными, и предлагаем новые перспективы на привычные вещи. Подготовьтесь к тому, чтобы быть поглощенным увлекательными рассказами!
      Секреты успеха внутри – https://babi-beauty.fr/signature-styles-the-best-haircuts-at-our-barber-shop

      Coreynax

      29 Jul 25 at 10:51 am

    33. купить диплом вуза с реестром [url=http://www.arus-diplom31.ru]купить диплом вуза с реестром[/url] .

    34. кайтинг Кайт школа: надёжный старт в мир кайтсёрфинга. Профессиональные инструкторы, современное оборудование, безопасные условия обучения – все это ждет вас в кайт школе.

      RamonLiata

      29 Jul 25 at 10:55 am

    35. Бонусные предложения могут значительно увеличить стартовый банк. Регистрация занимает несколько минут благодаря простой форме. Официальный ресурс https://blunavycrociere.com/ открыт для новых пользователей. Ставки на спорт могут добавить адреналин в просмотр матчей. Платежные методы содержат карты, электронные кошельки и криптовалюты.

      Robertgew

      29 Jul 25 at 10:56 am

    36. Мы предлагаем вам подробное руководство, основанное на проверенных источниках и реальных примерах. Каждая часть публикации направлена на то, чтобы помочь вам разобраться в сложных вопросах и применить знания на практике.
      Читать дальше – https://www.rapanalysis.com/kickknowledge/2018/05/11/20-the-next-rhyme-i-write-might-be-about-you-w-martin-connor

      JosephSic

      29 Jul 25 at 10:59 am

    37. купить программу 1с для ип [url=https://kupit-1s22.ru/]kupit-1s22.ru[/url] .

      kypit 1s_udmt

      29 Jul 25 at 11:01 am

    38. Для тех, кто в теме, будет очень актуально:

      Особенно понравился материал про cyq.ru.

      Ссылка ниже:

      [url=https://cyq.ru]https://cyq.ru[/url]

      Надеюсь, смог помочь.

      rusPoito

      29 Jul 25 at 11:02 am

    39. After I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I receive 4 emails
      with the exact same comment. There has to be an easy method you can remove me from that service?
      Thank you!

    40. Этот информационный материал собраны данные, которые помогут лучше понять текущие тенденции и процессы в различных сферах жизни. Мы предоставляем четкий анализ, графики и примеры, чтобы информация была не только понятной, но и практичной для принятия решений.
      Узнать напрямую – https://komisite.ru

      LutherVAP

      29 Jul 25 at 11:07 am

    41. Эта информационная статья содержит полезные факты, советы и рекомендации, которые помогут вам быть в курсе последних тенденций и изменений в выбранной области. Материал составлен так, чтобы быть полезным и понятным каждому.
      Все материалы собраны здесь – http://www.shopsv.ru

      Jorgevem

      29 Jul 25 at 11:08 am

    42. прогнозы на матчи [url=http://prognoz-na-segodnya-na-sport8.ru]http://prognoz-na-segodnya-na-sport8.ru[/url] .

    43. кайтсёрфинг Доска для кайтсерфинга: выбор в зависимости от стиля

      RamonLiata

      29 Jul 25 at 11:22 am

    44. 이건 정말 매혹적이고 것입니다, 귀하는 매우
      전문적인 블로거입니다. 당신의 RSS 피드에 가입했고,
      당신의 웅장한 포스트를 더 찾고 있습니다.
      또한, 제 소셜 네트워크에서 당신의 웹사이트를
      공유했습니다.

      Nice answer back in return of this issue with genuine arguments and describing everything
      about that.

    Leave a Reply