Object.php 6.78 KB
Newer Older
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5 6 7 8 9 10
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

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

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

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

	/**
	 * 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;
105
		if (method_exists($this, $getter)) {
106
			return $this->$getter() !== null;
Qiang Xue committed
107
		} else {
Qiang Xue committed
108
			return false;
109 110 111 112
		}
	}

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

Qiang Xue committed
133 134 135 136 137 138 139 140 141
	/**
	 * 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
142
	 * @throws UnknownMethodException when calling unknown method
Qiang Xue committed
143 144 145 146
	 * @return mixed the method return value
	 */
	public function __call($name, $params)
	{
Qiang Xue committed
147 148 149
		$getter = 'get' . $name;
		if (method_exists($this, $getter)) {
			$func = $this->$getter();
Qiang Xue committed
150 151 152 153
			if ($func instanceof \Closure) {
				return call_user_func_array($func, $params);
			}
		}
Qiang Xue committed
154
		throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
Qiang Xue committed
155 156
	}

157 158
	/**
	 * Returns a value indicating whether a property is defined.
Qiang Xue committed
159 160 161 162 163 164
	 * 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);
	 *
165
	 * @param string $name the property name
Qiang Xue committed
166
	 * @param boolean $checkVar whether to treat member variables as properties
167 168 169 170
	 * @return boolean whether the property is defined
	 * @see canGetProperty
	 * @see canSetProperty
	 */
Qiang Xue committed
171
	public function hasProperty($name, $checkVar = true)
172
	{
Qiang Xue committed
173
		return $this->canGetProperty($name, $checkVar) || $this->canSetProperty($name, false);
174 175 176 177
	}

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

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