12

Responder

  1. Object
    1. Responder

A Responder instance automatically sends notification messages to one or several instances. Responder instances can be linked so notifications are automatically transmitted from one responder to another.

INSTANCE METHODS

  1. namespace Responder;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. defclass('Responder', null, 1, null, array('nextResponders'), null, array('respondTo', 'nextResponders', 'setNextResponders', 'addNextResponder', 'removeNextResponder'));

The Responder class inherits from the Object class. The instance property nextResponders holds the list of instances which have asked to be notified when an event occurs. The Responder class adds the instance messages respondTo, nextResponders, setNextResponders, addNextResponder and removeNextResponder.

nextResponders
SYNOPSIS

sendmsg($self, 'nextResponders')

DESCRIPTION

nextResponders returns the list of responders of $self in an array.

CODE
  1. function i_nextResponders($self) {
  2.     return sendmsg($self, 'get', 'nextResponders');
  3. }
setNextResponders
SYNOPSIS

sendmsg($self, 'setNextResponders', $responders)

DESCRIPTION

setNextResponders initializes the list of responders of $self to $responders. $responders is an array of instances.

CODE
  1. function i_setNextResponders($self, $responders) {
  2.     return sendmsg($self, 'set', 'nextResponders', $responders);
  3. }
addNextResponder
SYNOPSIS

sendmsg($self, 'addNextResponder', $r)

DESCRIPTION

addNextResponder adds $r to the list of responders of $self.

CODE
  1. function i_addNextResponder($self, $r) {
  2.     $responders=sendmsg($self, 'get', 'nextResponders');
  3.  
  4.     if ($responders)  {
  5.         if (!in_array($r, $responders)) {
  6.             $responders[]=$r;
  7.             sendmsg($self, 'set', 'nextResponders', $responders);
  8.         }
  9.     }
  10.     else {
  11.         $responders=array($r);
  12.         sendmsg($self, 'set', 'nextResponders', $responders);
  13.     }
  14.  
  15.     return $self;
  16. }
removeNextResponder
SYNOPSIS

sendmsg($self, 'removeNextResponder', $r)

DESCRIPTION

removeNextResponder removes $r from the list of responders of $self.

CODE
  1. function i_removeNextResponder($self, $r) {
  2.     $responders=sendmsg($self, 'get', 'nextResponders');
  3.  
  4.     if ($responders) {
  5.         $i=array_search($r, $responders);
  6.  
  7.         if ($i !== false) {
  8.             unset($responders[$i]);
  9.             sendmsg($self, 'set', 'nextResponders', $responders);
  10.         }
  11.     }
  12.  
  13.     return $self;
  14. }
respondTo
SYNOPSIS

sendmsg($self, 'respondTo', $msg[, $arg ...])

DESCRIPTION
CODE

If self responds to $msg, respondTo sends $msg and its parameters $arg to $self. If $self doesn't respond to $msg or if the execution of the message returns false, respondTo transmits the message and its parameters to all the instances of the list of responders of $self.

  1. function i_respondTo($self, $msg) {
  2.     if (sendmsg($self, 'respondsTo', $msg) and sendmsg($self, 'perform', $msg, array_slice(func_get_args(), 2))) {
  3.         return $self;
  4.     }
  5.  
  6.     $responders=sendmsg($self, 'get', 'nextResponders');
  7.  
  8.     if ($responders) {
  9.         foreach ($responders as $r) {
  10.             sendmsg($r, 'perform', 'respondTo', array_slice(func_get_args(), 1));
  11.         }
  12.     }
  13.  
  14.     return $self;
  15. }
EXAMPLE

An instance of Button notifies the message clicked when it receives the message click:

  1. namespace Button;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. require_once 'Responder.php';
  6.  
  7. defclass('Button', $Responder, 1, null, null, null, array('click'));
  8.  
  9. function i_click($self) {
  10.     echo $self, ' click', PHP_EOL;
  11.  
  12.     sendmsg($self, 'respondTo', 'clicked', $self);
  13. }

click displays a trace message then notifies the message clicked.

An instance of Action responds to clicked:

  1. namespace Action;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. require_once 'Responder.php';
  6.  
  7. defclass('Action', $Responder, 1, null, null, null, array('clicked'));
  8.  
  9. function i_clicked($self, $sender) {
  10.     echo $self . ' clicked from ', $sender, PHP_EOL;
  11.  
  12.     return false;
  13. }
$ php -a
php > require_once 'Button.php';
php > $btn1=sendmsg($Button, 'new');
php > sendmsg($btn1, 'click');
object(Button) click

Create an instance of Action and add it as a responder to the instance of Button:

php > require_once 'Action.php';
php > $act1=sendmsg($Action, 'new');
php > sendmsg($btn1, 'addNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)

clicked is automatically sent by $btn1 to $act1.

An instance of AnotherAction responds to clicked too:

  1. namespace AnotherAction;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. require_once 'Responder.php';
  6.  
  7. defclass('AnotherAction', $Responder, 1, null, null, null, array('clicked'));
  8.  
  9. function i_clicked($self, $sender) {
  10.     echo $self . ' clicked from ', $sender, PHP_EOL;
  11.  
  12.     return true;
  13. }

Add an instance of AnotherAction as a responder to the instance of Button:

php > require_once 'AnotherAction.php';
php > $act2=sendmsg($AnotherAction, 'new');
php > sendmsg($btn1, 'addNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)

clicked is sent by $btn1 to $act1 then to $act2.

Responders can build a chain:

php > sendmsg($btn1, 'removeNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)

$btn1 has only $act1 as responder. Add $act2 as a responder to $act1:

php > sendmsg($act1, 'addNextResponder', $act2);
php > sendmsg($btn1, 'click');
object(Button) click
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)

In this configuration, clicked is sent to the responder of $btn1 then to the responder of $act1.

If a responder returns true, the chain is broken:

php > sendmsg($act1, 'removeNextResponder', $act2);
php > sendmsg($btn1, 'removeNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
php > sendmsg($btn1, 'addNextResponder', $act2);
php > sendmsg($act2, 'addNextResponder', $act1);
php > sendmsg($btn1, 'click');
object(Button) click
object(AnotherAction) clicked from object(Button)

clicked is sent by $btn1 to $act2 which returns true and blocks sending the message to $act1.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].