-
-
Save barnabywalters/fa693c8b79a356c1325d to your computer and use it in GitHub Desktop.
Symfony Event Dispatcher Minimal Proof-of-Concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Symfony\Component\EventDispatcher\EventDispatcher, | |
Symfony\Component\EventDispatcher\Event; | |
class RandomEvent extends Event { | |
protected $data; | |
public function __construct($data) { | |
$this -> data = $data; | |
} | |
public function getData() | |
{ | |
return $this -> data; | |
} | |
} | |
$dis = new EventDispatcher; | |
$dis -> addListener('foo.action', function (RandomEvent $e) { | |
// Change something about $e | |
$data = $e -> getData(); | |
$data -> text = 'This text has been changed by the event listener'; | |
}); | |
$myData = new stdclass; | |
$myData -> text = 'This text as it was orignally';/**/ | |
echo '<p>Before event dispatch: <code>' . print_r($myData, true) . '</code>'; | |
echo '<p>Dispatching event…'; | |
$event = new RandomEvent($myData); | |
$dis -> dispatch('foo.action', $event); | |
echo '<p>After event dispatch: <code>' . print_r($myData, true) . '</code>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Before event dispatch: <code>stdClass Object | |
( | |
[text] => This text as it was orignally | |
) | |
</code><p>Dispatching event…<p>After event dispatch: <code>stdClass Object | |
( | |
[text] => This text has been changed by the event listener | |
) | |
</code> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment