Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an approach I've used, it's an attempt to copy from Qt signals/slots mechanism, a kind of Observer pattern. Objects can emit signals. Every signal has an ID in the system - it's composed by sender's id + object name Every signal can be binded to the receivers, which simply is a "callable" You use a bus class to pass the signals to anybody interested in receiving them When something happens, you "send" a signal. Below is and example implementation</p> <pre><code> &lt;?php class SignalsHandler { /** * hash of senders/signals to slots * * @var array */ private static $connections = array(); /** * current sender * * @var class|object */ private static $sender; /** * connects an object/signal with a slot * * @param class|object $sender * @param string $signal * @param callable $slot */ public static function connect($sender, $signal, $slot) { if (is_object($sender)) { self::$connections[spl_object_hash($sender)][$signal][] = $slot; } else { self::$connections[md5($sender)][$signal][] = $slot; } } /** * sends a signal, so all connected slots are called * * @param class|object $sender * @param string $signal * @param array $params */ public static function signal($sender, $signal, $params = array()) { self::$sender = $sender; if (is_object($sender)) { if ( ! isset(self::$connections[spl_object_hash($sender)][$signal])) { return; } foreach (self::$connections[spl_object_hash($sender)][$signal] as $slot) { call_user_func_array($slot, (array)$params); } } else { if ( ! isset(self::$connections[md5($sender)][$signal])) { return; } foreach (self::$connections[md5($sender)][$signal] as $slot) { call_user_func_array($slot, (array)$params); } } self::$sender = null; } /** * returns a current signal sender * * @return class|object */ public static function sender() { return self::$sender; } } class User { public function login() { /** * try to login */ if ( ! $logged ) { SignalsHandler::signal(this, 'loginFailed', 'login failed - username not valid' ); } } } class App { public static function onFailedLogin($message) { print $message; } } $user = new User(); SignalsHandler::connect($user, 'loginFailed', array($Log, 'writeLog')); SignalsHandler::connect($user, 'loginFailed', array('App', 'onFailedLogin')); $user-&gt;login(); ?&gt; </code></pre>
    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. 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.
    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