Object.php 6.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?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;

/**
Qiang Xue committed
13
 * Object is the base class that provides the *property* feature.
14
 *
Qiang Xue committed
15
 * @include @yii/base/Object.md
16 17 18 19 20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Object
{
Qiang Xue committed
22 23
	/**
	 * Constructor.
Qiang Xue committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	 * 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.
Qiang Xue committed
48
	 */
Qiang Xue committed
49
	public function init()
Qiang Xue committed
50 51 52
	{
	}

53
	/**
mdomba (mdlap) committed
54
	 * Returns the value of an object property.
55 56 57 58 59 60
	 *
	 * 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
Qiang Xue committed
61
	 * @throws BadPropertyException if the property is not defined
62 63 64 65 66 67 68
	 * @see __set
	 */
	public function __get($name)
	{
		$getter = 'get' . $name;
		if (method_exists($this, $getter)) {
			return $this->$getter();
Qiang Xue committed
69
		} else {
Qiang Xue committed
70
			throw new BadPropertyException('Getting unknown property: ' . get_class($this) . '.' . $name);
71 72 73 74
		}
	}

	/**
mdomba (mdlap) committed
75
	 * Sets value of an object property.
76 77 78 79 80
	 *
	 * 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
Qiang Xue committed
81
	 * @throws BadPropertyException if the property is not defined or read-only.
82 83 84 85 86 87
	 * @see __get
	 */
	public function __set($name, $value)
	{
		$setter = 'set' . $name;
		if (method_exists($this, $setter)) {
Qiang Xue committed
88
			$this->$setter($value);
Qiang Xue committed
89
		} elseif (method_exists($this, 'get' . $name)) {
Qiang Xue committed
90
			throw new BadPropertyException('Setting read-only property: ' . get_class($this) . '.' . $name);
Qiang Xue committed
91
		} else {
Qiang Xue committed
92
			throw new BadPropertyException('Setting unknown property: ' . get_class($this) . '.' . $name);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		}
	}

	/**
	 * 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;
109 110
		if (method_exists($this, $getter)) {
			// property is not null
111
			return $this->$getter() !== null;
Qiang Xue committed
112
		} else {
Qiang Xue committed
113
			return false;
114 115 116 117
		}
	}

	/**
mdomba (mdlap) committed
118
	 * Sets an object property to null.
119 120 121 122 123 124 125
	 *
	 * 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
Qiang Xue committed
126
	 * @throws BadPropertyException if the property is read only.
127 128 129 130
	 */
	public function __unset($name)
	{
		$setter = 'set' . $name;
131 132
		if (method_exists($this, $setter)) {
			// write property
133
			$this->$setter(null);
Qiang Xue committed
134
		} elseif (method_exists($this, 'get' . $name)) {
Qiang Xue committed
135
			throw new BadPropertyException('Unsetting read-only property: ' . get_class($this) . '.' . $name);
136 137 138
		}
	}

Qiang Xue committed
139 140 141 142 143 144 145 146 147
	/**
	 * 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
Qiang Xue committed
148
	 * @throws BadMethodException when calling unknown method
Qiang Xue committed
149 150 151 152 153 154 155 156 157 158 159
	 * @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);
			}
		}
Qiang Xue committed
160
		throw new BadMethodException('Unknown method: ' . get_class($this) . "::$name()");
Qiang Xue committed
161 162
	}

163 164 165 166 167
	/**
	 * 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
Qiang Xue committed
168
	 * @param boolean $checkVar whether to treat member variables as properties
169 170 171 172
	 * @return boolean whether the property is defined
	 * @see canGetProperty
	 * @see canSetProperty
	 */
Qiang Xue committed
173
	public function hasProperty($name, $checkVar = true)
174
	{
Qiang Xue committed
175
		return $this->canGetProperty($name, $checkVar) || $this->canSetProperty($name, false);
176 177 178 179 180 181 182
	}

	/**
	 * 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
Qiang Xue committed
183
	 * @param boolean $checkVar whether to treat member variables as properties
184 185 186
	 * @return boolean whether the property can be read
	 * @see canSetProperty
	 */
Qiang Xue committed
187
	public function canGetProperty($name, $checkVar = true)
188
	{
Qiang Xue committed
189
		return method_exists($this, 'get' . $name) || $checkVar && property_exists($this, $name);
190 191 192 193 194 195 196
	}

	/**
	 * 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
Qiang Xue committed
197
	 * @param boolean $checkVar whether to treat member variables as properties
198 199 200
	 * @return boolean whether the property can be written
	 * @see canGetProperty
	 */
Qiang Xue committed
201
	public function canSetProperty($name, $checkVar = true)
202
	{
Qiang Xue committed
203
		return method_exists($this, 'set' . $name) || $checkVar && property_exists($this, $name);
204 205
	}
}