You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
6.9 KiB
220 lines
6.9 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\base;
|
||
|
|
||
|
/**
|
||
12 years ago
|
* @include @yii/base/Object.md
|
||
13 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Object
|
||
|
{
|
||
|
/**
|
||
12 years ago
|
* @return string the fully qualified name of this class.
|
||
|
*/
|
||
|
public static function className()
|
||
|
{
|
||
|
return get_called_class();
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Constructor.
|
||
13 years ago
|
* The default implementation does two things:
|
||
|
*
|
||
|
* - Initializes the object with the given configuration `$config`.
|
||
|
* - Call [[init()]].
|
||
|
*
|
||
|
* If this method is overridden in a child class, it is recommended that
|
||
|
*
|
||
|
* - the last parameter of the constructor is a configuration array, like `$config` here.
|
||
|
* - call the parent implementation at the end of the constructor.
|
||
|
*
|
||
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||
|
*/
|
||
|
public function __construct($config = array())
|
||
|
{
|
||
|
foreach ($config as $name => $value) {
|
||
|
$this->$name = $value;
|
||
|
}
|
||
|
$this->init();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Initializes the object.
|
||
|
* This method is invoked at the end of the constructor after the object is initialized with the
|
||
|
* given configuration.
|
||
13 years ago
|
*/
|
||
13 years ago
|
public function init()
|
||
13 years ago
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns the value of an object property.
|
||
13 years ago
|
*
|
||
|
* Do not call this method directly as it is a PHP magic method that
|
||
|
* will be implicitly called when executing `$value = $object->property;`.
|
||
|
* @param string $name the property name
|
||
|
* @return mixed the property value, event handlers attached to the event,
|
||
|
* the named behavior, or the value of a behavior's property
|
||
12 years ago
|
* @throws UnknownPropertyException if the property is not defined
|
||
13 years ago
|
* @see __set
|
||
|
*/
|
||
|
public function __get($name)
|
||
|
{
|
||
|
$getter = 'get' . $name;
|
||
|
if (method_exists($this, $getter)) {
|
||
|
return $this->$getter();
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Sets value of an object property.
|
||
13 years ago
|
*
|
||
|
* Do not call this method directly as it is a PHP magic method that
|
||
|
* will be implicitly called when executing `$object->property = $value;`.
|
||
|
* @param string $name the property name or the event name
|
||
|
* @param mixed $value the property value
|
||
12 years ago
|
* @throws UnknownPropertyException if the property is not defined
|
||
|
* @throws InvalidCallException if the property is read-only.
|
||
13 years ago
|
* @see __get
|
||
|
*/
|
||
|
public function __set($name, $value)
|
||
|
{
|
||
|
$setter = 'set' . $name;
|
||
|
if (method_exists($this, $setter)) {
|
||
13 years ago
|
$this->$setter($value);
|
||
13 years ago
|
} elseif (method_exists($this, 'get' . $name)) {
|
||
12 years ago
|
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks if the named property is set (not null).
|
||
|
*
|
||
|
* Do not call this method directly as it is a PHP magic method that
|
||
|
* will be implicitly called when executing `isset($object->property)`.
|
||
|
*
|
||
|
* Note that if the property is not defined, false will be returned.
|
||
|
* @param string $name the property name or the event name
|
||
|
* @return boolean whether the named property is set (not null).
|
||
|
*/
|
||
|
public function __isset($name)
|
||
|
{
|
||
|
$getter = 'get' . $name;
|
||
13 years ago
|
if (method_exists($this, $getter)) {
|
||
13 years ago
|
return $this->$getter() !== null;
|
||
13 years ago
|
} else {
|
||
13 years ago
|
return false;
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Sets an object property to null.
|
||
13 years ago
|
*
|
||
|
* Do not call this method directly as it is a PHP magic method that
|
||
|
* will be implicitly called when executing `unset($object->property)`.
|
||
|
*
|
||
|
* Note that if the property is not defined, this method will do nothing.
|
||
|
* If the property is read-only, it will throw an exception.
|
||
|
* @param string $name the property name
|
||
12 years ago
|
* @throws InvalidCallException if the property is read only.
|
||
13 years ago
|
*/
|
||
|
public function __unset($name)
|
||
|
{
|
||
|
$setter = 'set' . $name;
|
||
13 years ago
|
if (method_exists($this, $setter)) {
|
||
13 years ago
|
$this->$setter(null);
|
||
13 years ago
|
} elseif (method_exists($this, 'get' . $name)) {
|
||
12 years ago
|
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Calls the named method which is not a class method.
|
||
|
* If the name refers to a component property whose value is
|
||
|
* an anonymous function, the method will execute the function.
|
||
|
*
|
||
|
* Do not call this method directly as it is a PHP magic method that
|
||
|
* will be implicitly called when an unknown method is being invoked.
|
||
|
* @param string $name the method name
|
||
|
* @param array $params method parameters
|
||
12 years ago
|
* @throws UnknownMethodException when calling unknown method
|
||
13 years ago
|
* @return mixed the method return value
|
||
|
*/
|
||
|
public function __call($name, $params)
|
||
|
{
|
||
12 years ago
|
$getter = 'get' . $name;
|
||
|
if (method_exists($this, $getter)) {
|
||
|
$func = $this->$getter();
|
||
13 years ago
|
if ($func instanceof \Closure) {
|
||
|
return call_user_func_array($func, $params);
|
||
|
}
|
||
|
}
|
||
12 years ago
|
throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Returns a value indicating whether a property is defined.
|
||
12 years ago
|
* A property is defined if:
|
||
|
*
|
||
|
* - the class has a getter or setter method associated with the specified name
|
||
|
* (in this case, property name is case-insensitive);
|
||
|
* - the class has a member variable with the specified name (when `$checkVar` is true);
|
||
|
*
|
||
13 years ago
|
* @param string $name the property name
|
||
13 years ago
|
* @param boolean $checkVar whether to treat member variables as properties
|
||
13 years ago
|
* @return boolean whether the property is defined
|
||
|
* @see canGetProperty
|
||
|
* @see canSetProperty
|
||
|
*/
|
||
13 years ago
|
public function hasProperty($name, $checkVar = true)
|
||
13 years ago
|
{
|
||
13 years ago
|
return $this->canGetProperty($name, $checkVar) || $this->canSetProperty($name, false);
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a value indicating whether a property can be read.
|
||
12 years ago
|
* A property is readable if:
|
||
|
*
|
||
|
* - the class has a getter method associated with the specified name
|
||
|
* (in this case, property name is case-insensitive);
|
||
|
* - the class has a member variable with the specified name (when `$checkVar` is true);
|
||
|
*
|
||
13 years ago
|
* @param string $name the property name
|
||
13 years ago
|
* @param boolean $checkVar whether to treat member variables as properties
|
||
13 years ago
|
* @return boolean whether the property can be read
|
||
|
* @see canSetProperty
|
||
|
*/
|
||
13 years ago
|
public function canGetProperty($name, $checkVar = true)
|
||
13 years ago
|
{
|
||
13 years ago
|
return method_exists($this, 'get' . $name) || $checkVar && property_exists($this, $name);
|
||
13 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a value indicating whether a property can be set.
|
||
12 years ago
|
* A property is writable if:
|
||
|
*
|
||
|
* - the class has a setter method associated with the specified name
|
||
|
* (in this case, property name is case-insensitive);
|
||
|
* - the class has a member variable with the specified name (when `$checkVar` is true);
|
||
|
*
|
||
13 years ago
|
* @param string $name the property name
|
||
13 years ago
|
* @param boolean $checkVar whether to treat member variables as properties
|
||
13 years ago
|
* @return boolean whether the property can be written
|
||
|
* @see canGetProperty
|
||
|
*/
|
||
13 years ago
|
public function canSetProperty($name, $checkVar = true)
|
||
13 years ago
|
{
|
||
13 years ago
|
return method_exists($this, 'set' . $name) || $checkVar && property_exists($this, $name);
|
||
13 years ago
|
}
|
||
|
}
|