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.

262 lines
7.8 KiB

<?php
/**
* Object class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
13 years ago
* Object is the base class that provides the *property* feature.
*
13 years ago
* @include @yii/base/Object.md
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Object
{
/**
13 years ago
* Constructor.
*/
public function __construct()
{
}
/**
13 years ago
* Returns the value of an object property.
*
* 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
* @throws Exception if the property is not defined
* @see __set
*/
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
13 years ago
} else {
13 years ago
throw new Exception('Getting unknown property: ' . get_class($this) . '.' . $name);
}
}
/**
13 years ago
* Sets value of an object property.
*
* 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
* @throws Exception if the property is not defined or read-only.
* @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)) {
throw new Exception('Setting read-only property: ' . get_class($this) . '.' . $name);
13 years ago
} else {
throw new Exception('Setting unknown property: ' . get_class($this) . '.' . $name);
}
}
/**
* 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;
if (method_exists($this, $getter)) {
// property is not null
return $this->$getter() !== null;
13 years ago
} else {
13 years ago
return false;
}
}
/**
13 years ago
* Sets an object property to null.
*
* 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
* @throws Exception if the property is read only.
*/
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// write property
$this->$setter(null);
13 years ago
} elseif (method_exists($this, 'get' . $name)) {
throw new Exception('Unsetting read-only property: ' . get_class($this) . '.' . $name);
}
}
/**
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
* @throws Exception when calling unknown method
13 years ago
* @return mixed the method return value
*/
public function __call($name, $params)
{
if ($this->canGetProperty($name, false)) {
$getter = 'get' . $name;
$func = $this->$getter;
if ($func instanceof \Closure) {
return call_user_func_array($func, $params);
}
}
throw new Exception('Unknown method: ' . get_class($this) . "::$name()");
}
/**
* Returns a value indicating whether a property is defined.
* A property is defined if there is a getter or setter method
* defined in the class. Note that property names are case-insensitive.
* @param string $name the property name
13 years ago
* @param boolean $checkVar whether to treat member variables as properties
* @return boolean whether the property is defined
* @see canGetProperty
* @see canSetProperty
*/
13 years ago
public function hasProperty($name, $checkVar = true)
{
13 years ago
return $this->canGetProperty($name, false) || $this->canSetProperty($name, false) || $checkVar && property_exists($this, $name);
}
/**
* Returns a value indicating whether a property can be read.
* A property can be read if the class has a getter method
* for the property name. Note that property name is case-insensitive.
* @param string $name the property name
13 years ago
* @param boolean $checkVar whether to treat member variables as properties
* @return boolean whether the property can be read
* @see canSetProperty
*/
13 years ago
public function canGetProperty($name, $checkVar = true)
{
13 years ago
return method_exists($this, 'get' . $name) || $checkVar && property_exists($this, $name);
}
/**
* Returns a value indicating whether a property can be set.
* A property can be written if the class has a setter method
* for the property name. Note that property name is case-insensitive.
* @param string $name the property name
13 years ago
* @param boolean $checkVar whether to treat member variables as properties
* @return boolean whether the property can be written
* @see canGetProperty
*/
13 years ago
public function canSetProperty($name, $checkVar = true)
{
13 years ago
return $checkVar && property_exists($this, $name) || method_exists($this, 'set' . $name);
}
/**
13 years ago
* Creates a new instance of the calling class.
*
13 years ago
* The newly created object will be initialized with the specified configuration.
*
* Extra parameters passed to this method will be used as the parameters to the object
* constructor.
*
13 years ago
* This method does the following steps to create a object:
*
* - create the object using the PHP `new` operator;
* - if [[Yii::objectConfig]] contains the configuration for the object class,
13 years ago
* it will be merged with the $config parameter;
* - initialize the object properties using the configuration passed to this method;
13 years ago
* - call the `init` method of the object if it implements the [[yii\base\Initable]] interface.
*
* For example,
*
* ~~~
13 years ago
* class Foo extends \yii\base\Object implements \yii\base\Initable
* {
13 years ago
* public $c;
* public function __construct($a, $b)
* {
* ...
* }
* public function init()
* {
* ...
* }
* }
*
13 years ago
* $model = Foo::newInstance(array('c' => 3), 1, 2);
* // which is equivalent to the following lines:
* $model = new Foo(1, 2);
* $model->c = 3;
13 years ago
* $model->init();
* ~~~
*
13 years ago
* @param array $config the object configuration (name-value pairs that will be used to initialize the object)
* @return \yii\base\Object the created object
* @throws Exception if the configuration is invalid.
*/
13 years ago
public static function newInstance($config = array())
{
13 years ago
$class = get_called_class();
13 years ago
13 years ago
if (($n = func_num_args()) > 1) {
$args = func_get_args();
13 years ago
if ($n === 2) {
$object = new $class($args[1]);
} elseif ($n === 3) {
$object = new $class($args[1], $args[2]);
} elseif ($n === 4) {
$object = new $class($args[1], $args[2], $args[3]);
} else {
// remove $config
array_shift($args);
13 years ago
$r = new \ReflectionClass($class);
$object = $r->newInstanceArgs($args);
}
13 years ago
} else {
13 years ago
$object = new $class;
13 years ago
}
13 years ago
if (isset(\Yii::$objectConfig[$class])) {
$config = array_merge(\Yii::$objectConfig[$class], $config);
13 years ago
}
13 years ago
foreach ($config as $name => $value) {
$object->$name = $value;
13 years ago
}
if ($object instanceof Initable) {
13 years ago
$object->init();
}
return $object;
}
}