30

Application

  1. Object
    1. Once
      1. Application

An Application class has only one instance. An Application instance can have a name. It can be associated to another instance which will interpret all the messages it receives but which it doesn't implement.

INSTANCE METHODS

  1. namespace Application;
  2.  
  3. use \InvalidArgumentException as InvalidArgumentException;
  4.  
  5. require_once 'So-o.php';
  6.  
  7. require_once 'Once.php';
  8.  
  9. defclass('Application', $Once, 1, null, array('appName', 'app'), null, array('init', 'appName', 'doesNotRecognize'));

The Application class inherits from the Once class. The instance properties appName and app contain the name of an Application instance and the reference to its proxy. The Application class redefines the instance messages init and doesNotRecognize. It adds the instance message appName.

init
SYNOPSIS

sendmsg($self, 'init', $appName=null, $app=null)

DESCRIPTION

init initializes the name and the proxy of $self with $appName and $app.

CODE
  1. function i_init($self, $appName=null, $app=null) {
  2.     if (!(is_null($appName) or is_string($appName))) {
  3.         throw new InvalidArgumentException();
  4.     }
  5.  
  6.     if (!(is_null($app) or is_object($app))) {
  7.         throw new InvalidArgumentException();
  8.     }
  9.  
  10.     supersend('init', func_get_args());
  11.  
  12.     if ($appName) {
  13.         sendmsg($self, 'set', 'appName', $appName);
  14.         if ($app) {
  15.             sendmsg($self, 'set', 'app', $app);
  16.         }
  17.     }
  18.  
  19.     return $self;
  20. }

Checks if $appName is either null or a string and if $app is either null or an object and triggers an InvalidArgumentException exception in case of error. Proceeds by running the init method of the superclass of the Application class. Initializes the instance properties appName and app with the parameters $appName and $app.

appName
SYNOPSIS

sendmsg($self, 'appName')

DESCRIPTION

appName returns the name of the Application instance $self. If $self has no name, appName returns null.

CODE
  1. function i_appName($self) {
  2.     return sendmsg($self, 'get', 'appName');
  3. }

Returns the appName property of $self.

doesNotRecognize
SYNOPSIS

sendmsg($self, 'doesNotRecognize', $msg)

DESCRIPTION

An Application instance intercepts the message doesNotRecognize which is automatically sent to a class or an instance which receives a message it doesn't implement.

doesNotRecognize returns the result of forwarding $msg and its parameters to the proxy of $self, the instance which was passed as a parameter to the new message to create $self.

If $self has no proxy, doesNotRecognize executes the message doesNotRecognize in the context of the superclass of the Application class, which will run the doesNotRecognize method of the Object class, which triggers a PHP error.

CODE
  1. function i_doesNotRecognize($self, $msg) {
  2.     $app=sendmsg($self, 'get', 'app');
  3.  
  4.     if (!$app) {
  5.         return supersend('doesNotRecognize', func_get_args());
  6.     }
  7.  
  8.     return sendmsg($app, 'perform', $msg, array_slice(func_get_args(), 2));
  9. }

Initializes $app to the value of the app property of $self. If $app is null, returns the result of executing the message doesNotRecognize by the superclass of the Application class. Otherwise, if $self has a proxy, returns the result of sending $msg and its parameters to $app.

EXAMPLE
$ php -a
php > require_once 'Hello.php';
php > $hello=sendmsg($Hello, 'new');
php > sendmsg($hello, 'hello');
Hello from So-o!

Create an instance of Application with $hello as a proxy:

php > require_once 'Application.php';
php > $app=sendmsg($Application, 'new', 'Hello', $hello);
php > sendmsg($app, 'appName');
Hello

If you send the message hello to $app, it's interpreted by $hello:

php > sendmsg($app, 'hello');
Hello from So-o!

Try an unknown message:

php > sendmsg($app, 'foobar');
PHP Fatal error:  Hello::foobar Invalid instance message

Try without a proxy:

php > sendmsg($app, 'set', 'app', null);
php > sendmsg($app, 'hello');
PHP Fatal error:  Application::hello Invalid instance message
SEE ALSO

Once, Responder

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].