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.
- namespace OL;
- use \InvalidArgumentException as InvalidArgumentException;
- 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.
- class Definition {
The container for a class is a PHP class called Definition.
- public $name;
- public $revision;
- public $superclass;
- public $c_properties;
- public $i_properties;
- public $c_messages;
- public $i_messages;
- 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.
- 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
.
- 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.
- if (!(is_string($cname) and preg_match($varname, $cname))) {
- throw new InvalidArgumentException();
- }
Checks if the class name is a string and a valid variable name. Triggers a InvalidArgumentException exception in case of error.
- if (!(is_null($sc) or (is_object($sc) and __NAMESPACE__ . '\Definition' == get_class($sc)))) {
- throw new InvalidArgumentException();
- }
Checks if the superclass is either null
or a class.
Triggers a InvalidArgumentException exception in case of error.
- if (!(is_int($rev) and $rev > 0)) {
- throw new InvalidArgumentException();
- }
Checks if the revision number is an integer > 0. Triggers a InvalidArgumentException exception in case of error.
- foreach (array($c_props, $i_props, $c_msgs, $i_msgs) as $arr) {
- if (is_null($arr)) {
- continue;
- }
- if (is_array($arr)) {
- foreach ($arr as $s) {
- if (!(is_string($s) and preg_match($varname, $s))) {
- throw new InvalidArgumentException();
- }
- }
- continue;
- }
- throw new InvalidArgumentException();
- }
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.
- $this->name=$cname;
- $this->revision=$rev;
- $this->superclass='Object' != $cname ? ($sc ? $sc : $GLOBALS['Object']) : null;
- $this->c_properties=$c_props ? array_fill_keys($c_props, 0) : null;
- $this->i_properties=$i_props ? array_fill_keys($i_props, 0) : null;
- $this->c_messages=$c_msgs ? array_fill_keys($c_msgs, 0) : null;
- $this->i_messages=$i_msgs ? array_fill_keys($i_msgs, 0) : null;
- $this->attributes=array();
- }
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.
- function __destruct() {
- return class_send_message($this, 'free');
- }
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.
- function __toString() {
- return 'class(' . $this->name . ')';
- }
The string representation of a class returns the word class
followed by the name of the class between parenthesis.
- function sendself($msg, $args=false) {
- return class_send_message($this, $msg, $args);
- }
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.
- function sendsuper($msg, $args=false) {
- $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.
- $fromfunc=count($backtrace) > 2 ? $backtrace[2]['function'] : false;
- $pos=$fromfunc ? strpos($fromfunc, '\\c_') : false;
- $fromclassname=$pos ? substr($fromfunc, 0, $pos) : false;
- $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
.
- if (!$fromclass) {
- throw new LogicException();
- }
Triggers a LogicException exception if sendsuper
wasn't called from a class method.
- return class_super_send_message($fromclass, $this, $msg, $args);
- }
- }
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.
- class Instance {
The container for an object is a PHP class called Instance.
- public $class;
- public $attributes;
$class
is the global reference of the instance's class.
$attributes
holds the values of the properties of an instance.
- function __construct($c) {
Constructing an Instance takes 1 argument.
$c
is the global reference of the class of the new instance.
- if (!(is_object($c) and __NAMESPACE__ . '\Definition' == get_class($c))) {
- throw new InvalidArgumentException();
- }
Checks if the class is a Definition. Triggers a InvalidArgumentException exception in case of error.
- $this->class=$c;
- $this->attributes=array();
- }
Initializes the new Instance.
NOTE: Only the class_make
function of the Object Layer constructs an Instance.
- function __destruct() {
- return object_send_message($this, 'free');
- }
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.
- function __toString() {
- return 'object(' . $this->class->name . ')';
- }
The string representation of an instance returns the word object
followed by the name of the class of the instance between parenthesis..
- function sendself($msg, $args=false) {
- return object_send_message($this, $msg, $args);
- }
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.
- function sendsuper($msg, $args=false) {
- $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.
- $fromfunc=count($backtrace) > 2 ? $backtrace[2]['function'] : false;
- $pos=$fromfunc ? strpos($fromfunc, '\\i_') : false;
- $fromclassname=$pos ? substr($fromfunc, 0, $pos) : false;
- $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
.
- if (!$fromclass) {
- throw new LogicException();
- }
Triggers a LogicException exception if sendsuper
wasn't called from an instance method.
- return object_super_send_message($fromclass, $this, $msg, $args);
- }
- }
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.
Comments