Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>First of all, there are no events in PHP</h2> <p>An event is an abstraction for callback functions with their name. Typically, we'd define them as <code>$eventName =&gt; $listener</code>, where <code>$listener</code> is a callback function for the <code>$eventName</code></p> <p></p> <h2>What is the difference between events and regular callback functions?</h2> <p>Again - the core point to remember, is that events are callback functions. Nothing more.</p> <p>The only difference between them, is how we do invoke them. </p> <blockquote> <blockquote> <p><sub>An event is defined on bootstrap step with its required arguments, but invoked on demand without arguments. While the callback function is invoked with arguments and only</p> </blockquote> </blockquote> <p>Consider this example,</p> <pre><code>&lt;?php $eventManager = new EventManager(); $eventManager-&gt;attach('my_event', function(){ print_r(func_get_args()); }, array('foo', 'bar')); </code></pre> <p>As we have just defined an event, we'd invoke like,</p> <p><code>$eventManager-&gt;trigger('my_event');</code></p> <p>This will output: <code>Array([0] =&gt; [foo], [1] =&gt; [bar]</code></p> <h2>That's related to JavaScript!</h2> <p>Since most of us are familiar with JavaScript even-driven architecture, its worth nothing to mention an example of its common usage:</p> <pre><code>var a = document.getElementsByTagName('a')[0]; a.onclick = function(event) { // &lt;-- We define an event with the event argument event.preventDefault(); alert('A element was clicked'); } a.click(); // &lt;-- but we invoke it without arguments // or If you want a Jquery $("a").click(function(event){ event.preventDefault(); alert('A element was clicked'); }); $("a").click(); </code></pre> <p>Since in PHP we don't have such event-driven nature, we can replace it with our own class that manage events and takes a full advantage of it.</p> <h2>Why use them?</h2> <p>While events confuse so many people, they are extremely useful.</p> <p>Imagine you have a <strong>Content Management System (CMS)</strong>, where your users can decide how to handle <code>404</code> errors. Say, they can handle with <br /></p> <p>1) Show a blank page <br /> 2) Redirect to <code>/</code> <br /> 3) Show a custom message <br /></p> <p>Without events you would have to do it, like</p> <pre><code>if ($router-&gt;isMatched($request)){ //do dispatch etc } else { // Here you start handling 404 errors switch($config-&gt;read('404_way_handle')){ case 'show_blank': die(); break; case 'show_msg': echo 'Some custom message'; break; case 'redirect': // do redirect break; } } </code></pre> <p>With an event you can simplify the readability and keep the code more maintainable:</p> <pre><code>if ($router-&gt;isMatched($request)){ // do dispatch } else { $eventManager-&gt;trigger('404_handler'); } </code></pre> <p>while <code>404_handler</code> itself looks like</p> <pre><code> $eventManager-&gt;attach('404_handler', function(){ switch($config-&gt;read('404_way_handle')){ case 'show_blank': die(); break; case 'show_msg': echo 'Some custom message'; break; case 'redirect': // do redirect break; } }, $config); </code></pre> <p><br /></p> <h2>So let's break it down</h2> <p>1) Events improve readability, which is great for future<br /></p> <p>2) Events do adhere to the <strong>Single-Responsibility Principle</strong>, because you can simply inject <code>$eventManager</code> to your classes that need it, while callback functions could break it or could introduce a <strong>global state</strong> too (Which is bad for unit-testings).</p> <p>3) There are distinct types of logic - template logic, business logic, error handler logic, data access logic etc etc. Events <strong>do simplify</strong> your application logic by decoupling business (or another kind) logic from its <strong>configuration</strong> logic, so that you end up with clear application logic.</p> <blockquote> <blockquote> <p>You can watch this <a href="http://www.youtube.com/watch?v=O6dwYklDVp0" rel="nofollow">lecture</a> if you want to know how they do work in Zend Framework 2 (watch it even if you're not familiar with Zend Framework 2)</sub></p> </blockquote> </blockquote> <p><strong>Events in MVC-related architectures</strong></p> <p>Since you've been talking about frameworks, its worth nothing to mention, that there could be events in <code>MVC-related</code> architectures too. And since events are callback functions you can abstract common <code>boostrap</code> events in your MVC-like architecture, like this.</p> <pre><code>$mvcEvent-&gt;on(MVC_EVENT::ROUTE_MATCH, function(){ $mvcEvent-&gt;on(MVC_EVENT::DISTPATCH, function($content){ echo $mvcEvent-&gt;trigger(MVC_EVENT::RENDER, $content); }); }); </code></pre> <p>Note : <strong>In pure MVC theory, there are no events at all</strong>. They do acts as helpers, but again - in frameworks you can abstract them and call them "events".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload