Interface
The functional interface of So-o in PHP consists of 3 functions:
defclass
which defines a new class, sendmsg
which is systematically used to send a message to a class or an instance, and supersend
which runs a method inherited from a superclass.
All the code for the So-o interface is in the file So-o.php.
- require_once 'OL.php';
- require_once 'Object.php';
Loading the code for So-o includes the code for the Object Layer and the Object class.
defclass
SYNOPSIS
defclass($name, $superclass, $revision, $class_properties, $instance_properties, $class_messages, $instance_messages)
DESCRIPTION
defclass
defines a class.
$name
specifies the name of the class. $name
must be a valid PHP variable name.
$superclass
is the global reference of the superclass of the new class.
If $superclass
is null
, the new class inherits by default from the Object class, $Object
.
$revision
gives the revision number of the class. $revision
is an integer > 0 which can be used to differentiate successive versions of the class.
$class_properties
, $instance_properties
, $class_messages
and $instance_messages
list the properties and the messages of the class and instances of the class.
Each of these parameters is an array of strings or null
.
A property or a message must be a valid PHP variable name.
defclass
creates the global variable $name
.
Notice that a class can be redefined.
In case of error, defclass
triggers the exception InvalidArgumentException.
A new class automatically receives the message initialize.
NOTE: The initialize
class method defined by the Object class does nothing.
EXAMPLE
- namespace Hello;
The class Hello is defined in a namespace with the same name. A class always places its code in its own namespace.
- require_once 'So-o.php';
Loads the code for So-o. The file So-o.php automatically includes the Object class.
- defclass('Hello', null, 1, null, null, null, array('hello'));
Defines the class Hello and associates it to the global variable $Hello
.
The Hello class inherits by default from the Object class.
Its revision number is 1.
It doesn't have any class properties, class messages or instance properties.
It has an instance message: hello.
- function i_hello($self) {
- echo 'Hello from So-o!', PHP_EOL;
- return $self;
- }
Defines the code of the instance message hello.
An instance method is a function whose name is the instance message prefixed by i_
.
A class method is a function whose name is the class message prefixed by c_
.
A class or an instance method is defined in the naming space of the class.
The first argument of a method is always the class or the instance which receives the message.
Conventionally, this argument has the variable name $self
.
A method which has nothing in particular to return generally returns $self
.
$ php -a
php > require_once 'Hello.php';
php > echo $Hello;
class(Hello)
php > $hello=sendmsg($Hello, 'new');
php > echo $hello;
object(Hello)
php > sendmsg($hello, 'hello');
Hello from So-o!
CODE
- function defclass($name, $superclass, $revision, $class_properties, $instance_properties, $class_messages, $instance_messages) {
defclass
takes 7 arguments.
$name
specifies the name of the class.
$superclass
is the global reference of the superclass of the new class.
$revision
gives the revision number of the class.
$class_properties
, $instance_properties
, $class_messages
and $instance_messages
list the properties and the messages of the class and the instances of the class.
- $class=new \OL\Definition($name, $superclass, $revision, $class_properties, $instance_properties, $class_messages, $instance_messages);
Creates an object of the class Definition defined by the Object Layer in the namespace OL with the parameters of the call to defclass
.
- $GLOBALS[$name]=$class;
Assigns the class to a global variable whose name is the name of the class.
- if ('Object' != $name) {
- \OL\class_send_message($class, 'initialize');
- }
Sends the message initialize to the new class if it's not the Object class.
- return $class;
- }
Returns the class.
sendmsg
SYNOPSIS
sendmsg($receiver, $msg[, $arg ...])
DESCRIPTION
sendmsg
returns the result of sending the message $msg
and its parameters $arg
to the instance or the class $receiver
.
CODE
- function sendmsg($receiver, $msg) {
- return $receiver->sendself($msg, array_slice(func_get_args(), 2));
- }
sendmsg
calls the method sendself
of $receiver
with in argument $msg
and an array containing all the other arguments following $msg
.
supersend
SYNOPSIS
supersend($msg, $args)
DESCRIPTION
supersend
returns the result of sending the message $msg
and its parameters to an instance or a class in the context of the superclass of the class whose method has called supersend
.
$args
is an array whose first element is the instance or the class which is sending and receiving the message, e.g. $self
.
$args
can contain other parameters which will be transmitted with $msg
.
supersend
is generally used to call the inherited version of a method which is redefined by a subclass.
Calling supersend
outside a method is a case of error which triggers a LogicException exception.
CODE
- function supersend($msg, $args) {
- $receiver=array_shift($args);
- return $receiver->sendsuper($msg, $args);
- }
supersend
extracts $receiver
from $args
and calls the method sendsuper
of $receiver
with in argument $msg
and all the arguments remaining in $args
.
EXAMPLE
- namespace X;
- require_once 'So-o.php';
- defclass('X', null, 1, array('count'), array('value'), array('initialize', 'new', 'count'), array('free', 'init'));
The X class counts its instances in the class property count. It redefines the class messages initialize and new and the instance messages free and init. It adds the instance property value and the class message count.
- function c_initialize($self) {
- return sendmsg($self, 'set', 'count', 0);
- }
c_initialize
initializes count to 0 when the class is constructed.
NOTE: The initialize message is automatically sent to a new class by defclass
.
- function c_new($self) {
- $i=supersend('new', func_get_args());
- sendmsg($self, 'set', 'count', sendmsg($self, 'get', 'count') + 1);
- return $i;
- }
c_new
creates an instance of X by sending the message new in the context of the Object class then increments count before returning the new instance.
Notice how the PHP function func_get_args
allows to easily build the call to supersend
.
- function c_count($self) {
- return sendmsg($self, 'get', 'count');
- }
c_count
returns the number of instances of X.
- function i_free($self) {
- $count=sendmsg(sendmsg($self, 'class'), 'get', 'count');
- sendmsg(sendmsg($self, 'class'), 'set', 'count', $count - 1);
- supersend('free', func_get_args());
- }
i_free
decrements count then executes the message in the context of the Object class.
Notice how an instance method sends a message to its class.
NOTE: The free message is automatically sent to an instance which is not referenced anymore.
- function i_init($self, $value=0) {
- supersend('init', func_get_args());
- sendmsg($self, 'set', 'value', $value);
- return $self;
- }
i_init
executes the init message in the context of the Object class then initializes value with the parameter $value
of the message.
NOTE: The init message is automatically sent to a new instance by new.
$ php -a
php > require_once 'X.php';
php > echo sendmsg($X, 'count'), PHP_EOL;
0
php > $x1=sendmsg($X, 'new');
php > echo sendmsg($x1, 'get', 'value'), PHP_EOL;
0
php > $x2=sendmsg($X, 'new', 2);
php > echo sendmsg($x2, 'get', 'value'), PHP_EOL;
2
php > echo sendmsg($X, 'count'), PHP_EOL;
2
php > unset($x2);
php > echo sendmsg($X, 'count'), PHP_EOL;
1
php > quit
Comments