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

[email protected] (Nick Stinemates)
Newsgroups php.general
Message-ID <20080425172932.GB11613@deviL>
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 ([email protected])
http://nick.stinemates.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.