Re: the most amazing php code i have ever seen so far

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Apr 24, 2008 at 10:41:05AM -0400, Eric Butera wrote:
> I can't show you the code because I wrote it on company time therefore
> I don't own it.
> 
> A very generalized example.
> 
> Register events:
> $events = array(
> 	array('event'=>'onSave', 'class'=>'localEvents', 'method'=>'handleOnSave')
> );
> $dispatcher = namespace_registry::get('dispatcher');
> $dispatcher->lazyRegister($events);
> 
> 
> Sample save page:
> $record       = new record;
> $record->name = 'Eric Butera'
> $isSuccess    = $record->save();
> $dispatcher->trigger('onSave', $record, array('isSuccess'=>$isSuccess));
> 
> 
> Upon firing, the dispatcher would check to see if any lazy registrants
> exist for the onSave event.  If some exist, then it will then use the
> configured settings to somehow load & call the defined class/method
> and inject it with an event notification object.
> 
> Sample event handler:
> class localEvents {
>     public function handleOnSave(namespace_Event $event) {
>         echo "An onsave event is being handled, triggered in subject
> class ". get_class($event->getSubject()) ."\n";
>         $info = $event->getInfo();
>         if ($info['isSuccess'] === true) {
>             // do something else!
>         }
>     }
> }
> 
> 
> Why is this useful?  Because you can add functionality to your save
> page without touching it.  This is very powerful when you have a
> shared code base and need to add some parts to it without breaking
> other sites.  Since it is expected that events will be registered it
> is okay to rely on this functionality always being there rather than
> the internals of your app changing and creating nightmares.

That is a beatiful example of the observer pattern.

-- 
Nick Stinemates (nick@xxxxxxxxxxxxxx)
http://nick.stinemates.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux