Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Good reference material on how to code an event-driven application</p> </blockquote> <p>You can either do this with <a href="http://php.net/manual/en/language.pseudo-types.php">"dumb" callbacks</a> (<a href="http://codepad.viper-7.com/bJJNif">Demo</a>):</p> <pre><code>class Hooks { private $hooks; public function __construct() { $this-&gt;hooks = array(); } public function add($name, $callback) { // callback parameters must be at least syntactically // correct when added. if (!is_callable($callback, true)) { throw new InvalidArgumentException(sprintf('Invalid callback: %s.', print_r($callback, true))); } $this-&gt;hooks[$name][] = $callback; } public function getCallbacks($name) { return isset($this-&gt;hooks[$name]) ? $this-&gt;hooks[$name] : array(); } public function fire($name) { foreach($this-&gt;getCallbacks($name) as $callback) { // prevent fatal errors, do your own warning or // exception here as you need it. if (!is_callable($callback)) continue; call_user_func($callback); } } } $hooks = new Hooks; $hooks-&gt;add('event', function() {echo 'morally disputed.';}); $hooks-&gt;add('event', function() {echo 'explicitly called.';}); $hooks-&gt;fire('event'); </code></pre> <p>Or implementing a pattern often used in event-driven applications: <a href="http://en.wikipedia.org/wiki/Observer_pattern"><em>Observer Pattern</em></a>.</p> <blockquote> <p>Code snippet(s) showing how to put a "hook" in a function in PHP</p> </blockquote> <p>The manual link above (callbacks can be stored into a variable) and some <a href="http://en.wikipedia.org/wiki/Observer_pattern#PHP">PHP code examples for the Observer Pattern</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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