Object
The Object class implements the base messages of the So-o model. All the other classes inherit from the Object class.
All the code for the So-o Object class is in the file Object.php.
- namespace Object;
- use \InvalidArgumentException as InvalidArgumentException;
Places the code of the Object class in its own namespace.
- require_once 'OL.php';
Loads the code of the Object Layer.
- define(__NAMESPACE__ . '\InvalidClassProperty', '%s::%s Invalid class property');
- define(__NAMESPACE__ . '\InvalidClassMessage', '%s::%s Invalid class message');
- define(__NAMESPACE__ . '\InvalidInstanceProperty', '%s::%s Invalid instance property');
- define(__NAMESPACE__ . '\InvalidInstanceMessage', '%s::%s Invalid instance message');
- define(__NAMESPACE__ . '\NotImplemented', '%s::%s Not implemented');
- define(__NAMESPACE__ . '\SubclassResponsibility', '%s::%s Subclass responsibility');
Defines the texts of the error messages returned by the Object class.
- defclass('Object',
- // superclass
- null,
- // revision
- 1,
- // class properties
- null,
- // instance properties
- null,
- // class messages
- array('get', 'set', 'make', 'new', 'initialize', 'free', 'class', 'name', 'superclass', 'classMessages', 'instanceMessages', 'classProperties', 'instanceProperties', 'classMethodFor', 'instanceMethodFor', 'perform', 'read', 'write', 'error', 'doesNotContain', 'doesNotRecognize', 'notImplemented', 'subclassResponsibility', 'revision', 'addInstanceMessage', 'removeInstanceMessage', 'addClassMessage', 'removeClassMessage', 'addInstanceProperty', 'removeInstanceProperty', 'addClassProperty', 'removeClassProperty', 'check'),
- // instance messages
- array('get', 'set', 'init', 'free', 'class', 'superclass', 'perform', 'delegate', 'setDelegate', 'respondsTo', 'methodFor', 'isKindOf', 'copy', 'read', 'write', 'assume', 'toString', 'print', 'error', 'doesNotContain', 'doesNotRecognize', 'notImplemented', 'subclassResponsibility', 'messages', 'properties')
- );
The Object class is the only class which doesn't inherit from a superclass. It doesn't have any class or instance properties. It implements all the base messages of the So-o model.
CLASS METHODS
free
SYNOPSIS
free($self)
DESCRIPTION
free is automatically sent by the Object Layer to a class which is not referenced anymore.
The free method defined by the Object class does nothing.
IMPORTANT: If you decide to remove the __destruct method from the Definition class of the Object Layer, the container for a class, free will not be sent automatically.
CODE
- function c_free($self) {
- }
free does nothing.
initialize
SYNOPSIS
initialize($self)
DESCRIPTION
initialize is automatically sent by defclass to a new class.
The initialize method defined by the Object class does nothing.
initialize is usually redefined by a class which must initialize its class properties.
CODE
- function c_initialize ($self) {
- return $self;
- }
initialize does nothing and returns $self.
class
name
revision
superclass
SYNOPSIS
class($self)
name($self)
revision($self)
superclass($self)
DESCRIPTION
class returns the class of the class $self, i.e. $self.
name returns the name of the class $self.
revision returns the revision number of the class $self.
superclass returns the superclass of the class $self, null if $self is $Object, the Object class.
CODE
- function c_class($self) {
- return $self;
- }
- function c_name($self) {
- return \OL\class_name($self);
- }
- function c_revision($self) {
- return \OL\class_revision($self);
- }
- function c_superclass($self) {
- return \OL\class_superclass($self);
- }
class returns $self.
name, revision and superclass return the result of the functions class_name, class_revision and class_superclass of the Object Layer.
get
set
SYNOPSIS
get($self, $attribute)
set($self, $attribute, $value)
DESCRIPTION
get returns the value of the property $attribute of $self.
set sets the value of the property $attribute of $self to $value.
IMPORTANT: get returns a value, not a reference to a value. To modify the content of an attribute whose value is an array, obtain its value with get, modify it then change the value of the property with set.
CODE
- function c_get($self, $attribute) {
- return \OL\class_get($self, $attribute);
- }
- function c_set($self, $attribute, $value) {
- return \OL\class_set($self, $attribute, $value);
- }
get and set return the result of the functions class_get and class_set of the Object Layer.
make
new
SYNOPSIS
make($self)
new($self[, $arg ...])
DESCRIPTION
make returns a new instance of $self.
new creates an instance of $self, sends it the message init with all the parameters of the message following $self and returns the new initialized instance.
IMPORTANT: Always create an instance with new.
CODE
- function c_make($self) {
- return \OL\class_make($self);
- }
- function c_new($self) {
- return \OL\object_send_message(\OL\class_send_message($self, 'make'), 'init', array_slice(func_get_args(), 1));
- }
make returns the result of the function class_make of the Object Layer.
new creates an instance by sending make to $self, sends it init with the parameters of the message following self and returns the new instance.
perform
SYNOPSIS
perform($self, $message, $args=false)
DESCRIPTION
perform returns the result of sending the message $message to $self with $args in argument.
$args is an array.
By default, the message has no argument.
CODE
- function c_perform($self, $message, $args=false) {
- return \OL\class_send_message($self, $message, $args);
- }
perform returns the result of sending $message and $args to $self.
read
write
SYNOPSIS
read($self, $data)
write($self)
DESCRIPTION
write returns the serialization of the attributes of $self.
read initializes the attributes of $self with $data.
$data is a serialized array of values.
CODE
- function c_read($self, $data) {
- $properties=\OL\class_send_message($self, 'classProperties', array(true));
- if (!$properties) {
- return $self;
- }
- $data=unserialize($data);
- if (!is_array($data)) {
- throw new InvalidArgumentException();
- }
- $attributes=array();
- foreach ($properties as $prop) {
- if (isset($data[$prop])) {
- $attributes[$prop]=$data[$prop];
- }
- }
- return \OL\class_set_attributes($self, $attributes);
- }
- function c_write($self) {
- return serialize(\OL\class_attributes($self));
- }
write calls the function serialize with in argument the array of attributes of $self and returns the result.
read obtains the list of properties of $self, calls the function unserialize with $data in argument, checks if the result is an array, then initializes every attribute of $self with the corresponding value extracted from this array.
classMessages
instanceMessages
classMethodFor
instanceMethodFor
addClassMessage
removeClassMessage
addInstanceMessage
removeInstanceMessage
SYNOPSIS
classMessages($self, $inherit=true)
instanceMessages($self, $inherit=true)
classMethodFor($self, $message)
instanceMethodFor($self, $message)
addClassMessage($self, $message)
removeClassMessage($self, $message)
addInstanceMessage($self, $message)
removeInstanceMessage($self, $message)
DESCRIPTION
classMessages returns the class messages of $self in an array.
If $inherit is true, classMessages also returns the class messages inherited from all the superclasses of $self.
instanceMessages returns the instance messages of $self in an array.
If $inherit is true, instanceMessages also returns the instance messages inherited from all the superclasses of $self.
classMethodFor returns the function which implements the class message $message of $self.
instanceMethodFor returns the function which implements the instance message $message of $self.
addClassMessage adds the class message $message to $self.
removeClassMessage removes the class message $message from $self.
addInstanceMessage adds the instance message $message to $self.
removeInstanceMessage removes the instance message $message from $self.
CODE
- function c_classMessages($self, $inherit=true) {
- $messages=\OL\class_class_messages($self);
- if ($messages) {
- $messages=array_keys($messages);
- }
- if ($inherit) {
- $superclass=\OL\class_superclass($self);
- if ($superclass) {
- $inherited_messages=\OL\class_send_message($superclass, 'classMessages', array(true));
- if ($inherited_messages) {
- $messages=$messages ? array_unique(array_merge($messages, $inherited_messages)) : $inherited_messages;
- }
- }
- }
- return $messages;
- }
- function c_instanceMessages($self, $inherit=true) {
- $messages=\OL\class_instance_messages($self);
- if ($messages) {
- $messages=array_keys($messages);
- }
- if ($inherit) {
- $superclass=\OL\class_superclass($self);
- if ($superclass) {
- $inherited_messages=\OL\class_send_message($superclass, 'instanceMessages', array(true));
- if ($inherited_messages) {
- $messages=$messages ? array_unique(array_merge($messages, $inherited_messages)) : $inherited_messages;
- }
- }
- }
- return $messages;
- }
- function c_classMethodFor($self, $message) {
- return \OL\class_find_class_method($self, $message);
- }
- function c_instanceMethodFor($self, $message) {
- return \OL\class_find_instance_method($self, $message);
- }
- function c_addClassMessage($self, $message) {
- return \OL\class_add_class_message($self, $message);
- }
- function c_removeClassMessage($self, $message) {
- return \OL\class_remove_class_message($self, $message);
- }
classMessages sets $messages to the list of the class messages of $self, adds the class messages of the superclasses of $self, deleting duplicates, if $inherit is true then returns $messages.
instanceMessages sets $messages to the list of the instance messages of $self, adds the instance messages of the superclasses of $self, deleting duplicates, if $inherit is true then returns $messages.
classMethodFor, instanceMethodFor, addClassMessage, removeClassMessage, addInstanceMessage and removeInstanceMessage return the result of the functions class_find_class_method, class_find_instance_method, class_add_class_message, class_remove_class_message, class_add_instance_message and class_remove_instance_message of the Object Layer.
classProperties
instanceProperties
addClassProperty
removeClassProperty
addInstanceProperty
removeInstanceProperty
SYNOPSIS
classProperties($self, $inherit=true)
instanceProperties($self, $inherit=true)
classMethodFor($self, $property)
instanceMethodFor($self, $property)
addClassProperty($self, $property)
removeClassProperty($self, $property)
addInstanceProperty($self, $property)
removeInstanceProperty($self, $property)
DESCRIPTION
classProperties returns the class properties of $self in an array.
If $inherit is true, classProperties also returns the class properties inherited from all the superclasses of $self.
instanceProperties returns the instance properties of $self in an array.
If $inherit is true, instanceProperties also returns the instance properties inherited from all the superclasses of $self.
addClassProperty adds the class property $property to $self.
removeClassProperty removes the class property $property from $self.
addInstanceProperty adds the instance property $property to $self.
removeInstanceProperty removes the instance property $property from $self.
CODE
- function c_addInstanceMessage($self, $message) {
- return \OL\class_add_instance_message($self, $message);
- }
- function c_removeInstanceMessage($self, $message) {
- return \OL\class_remove_instance_message($self, $message);
- }
- function c_classProperties($self, $inherit=true) {
- $properties=\OL\class_class_properties($self);
- if ($properties) {
- $properties=array_keys($properties);
- }
- if ($inherit) {
- $superclass=\OL\class_superclass($self);
- if ($superclass) {
- $inherited_properties=\OL\class_send_message($superclass, 'classProperties', array(true));
- if ($inherited_properties) {
- $properties=$properties ? array_unique(array_merge($properties, $inherited_properties)) : $inherited_properties;
- }
- }
- }
- return $properties;
- }
- function c_instanceProperties($self, $inherit=true) {
- $properties=\OL\class_instance_properties($self);
- if ($properties) {
- $properties=array_keys($properties);
- }
- if ($inherit) {
- $superclass=\OL\class_superclass($self);
- if ($superclass) {
- $inherited_properties=\OL\class_send_message($superclass, 'instanceProperties', array(true));
- if ($inherited_properties) {
- $properties=$properties ? array_unique(array_merge($properties, $inherited_properties)) : $inherited_properties;
- }
- }
- }
- return $properties;
- }
classProperties sets $properties to the list of the class properties of $self, adds the class properties of the superclasses of $self, deleting duplicates, if $inherit is true then returns $properties.
instanceProperties sets $properties to the list of the instance properties of $self, adds the instance properties of the superclasses of $self, deleting duplicates, if $inherit is true then returns $properties.
addClassProperty, removeClassProperty, addInstanceProperty and removeInstanceProperty return the result of the functions class_add_class_property, class_remove_class_property, class_add_instance_property and class_remove_instance_property of the Object Layer.
error
doesNotContain
doesNotRecognize
notImplemented
subclassResponsibility
SYNOPSIS
error($self, $err[, $arg ...])
doesNotContain($self, $property)
doesNotRecognize($self, $message)
notImplemented($self, $message)
subclassResponsibility($self, $message)
DESCRIPTION
error triggers a PHP error level E_USER_ERROR whose text is formatted with the function sprintf with in argument $err and the parameters of the message following $err.
doesNotContain sends the message error to $self with in argument the constant InvalidClassProperty, the name of the class $self and $property.
doesNotRecognize sends the message error to $self with in argument the constant InvalidClassMessage, the name of the class $self and $message.
notImplemented sends the message error to $self with in argument the constant NotImplemented, the name of the class $self and $message.
subclassResponsibility sends the message error to $self with in argument the constant SubclassResponsibility, the name of the class $self and $message.
doesNotContain and doesNotRecognize are sent by the Object Layer. notImplemented is generally sent by a method which isn't coded yet while subclassResponsibility is sent by a method which must be implemented by a subclass of an abstract class.
CODE
- function c_addClassProperty($self, $property) {
- return \OL\class_add_class_property($self, $property);
- }
- function c_removeClassProperty($self, $property) {
- return \OL\class_remove_class_property($self, $property);
- }
- function c_addInstanceProperty($self, $property) {
- return \OL\class_add_instance_property($self, $property);
- }
- function c_removeInstanceProperty($self, $property) {
- return \OL\class_remove_instance_property($self, $property);
- }
- function c_error ($self, $err) {
- $errmsg=call_user_func_array('sprintf', array_merge(array($err), array_slice(func_get_args(), 2)));
- return trigger_error($errmsg, E_USER_ERROR);
- }
error formats the error message $errmsg by passing $err and the remaining arguments of error following $err to sprintf then calls trigger_error with in argument $errmsg and the error level E_USER_ERROR.
doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility send the message error to $self with in argument the constant of the corresponding error message and its parameters.
check
SYNOPSIS
check($self)
DESCRIPTION
check checks the integrity of the class $self.
check returns an array of 4 arrays.
The first lists the class messages which don't have a corresponding function in the namespace of the class $self.
The second lists the class messages which are not mentionned in the definition of the class $self although the corresponding function has been found in its namespace.
The third lists the instance messages which don't have a corresponding function in the namespace of the class $self.
The fourth lists the instance messages which are not mentionned in the definition of the class $self although the corresponding function has been found in its namespace.
CODE
- function c_doesNotContain($self, $property) {
- return \OL\class_send_message($self, 'error', array(InvalidClassProperty, \OL\class_name($self), $property));
- }
check returns the result of the function class_check of the Object Layer.
INSTANCE METHODS
class
superclass
messages
properties
SYNOPSIS
class($self)
superclass($self)
messages($self, $inherit=true)
properties($self, $inherit=true)
DESCRIPTION
class returns the class of $self.
superclass returns the superclass of the class of $self, null if $self is an instance of the Object class.
messages returns the messages recognized by $self in an array.
If $inherit is true, messages also returns the messages inherited from all the superclasses of $self.
$inherit is true by default.
instanceProperties returns the properties of $self in an array.
If $inherit is true, properties also returns the properties inherited from all the superclasses of $self.
$inherit is true by default.
CODE
- function i_class($self) {
- return \OL\object_class($self);
- }
- function i_superclass($self) {
- return \OL\class_superclass(\OL\object_class($self));
- }
- function i_messages($self, $inherit=true) {
- return \OL\class_send_message(\OL\object_class($self), 'instanceMessages', array($inherit));
- }
- function i_properties($self, $inherit=true) {
- return \OL\class_send_message(\OL\object_class($self), 'instanceProperties', array($inherit));
- }
class returns the result of the function object_class of the Object Layer.
superclass returns the result of the function class_superclass with the class of $self in argument.
messages sends the message instanceMessages to the class of $self with $inherit in argument.
properties sends the message instanceProperties to the class of $self with $inherit in argument.
isKindOf
methodFor
respondsTo
assume
SYNOPSIS
isKindOf($self, $class)
methodFor($self, $message)
respondsTo($self, $message)
assume($self, $class)
DESCRIPTION
isKindOf returns true if $self is an instance of the class or a subclass of $class or false if not.
methodFor returns the function which implements the message $message for $self or false if $self doesn't respond to $message.
respondsTo returns true if $self has a method for the message $message, false if not.
assume changes the class of $self to $class.
The values of the properties of the former class of $self which are not part of the class $class are preserved but they are not accessible.
CODE
- function i_isKindOf($self, $class) {
- return \OL\class_is_kind_of(\OL\object_class($self), $class);
- }
- function i_methodFor($self, $message) {
- return \OL\class_find_instance_method(\OL\object_class($self), $message);
- }
- function i_respondsTo($self, $message) {
- return \OL\class_find_instance_method(\OL\object_class($self), $message) ? true : false;
- }
- function i_assume($self, $class) {
- return \OL\object_assume($self, $class);
- }
isKindOf returns the result of the function class_is_kind_of of the Object Layer with in argument the class of $self and $class.
methodFor returns the result of the function class_find_instance_method with in argument the class of $self and $message.
respondsTo returns true if the result of the function class_find_instance_method with in argument the class of $self and $message isn't false, true otherwise.
assume calls object_assume with $class in argument.
free
SYNOPSIS
free($self)
DESCRIPTION
free is automatically sent by the Object Layer to an instance which is not referenced anymore.
The free method defined by the Object class does nothing.
IMPORTANT: If you decide to remove the __destruct method from the Instance class of the Object Layer, the container for an instance, free will not be sent automatically.
CODE
- function i_free($self) {
- }
free does nothing.
init
SYNOPSIS
init($self[, $arg ...])
DESCRIPTION
init is automatically sent by new to a new instance.
The init method defined by the Object class does nothing.
init is usually redefined by a class which must initialize its instance properties.
CODE
- function i_init($self) {
- return $self;
- }
init does nothing and returns $self.
copy
SYNOPSIS
copy($self)
DESCRIPTION
copy returns a copy of $self.
CODE
- function i_copy ($self) {
- return \OL\object_copy($self);
- }
copy returns the result of the function object_copy of the Object Layer.
toString
SYNOPSIS
toString($self)
print($self, $eol=false)
DESCRIPTION
toString returns a readable representation of $self.
The toString method of the Object class returns an empty string.
print writes the readable representation of $self to the standard output buffer.
If $eol is true, print adds a newline.
$eol is false by default.
print returns $self.
CODE
- function i_toString($self) {
- return '';
- }
- function i_print($self, $eol=false) {
- echo \OL\object_send_message($self, 'toString');
- if ($eol) {
- echo PHP_EOL;
- }
- return $self;
- }
toString returns an empty string.
print writes the result of sending the message toString to $self to the standard output buffer, adds the value of the constant PHP_EOL if $eol is true and returns $self.
get
set
SYNOPSIS
get($self, $attribute)
set($self, $attribute, $value)
DESCRIPTION
get returns the value of the property $attribute of $self.
set sets the value of the property $attribute of $self to $value.
If $attribute isn't a property of $self, the message doesNotContain is sent to $self with $attribute in argument.
IMPORTANT: get returns a value, not a reference to a value. To modify the content of an attribute whose value is an array, obtain its value with get, modify it then change the value of the property with set.
CODE
- function i_get($self, $property) {
- return \OL\object_get($self, $property);
- }
- function i_set($self, $property, $value) {
- return \OL\object_set($self, $property, $value);
- }
get returns the result of the function object_get of the Object Layer with in argument $attribute.
set returns the result of the function object_set of the Object Layer with in argument $attribute and $value.
perform
SYNOPSIS
perform($self, $message, $args=false)
DESCRIPTION
perform returns the result of sending the message $message to $self with $args in argument.
$args is an array.
By default, the message has no argument.
CODE
- function i_perform($self, $message, $args=false) {
- return \OL\object_send_message($self, $message, $args);
- }
perform returns the result of sending $message and $args to $self.
read
write
SYNOPSIS
read($self, $data)
write($self)
DESCRIPTION
write returns the serialization of the attributs of $self.
read initializes the attributs of $self with $data.
$data is a serialized array of values.
CODE
- function i_read($self, $data) {
- $properties=\OL\class_send_message(\OL\object_class($self), 'instanceProperties', array(true));
- if (!$properties) {
- return $self;
- }
- $data=unserialize($data);
- if (!is_array($data)) {
- throw new InvalidArgumentException();
- }
- $attributes=array();
- foreach ($properties as $prop) {
- if (isset($data[$prop])) {
- $attributes[$prop]=$data[$prop];
- }
- }
- return \OL\object_set_attributes($self, $attributes);
- }
- function i_write($self) {
- return serialize(\OL\object_attributes($self));
- }
write calls the function serialize with in argument the array of attributs of $self and returns the result.
read obtains the list of properties of $self, calls the function unserialize with $data in argument, checks if the result is an array, then initializes every attributs of $self with the corresponding value extracted from this array.
delegate
setDelegate
SYNOPSIS
delegate($self, $msg=false[, $arg ...])
setDelegate($self, $delegate)
DESCRIPTION
delegate returns the result of sending the message $msg and the arguments $arg following $msg to the delegate of $self.
If $self doesn't have a delegate or if the delegate of $self doesn't respond to the message $msg, delegate returns false.
If $msg is false, delegate returns the delegate of $self.
setDelegate initializes the delegate property of $self with $delegate.
NOTE: Sending delegate or setDelegate to an instance which does have the property delegate sends the message doesNotContain to the instance.
CODE
- function i_delegate($self, $msg=false) {
- $delegate=\OL\object_get($self, 'delegate');
- if (!$msg) {
- return $delegate;
- }
- if (!$delegate) {
- return false;
- }
- if (!\OL\object_send_message($delegate, 'respondsTo', array($msg))) {
- return false;
- }
- return \OL\object_send_message($delegate, $msg, array_slice(func_get_args(), 2));
- }
- function i_setDelegate($self, $delegate) {
- if (!(is_null($delegate) or is_object($delegate))) {
- throw new InvalidArgumentException();
- }
- return \OL\object_set($self, 'delegate', $delegate);
- }
delegate sets $delegate to the valeur of the property delegate of $self, returns $delegate if $msg is false, returns false if $delegate is null or if $delegate doesn't respond to $msg, returns the result of sending $msg with the arguments of delegate following $msg to $delegate.
setDelegate checks if $delegate is null or an object then sets the property delegate of $self to $delegate.
error
doesNotContain
doesNotRecognize
notImplemented
subclassResponsibility
SYNOPSIS
error($self, $err[, $arg ...])
doesNotContain($self, $property)
doesNotRecognize($self, $message)
notImplemented($self, $message)
subclassResponsibility($self, $message)
DESCRIPTION
error triggers a PHP error level E_USER_ERROR whose text is formatted with the function sprintf with in argument $err and the parameters of the message following $err.
doesNotContain sends the message error to $self with in argument the constant InvalidClassProperty, the name of the class of $self and $property.
doesNotRecognize sends the message error to $self with in argument the constant InvalidClassMessage, the name of the class of $self and $message.
notImplemented sends the message error to $self with in argument the constant NotImplemented, the name of the class of $self and $message.
subclassResponsibility sends the message error to $self with in argument the constant SubclassResponsibility, the name of the class of $self and $message.
doesNotContain and doesNotRecognize are sent by the Object Layer. notImplemented is generally sent by a method which isn't coded yet while subclassResponsibility is sent by a method which must be implemented by a subclass of an abstract class.
CODE
- function i_error ($self, $err) {
- $errmsg=call_user_func_array('sprintf', array_merge(array($err), array_slice(func_get_args(), 2)));
- return trigger_error($errmsg, E_USER_ERROR);
- }
- function i_doesNotContain($self, $property) {
- return \OL\object_send_message($self, 'error', array(InvalidInstanceProperty, \OL\class_name(\OL\object_class($self)), $property));
- }
- function i_doesNotRecognize($self, $message) {
- return \OL\object_send_message($self, 'error', array(InvalidInstanceMessage, \OL\class_name(\OL\object_class($self)), $message));
- }
- function i_notImplemented($self, $message) {
- return \OL\object_send_message($self, 'error', array(NotImplemented, \OL\class_name(\OL\object_class($self)), $message));
- }
- function i_subclassResponsibility($self, $message) {
- return \OL\object_send_message($self, 'error', array(SubclassResponsibility, \OL\class_name(\OL\object_class($self)), $message));
- }
error formats the error message $errmsg by passing $err and the remaining arguments of error following $err to sprintf then calls trigger_error with in argument $errmsg and the error level E_USER_ERROR.
doesNotContain, doesNotRecognize, notImplemented and subclassResponsibility send the message error to $self with in argument the constant of the corresponding error message and its parameters.
Comments