Skip to content

Instantly share code, notes, and snippets.

@basz
Created April 10, 2012 12:15
Show Gist options
  • Save basz/2350974 to your computer and use it in GitHub Desktop.
Save basz/2350974 to your computer and use it in GitHub Desktop.
events - locator - lazy vs non lazy
// Could the event manager handle this better? I would want the lazy behavior without typing as much...
$events = StaticEventManager::getInstance();
// obvious method (Application\Service\StoreService is instanciated even when the event has not been triggered)
$events->attach('Application\Controller\PaypalController', 'transaction.completed', array($locator->get('Application\Service\StoreService'), 'processTransaction'), 100);
// lazy method (Application\Service\StoreService is instanciated when the event has been triggered)
$events->attach('Application\Controller\PaypalController', 'transaction.completed', function($e) use ($locator) {
$service = $locator->get('Application\Service\StoreService');
$service->processTransaction($e);
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment