Application
- Object
- Once
- Application
- Once
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
- namespace Application;
- use \InvalidArgumentException as InvalidArgumentException;
- require_once 'So-o.php';
- require_once 'Once.php';
- 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
- function i_init($self, $appName=null, $app=null) {
- if (!(is_null($appName) or is_string($appName))) {
- throw new InvalidArgumentException();
- }
- if (!(is_null($app) or is_object($app))) {
- throw new InvalidArgumentException();
- }
- supersend('init', func_get_args());
- if ($appName) {
- sendmsg($self, 'set', 'appName', $appName);
- if ($app) {
- sendmsg($self, 'set', 'app', $app);
- }
- }
- return $self;
- }
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
- function i_appName($self) {
- return sendmsg($self, 'get', 'appName');
- }
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
- function i_doesNotRecognize($self, $msg) {
- $app=sendmsg($self, 'get', 'app');
- if (!$app) {
- return supersend('doesNotRecognize', func_get_args());
- }
- return sendmsg($app, 'perform', $msg, array_slice(func_get_args(), 2));
- }
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
Comments