Re: the most amazing php code i have ever seen so far
| From: | Nick Stinemates | Date: | Fri, 25 Apr 2008 17:29:32 +0000 |
| Subject: | Re: the most amazing php code i have ever seen so far | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-273580@lists.php.net to get a copy of this message | ||
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@stinemates.org)
http://nick.stinemates.org