19

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.

  1. require_once 'OL.php';
  2. 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
  1. namespace Hello;

The class Hello is defined in a namespace with the same name. A class always places its code in its own namespace.

  1. require_once 'So-o.php';

Loads the code for So-o. The file So-o.php automatically includes the Object class.

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

  1. function i_hello($self) {
  2.     echo 'Hello from So-o!', PHP_EOL;
  3.  
  4.     return $self;
  5. }

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

  1.     $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.

  1.     $GLOBALS[$name]=$class;

Assigns the class to a global variable whose name is the name of the class.

  1.     if ('Object' != $name) {
  2.         \OL\class_send_message($class, 'initialize');
  3.     }

Sends the message initialize to the new class if it's not the Object class.

  1.     return $class;
  2. }

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
  1. function sendmsg($receiver, $msg) {
  2.     return $receiver->sendself($msg, array_slice(func_get_args(), 2));
  3. }

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
  1. function supersend($msg, $args) {
  2.     $receiver=array_shift($args);
  3.  
  4.     return $receiver->sendsuper($msg, $args);
  5. }

supersend extracts $receiver from $args and calls the method sendsuper of $receiver with in argument $msg and all the arguments remaining in $args.

EXAMPLE
  1. namespace X;
  2.  
  3. require_once 'So-o.php';
  4.  
  5. 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.

  1. function c_initialize($self) {
  2.     return sendmsg($self, 'set', 'count', 0);
  3. }

c_initialize initializes count to 0 when the class is constructed. NOTE: The initialize message is automatically sent to a new class by defclass.

  1. function c_new($self) {
  2.     $i=supersend('new', func_get_args());
  3.  
  4.     sendmsg($self, 'set', 'count', sendmsg($self, 'get', 'count') + 1);
  5.  
  6.     return $i;
  7. }

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.

  1. function c_count($self) {
  2.     return sendmsg($self, 'get', 'count');
  3. }

c_count returns the number of instances of X.

  1. function i_free($self) {
  2.     $count=sendmsg(sendmsg($self, 'class'), 'get', 'count');
  3.  
  4.     sendmsg(sendmsg($self, 'class'), 'set', 'count', $count - 1);
  5.  
  6.     supersend('free', func_get_args());
  7. }

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.

  1. function i_init($self, $value=0) {
  2.     supersend('init', func_get_args());
  3.  
  4.     sendmsg($self, 'set', 'value', $value);
  5.  
  6.     return $self;
  7. }

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
SEE ALSO

Object Layer, Object

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