1
28

Object Layer

The Object Layer implements the containers for So-o classes and instances and all the functions needed by the Object class.

All the code for the So-o Object Layer is in the file OL.php.

  1. namespace OL;
  2.  
  3. use \InvalidArgumentException as InvalidArgumentException;
  4. use \LogicException as LogicException;

The code for the Object Layer is placed in its own namespace. Note that only the So-o interface functions and the methods of the Object class access this code.

  1. class Definition {

The container for a class is a PHP class called Definition.

  1.     public $name;
  2.     public $revision;
  3.     public $superclass;
  4.  
  5.     public $c_properties;
  6.     public $i_properties;
  7.     public $c_messages;
  8.     public $i_messages;
  9.     public $attributes;

$name holds the name of the class. $revision is a number which the programmer can use to differentiate successive versions of the class. $superclass points to the superclass of the class. It's either the global reference of another class such as $Object or null for the Object class. $c_properties and $c_messages list the properties and the messages of the class. $i_properties and $i_messages list the properties and the messages of an instance of the class. $attributes holds the values of the properties of a class.

  1.     function __construct($cname, $sc, $rev, $c_props, $i_props, $c_msgs, $i_msgs) {

Constructing a Definition takes 7 arguments. $cname specifies the name of the class, a valid variable name. $sc is the global reference of the superclass or null. $rev is the revision number of the class, an integer > 0. $c_props gives the names of the class properties, an array of strings or null. $i_props gives the names of the instance properties, an array of strings or null. $c_msgs gives the names of the class messages, an array of strings or null. $i_msgs gives the names of the instance messages, an array of strings or null.

  1.         static $varname='/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';

$varname holds a regular expression which matches a valid PHP variable name.

  1.         if (!(is_string($cname) and preg_match($varname, $cname))) {
  2.             throw new InvalidArgumentException();
  3.         }

Checks if the class name is a string and a valid variable name. Triggers a InvalidArgumentException exception in case of error.

  1.         if (!(is_null($sc) or (is_object($sc) and __NAMESPACE__ . '\Definition' == get_class($sc)))) {
  2.             throw new InvalidArgumentException();
  3.         }

Checks if the superclass is either null or a class. Triggers a InvalidArgumentException exception in case of error.

  1.         if (!(is_int($rev) and $rev > 0)) {
  2.             throw new InvalidArgumentException();
  3.         }

Checks if the revision number is an integer > 0. Triggers a InvalidArgumentException exception in case of error.

  1.         foreach (array($c_props, $i_props, $c_msgs, $i_msgs) as $arr) {
  2.             if (is_null($arr)) {
  3.                 continue;
  4.             }
  5.             if (is_array($arr)) {
  6.                 foreach ($arr as $s) {
  7.                     if (!(is_string($s) and preg_match($varname, $s))) {
  8.                         throw new InvalidArgumentException();
  9.                     }
  10.                 }
  11.                 continue;
  12.             }
  13.             throw new InvalidArgumentException();
  14.         }

Checks if the class and the instance properties and messages are either null or an array of strings which are valid variable names. Triggers a InvalidArgumentException exception in case of error.

  1.         $this->name=$cname;
  2.         $this->revision=$rev;
  3.         $this->superclass='Object' != $cname ? ($sc ? $sc : $GLOBALS['Object']) : null;
  4.  
  5.         $this->c_properties=$c_props ? array_fill_keys($c_props, 0) : null;
  6.         $this->i_properties=$i_props ? array_fill_keys($i_props, 0) : null;
  7.         $this->c_messages=$c_msgs ? array_fill_keys($c_msgs, 0) : null;
  8.         $this->i_messages=$i_msgs ? array_fill_keys($i_msgs, 0) : null;
  9.  
  10.         $this->attributes=array();
  11.     }

Initializes the new Definition. If $sc is null, the superclass of the class is set to $Object except for the Object class which is the only class without a superclass.

The lists of class and instance properties and messages are kept in associative arrays so the code which checks if a property or a message is implemented by the class is more effecient.

NOTE: Only the defclass function of the interface can construct a Definition.

  1.     function __destruct() {
  2.         return class_send_message($this, 'free');
  3.     }

When a class isn't referenced anymore, it's automatically sent the message free.

NOTE: The free class method defined by the Object class does nothing. In most cases, __destruct can be removed.

  1.     function __toString() {
  2.         return 'class(' . $this->name . ')';
  3.     }

The string representation of a class returns the word class followed by the name of the class between parenthesis.

  1.     function sendself($msg, $args=false) {
  2.         return class_send_message($this, $msg, $args);
  3.     }

sendself sends $msg with $args in argument to the class. $args is either false or an array. sendself simply returns the result of calling class_send_message with the class, the message and the parameters of the message in argument.

NOTE: Only the sendmsg function of the interface calls the sendself method of a Definition.

  1.     function sendsuper($msg, $args=false) {
  2.         $backtrace=debug_backtrace(false);

sendsuper sends $msg with $args in argument to the class, in the context of the class whose class method has called sendsuper. The code finds the class of the method being executed by analyzing the output of the debug_backtrace function of PHP.

  1.         $fromfunc=count($backtrace) > 2 ? $backtrace[2]['function'] : false;
  2.         $pos=$fromfunc ? strpos($fromfunc, '\\c_') : false;
  3.         $fromclassname=$pos ? substr($fromfunc, 0, $pos) : false;
  4.         $fromclass=($fromclassname and isset($GLOBALS[$fromclassname])) ? $GLOBALS[$fromclassname] : false;

Sets $fromfunc to the name of the function which has called supersend. Sets $fromclassname to the name of the class which has defined this function by extracting the namespace of $fromfunc. Sets $fromclass to the global reference of the class whose name is $fromclassname.

  1.         if (!$fromclass) {
  2.             throw new LogicException();
  3.         }

Triggers a LogicException exception if sendsuper wasn't called from a class method.

  1.         return class_super_send_message($fromclass, $this, $msg, $args);
  2.     }
  3. }

Returns the result of calling class_super_send_message with the class of the calling method, the class, the message and the parameters of the message in argument.

NOTE: Only the supersend function of the interface calls the sendsuper method of a Definition.

  1. class Instance {

The container for an object is a PHP class called Instance.

  1.     public $class;
  2.     public $attributes;

$class is the global reference of the instance's class. $attributes holds the values of the properties of an instance.

  1.     function __construct($c) {

Constructing an Instance takes 1 argument. $c is the global reference of the class of the new instance.

  1.         if (!(is_object($c) and __NAMESPACE__ . '\Definition' == get_class($c))) {
  2.             throw new InvalidArgumentException();
  3.         }

Checks if the class is a Definition. Triggers a InvalidArgumentException exception in case of error.

  1.         $this->class=$c;
  2.  
  3.         $this->attributes=array();
  4.     }

Initializes the new Instance.

NOTE: Only the class_make function of the Object Layer constructs an Instance.

  1.     function __destruct() {
  2.         return object_send_message($this, 'free');
  3.     }

When an instance isn't referenced anymore, it's automatically sent the message free.

NOTE: The free instance method defined by the Object class does nothing. In most cases, __destruct can be removed.

  1.     function __toString() {
  2.         return 'object(' . $this->class->name . ')';
  3.     }

The string representation of an instance returns the word object followed by the name of the class of the instance between parenthesis..

  1.     function sendself($msg, $args=false) {
  2.         return object_send_message($this, $msg, $args);
  3.     }

sendself sends $msg with $args in argument to the instance. $args is either false or an array. sendself returns the result of calling object_send_message with the instance, the message and the parameters of the message in argument.

NOTE: Only the sendmsg function of the interface calls the sendself method of an Instance.

  1.     function sendsuper($msg, $args=false) {
  2.         $backtrace=debug_backtrace(false);

sendsuper sends $msg with $args in argument to the instance, in the context of the class whose instance method has called sendsuper. The code finds the class of the method being executed by analyzing the output of the debug_backtrace function of PHP.

  1.         $fromfunc=count($backtrace) > 2 ? $backtrace[2]['function'] : false;
  2.         $pos=$fromfunc ? strpos($fromfunc, '\\i_') : false;
  3.         $fromclassname=$pos ? substr($fromfunc, 0, $pos) : false;
  4.         $fromclass=($fromclassname and isset($GLOBALS[$fromclassname])) ? $GLOBALS[$fromclassname] : false;

Sets $fromfunc to the name of the function which has called supersend. Sets $fromclassname to the name of the class which has defined this function by extracting its namespace. Sets $fromclass to the global reference of the class whose name is $fromclassname.

  1.         if (!$fromclass) {
  2.             throw new LogicException();
  3.         }

Triggers a LogicException exception if sendsuper wasn't called from an instance method.

  1.         return object_super_send_message($fromclass, $this, $msg, $args);
  2.     }
  3. }

Returns the result of calling object_super_send_message with the class of the calling method, the instance, the message and the parameters of the message in argument.

NOTE: Only the supersend function of the interface calls the sendsuper method of an Instance.

The rest of the code implements all the functions of the Object Layer which are needed by the Object class.

class_class_method_symbol
Returns the name of a class method.
class_instance_method_symbol
Returns the name of an instance method.
class_name
Returns the name of a class.
class_revision
Returns the revision number of a class.
class_superclass
Returns the superclass of a class.
class_class_properties
Returns the class properties defined by a class.
class_instance_properties
Returns the instance properties defined by a class.
class_class_messages
Returns the class messages defined by a class.
class_instance_messages
Returns the instance messages defined by a class.
class_set_class_properties
Initializes the class properties of a class.
class_set_instance_properties
Initializes the instance properties of a class.
class_set_class_messages
Initializes the class messages of a class.
class_set_instance_messages
Initializes the instance messages of a class.
class_add_class_message
Adds a class message to a class.
class_remove_class_message
Removes a class message from a class.
class_add_instance_message
Adds an instance message to a class.
class_remove_instance_message
Removes an instance message from a class.
class_add_class_property
Adds a class property to a class.
class_remove_class_property
Removes a class property from a class.
class_add_instance_property
Adds an instance property to a class.
class_remove_instance_property
Removes an instance property from a class.
class_attributes
Returns the values of the properties of a class.
class_set_attributes
Initializes the values of the properties of a class.
class_is_kind_of
Checks if a class is a subclass of another class.
class_get
Returns the value of a property of a class.
class_set
Modifies the value of a property of a class.
class_make
Returns a new instance of a class.
class_check
Checks the integrity of a class.
object_class
Returns the class of an instance.
object_superclass
Returns the superclass of an instance.
object_assume
Changes the class of an instance.
object_attributes
Returns the values of the properties of an instance.
object_set_attributes
Initializes the values of the properties of an instance.
object_get
Returns the value of a property of an instance.
object_set
Initializes the value of a property of an instance.
object_copy
Returns a copy of an instance.
class_find_class_property
Checks if a class property of a class exists.
class_find_instance_property
Checks if an instance property of a class exists.
class_find_class_method_class
Returns the class which implements a class message.
class_find_class_method
Returns the function which implements a class message.
class_find_instance_method_class
Returns the class which implements an instance message.
class_find_instance_method
Returns the function which implements an instance message.
class_apply_method
Runs a class method.
class_send_message
Executes a class message.
class_super_send_message
Executes a class message inherited from a superclass.
object_apply_method
Runs an instance method.
object_send_message
Executes an instance message.
object_super_send_message
Executes an instance message inherited from a superclass.
SEE ALSO

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