<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.amar.com/index.php?action=history&amp;feed=atom&amp;title=Notepad%3APHP_Result</id>
	<title>Notepad:PHP Result - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.amar.com/index.php?action=history&amp;feed=atom&amp;title=Notepad%3APHP_Result"/>
	<link rel="alternate" type="text/html" href="https://wiki.amar.com/index.php?title=Notepad:PHP_Result&amp;action=history"/>
	<updated>2026-04-08T01:51:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.amar.com/index.php?title=Notepad:PHP_Result&amp;diff=443&amp;oldid=prev</id>
		<title>Lax: Created page with &quot;&lt;pre class=&quot;prettyprint&quot;&gt; &lt;?php  /**  * Event Bus Challenge  *   * FIRST: COPY/PASTE ALL THIS CODE IN THE TEXTFIELD BELOW AND EDIT IT THERE  *   * 1. The goal is to build a ve...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.amar.com/index.php?title=Notepad:PHP_Result&amp;diff=443&amp;oldid=prev"/>
		<updated>2015-05-22T21:12:24Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;pre class=&amp;quot;prettyprint&amp;quot;&amp;gt; &amp;lt;?php  /**  * Event Bus Challenge  *   * FIRST: COPY/PASTE ALL THIS CODE IN THE TEXTFIELD BELOW AND EDIT IT THERE  *   * 1. The goal is to build a ve...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre class=&amp;quot;prettyprint&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Event Bus Challenge&lt;br /&gt;
 * &lt;br /&gt;
 * FIRST: COPY/PASTE ALL THIS CODE IN THE TEXTFIELD BELOW AND EDIT IT THERE&lt;br /&gt;
 * &lt;br /&gt;
 * 1. The goal is to build a very simple PubSub/event class in PHP. &lt;br /&gt;
 *    We will create an EventEmitter object and then we&amp;#039;ll subscribe to events and trigger them. &lt;br /&gt;
 *    Subscribing to an event simply adds a callback to be run when the event is triggered. &lt;br /&gt;
 *    Triggering an event (emit) should run all the attached callbacks.&lt;br /&gt;
 * 2. Don&amp;#039;t overthink it. The solution should only take a few minutes and a few lines of code. &lt;br /&gt;
 *    Build only what you need to get the desired ouput.&lt;br /&gt;
 * &lt;br /&gt;
 * Constraints:&lt;br /&gt;
 * 1. Although we only use error/success events, please build the class to handle arbitrary events.&lt;br /&gt;
 * 2. Events data will always be an associative array.&lt;br /&gt;
 * 3. A callback should always be safe to call.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class EventEmitter {&lt;br /&gt;
&lt;br /&gt;
    var $current_callback;&lt;br /&gt;
    &lt;br /&gt;
    public function __construct() {&lt;br /&gt;
        // no special needs I can see&lt;br /&gt;
        $this-&amp;gt;current_callback = array();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function emit($description , $data) {&lt;br /&gt;
        // emit an message using previsouly subscribed function&lt;br /&gt;
        // loop through all the previous functions.&lt;br /&gt;
        if (!empty($this-&amp;gt;current_callback[$description])) {&lt;br /&gt;
            foreach ($this-&amp;gt;current_callback[$description] as $current) {&lt;br /&gt;
                call_user_func($current, $data);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function subscribe($description, $callback_func_name) {&lt;br /&gt;
        // callback function specified. &lt;br /&gt;
        // add to array&lt;br /&gt;
        if (empty($this-&amp;gt;current_callback[$description])) {&lt;br /&gt;
            $this-&amp;gt;current_callback[$description] = array();&lt;br /&gt;
        }&lt;br /&gt;
        $this-&amp;gt;current_callback[$description][] = $callback_func_name;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$emitter = new EventEmitter;&lt;br /&gt;
&lt;br /&gt;
$error_callback = function($data) {&lt;br /&gt;
    echo &amp;quot;Error 1. {$data[&amp;quot;message&amp;quot;]} \n&amp;quot;;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
$error_callback2 = function($data) {&lt;br /&gt;
    echo &amp;quot;Error 2. {$data[&amp;quot;message&amp;quot;]} \n&amp;quot;;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
$success_callback = function($data) {&lt;br /&gt;
    echo &amp;quot;SUCCESS! {$data[&amp;quot;message&amp;quot;]} \n&amp;quot;;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
$emitter-&amp;gt;emit(&amp;quot;error&amp;quot;, [&amp;quot;message&amp;quot; =&amp;gt; &amp;quot;Error one.&amp;quot;]);&lt;br /&gt;
&lt;br /&gt;
$emitter-&amp;gt;subscribe(&amp;quot;error&amp;quot;, $error_callback);&lt;br /&gt;
$emitter-&amp;gt;emit(&amp;quot;error&amp;quot;, [&amp;quot;message&amp;quot; =&amp;gt; &amp;quot;Second error.&amp;quot;]);&lt;br /&gt;
&lt;br /&gt;
$emitter-&amp;gt;subscribe(&amp;quot;error&amp;quot;, $error_callback2);&lt;br /&gt;
$emitter-&amp;gt;emit(&amp;quot;error&amp;quot;, [&amp;quot;message&amp;quot; =&amp;gt; &amp;quot;Yet another error.&amp;quot;]);&lt;br /&gt;
&lt;br /&gt;
$emitter-&amp;gt;subscribe(&amp;quot;success&amp;quot;, $success_callback);&lt;br /&gt;
$emitter-&amp;gt;emit(&amp;quot;success&amp;quot;, [&amp;quot;message&amp;quot; =&amp;gt; &amp;quot;Great success!.&amp;quot;]);&lt;br /&gt;
&lt;br /&gt;
// Expected output:&lt;br /&gt;
&lt;br /&gt;
// Error 1. Second error.&lt;br /&gt;
// Error 1. Yet another error.&lt;br /&gt;
// Error 2. Yet another error.&lt;br /&gt;
// SUCCESS! Great success!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Lax</name></author>
	</entry>
</feed>