mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
MINOR Restructured code in Object.php to consistently have properties and important methods like __call() at the top of definitions (no logic changes). Added minor documentation.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65058 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
d102a902b9
commit
0465365b51
165
core/Object.php
165
core/Object.php
@ -8,6 +8,21 @@
|
||||
*/
|
||||
class Object {
|
||||
|
||||
/**
|
||||
* @var string $class
|
||||
*/
|
||||
public $class;
|
||||
|
||||
/**
|
||||
* @var array $statics
|
||||
*/
|
||||
protected static $statics = array();
|
||||
|
||||
/**
|
||||
* @var array $static_cached
|
||||
*/
|
||||
protected static $static_cached = array();
|
||||
|
||||
/**
|
||||
* This DataObjects extensions, eg Versioned.
|
||||
* @var array
|
||||
@ -27,18 +42,40 @@ class Object {
|
||||
*/
|
||||
public static $extensions = null;
|
||||
|
||||
/**
|
||||
* @var array $extraStatics
|
||||
*/
|
||||
protected static $extraStatics = array();
|
||||
|
||||
/**
|
||||
* @var array $classConstructed
|
||||
*/
|
||||
protected static $classConstructed = array();
|
||||
|
||||
/**
|
||||
* @var array $extraMethods
|
||||
*/
|
||||
protected static $extraMethods = array();
|
||||
|
||||
/**
|
||||
* @var array $builtInMethods
|
||||
*/
|
||||
protected static $builtInMethods = array();
|
||||
|
||||
/**
|
||||
* Use the class in the value instead of the class in the key
|
||||
* @var array $custom_classes Use the class in the value instead of the class in the key
|
||||
*/
|
||||
private static $custom_classes = array();
|
||||
|
||||
/**
|
||||
* @var array $strong_classes
|
||||
*/
|
||||
private static $strong_classes = array();
|
||||
|
||||
/**
|
||||
* @var array $uninherited_statics
|
||||
*/
|
||||
private static $uninherited_statics = array();
|
||||
|
||||
function __construct() {
|
||||
$this->class = get_class($this);
|
||||
@ -58,6 +95,45 @@ class Object {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a method.
|
||||
* Extra methods can be hooked to a class using
|
||||
*/
|
||||
public function __call($methodName, $args) {
|
||||
$lowerMethodName = strtolower($methodName);
|
||||
if(isset(Object::$extraMethods[$this->class][$lowerMethodName])) {
|
||||
$config = Object::$extraMethods[$this->class][$lowerMethodName];
|
||||
if(isset($config['parameterName'])) {
|
||||
if(isset($config['arrayIndex'])) $obj = $this->{$config['parameterName']}[$config['arrayIndex']];
|
||||
else $obj = $this->{$config['parameterName']};
|
||||
|
||||
if($obj) {
|
||||
return call_user_func_array(array(&$obj, $methodName), $args);
|
||||
} else {
|
||||
if($this->destroyed) user_error("Attempted to call $methodName on destroyed '$this->class' object", E_USER_ERROR);
|
||||
else user_error("'$this->class' object doesn't have a parameter $config[parameterName]($config[arrayIndex]) to pass control to. Perhaps this object has been mistakenly destroyed?", E_USER_WARNING);
|
||||
}
|
||||
|
||||
} else if(isset($config['wrap'])) {
|
||||
array_unshift($args, $config['methodName']);
|
||||
return call_user_func_array(array(&$this, $config['wrap']), $args);
|
||||
|
||||
} else if(isset($config['function'])) {
|
||||
$function = $config['function'];
|
||||
return $function($this, $args);
|
||||
|
||||
} else if($config['function_str']) {
|
||||
$function = Object::$extraMethods[$this->class][strtolower($methodName)]['function'] = create_function('$obj, $args', $config['function_str']);
|
||||
return $function($this, $args);
|
||||
|
||||
} else {
|
||||
user_error("Object::__call() Method '$methodName' in class '$this->class' an invalid format: " . var_export(Object::$extraMethods[$this->class][$methodName],true), E_USER_ERROR);
|
||||
}
|
||||
} else {
|
||||
user_error("Object::__call() Method '$methodName' not found in class '$this->class'", E_USER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function allows you to overload class creation methods, so certain classes are
|
||||
* always created correctly over your system.
|
||||
@ -132,16 +208,6 @@ class Object {
|
||||
return new $classToCreate( $arg0, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7, $arg8 );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if this object "exists", i.e., has a sensible value.
|
||||
* Overload this in subclasses.
|
||||
* For example, an empty DataObject record could return false.
|
||||
*/
|
||||
public function exists() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given method exists.
|
||||
*/
|
||||
@ -159,45 +225,6 @@ class Object {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a method.
|
||||
* Extra methods can be hooked to a class using
|
||||
*/
|
||||
public function __call($methodName, $args) {
|
||||
$lowerMethodName = strtolower($methodName);
|
||||
if(isset(Object::$extraMethods[$this->class][$lowerMethodName])) {
|
||||
$config = Object::$extraMethods[$this->class][$lowerMethodName];
|
||||
if(isset($config['parameterName'])) {
|
||||
if(isset($config['arrayIndex'])) $obj = $this->{$config['parameterName']}[$config['arrayIndex']];
|
||||
else $obj = $this->{$config['parameterName']};
|
||||
|
||||
if($obj) {
|
||||
return call_user_func_array(array(&$obj, $methodName), $args);
|
||||
} else {
|
||||
if($this->destroyed) user_error("Attempted to call $methodName on destroyed '$this->class' object", E_USER_ERROR);
|
||||
else user_error("'$this->class' object doesn't have a parameter $config[parameterName]($config[arrayIndex]) to pass control to. Perhaps this object has been mistakenly destroyed?", E_USER_WARNING);
|
||||
}
|
||||
|
||||
} else if(isset($config['wrap'])) {
|
||||
array_unshift($args, $config['methodName']);
|
||||
return call_user_func_array(array(&$this, $config['wrap']), $args);
|
||||
|
||||
} else if(isset($config['function'])) {
|
||||
$function = $config['function'];
|
||||
return $function($this, $args);
|
||||
|
||||
} else if($config['function_str']) {
|
||||
$function = Object::$extraMethods[$this->class][strtolower($methodName)]['function'] = create_function('$obj, $args', $config['function_str']);
|
||||
return $function($this, $args);
|
||||
|
||||
} else {
|
||||
user_error("Object::__call() Method '$methodName' in class '$this->class' an invalid format: " . var_export(Object::$extraMethods[$this->class][$methodName],true), E_USER_ERROR);
|
||||
}
|
||||
} else {
|
||||
user_error("Object::__call() Method '$methodName' not found in class '$this->class'", E_USER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the all methods from a given parameter to this object.
|
||||
* This is used for extensions.
|
||||
@ -301,14 +328,6 @@ class Object {
|
||||
}
|
||||
}
|
||||
|
||||
function parentClass() {
|
||||
return get_parent_class($this);
|
||||
}
|
||||
|
||||
function is_a($class) {
|
||||
return is_a($this, $class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an uninherited static variable
|
||||
*/
|
||||
@ -342,10 +361,6 @@ class Object {
|
||||
return isset(Object::$uninherited_statics[$this->class][$name]) ? Object::$uninherited_statics[$this->class][$name] : null;
|
||||
}
|
||||
|
||||
|
||||
protected static $statics = array();
|
||||
protected static $static_cached = array();
|
||||
|
||||
/**
|
||||
* Get a static variable.
|
||||
*
|
||||
@ -380,8 +395,30 @@ class Object {
|
||||
Object::$static_cached[$this->class][$name] = true;
|
||||
}
|
||||
|
||||
public $class;
|
||||
private static $uninherited_statics = array();
|
||||
/**
|
||||
* Returns true if this object "exists", i.e., has a sensible value.
|
||||
* Overload this in subclasses.
|
||||
* For example, an empty DataObject record could return false.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function exists() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function parentClass() {
|
||||
return get_parent_class($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for PHP's is_a()
|
||||
*
|
||||
* @param string $class
|
||||
* @return boolean
|
||||
*/
|
||||
function is_a($class) {
|
||||
return is_a($this, $class);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return $this->class;
|
||||
|
Loading…
x
Reference in New Issue
Block a user