Object.php 7.49 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
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

Qiang Xue committed
10 11
use Yii;

12
/**
13 14
 * Object is the base class that implements the *property* feature.
 *
Qiang Xue committed
15
 * @include @yii/base/Object.md
16
 *
17 18 19
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
20
class Object implements Arrayable
21
{
22 23 24 25 26 27 28 29
	/**
	 * @return string the fully qualified name of this class.
	 */
	public static function className()
	{
		return get_called_class();
	}

Qiang Xue committed
30 31
	/**
	 * Constructor.
Qiang Xue committed
32 33 34 35 36 37 38 39 40 41 42 43
	 * 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
	 */
Alexander Makarov committed
44
	public function __construct($config = [])
Qiang Xue committed
45
	{
Qiang Xue committed
46 47
		if (!empty($config)) {
			Yii::configure($this, $config);
Qiang Xue committed
48 49 50 51 52 53 54 55
		}
		$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
56
	 */
Qiang Xue committed
57
	public function init()
Qiang Xue committed
58 59 60
	{
	}

61
	/**
mdomba (mdlap) committed
62
	 * Returns the value of an object property.
63 64 65 66
	 *
	 * 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
67
	 * @return mixed the property value
Qiang Xue committed
68
	 * @throws UnknownPropertyException if the property is not defined
69
	 * @throws InvalidCallException if the property is write-only
70
	 * @see __set()
71 72 73 74 75 76
	 */
	public function __get($name)
	{
		$getter = 'get' . $name;
		if (method_exists($this, $getter)) {
			return $this->$getter();
77 78
		} elseif (method_exists($this, 'set' . $name)) {
			throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
Qiang Xue committed
79
		} else {
Qiang Xue committed
80
			throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
81 82 83 84
		}
	}

	/**
mdomba (mdlap) committed
85
	 * Sets value of an object property.
86 87 88 89 90
	 *
	 * 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
91
	 * @throws UnknownPropertyException if the property is not defined
92
	 * @throws InvalidCallException if the property is read-only
93
	 * @see __get()
94 95 96 97 98
	 */
	public function __set($name, $value)
	{
		$setter = 'set' . $name;
		if (method_exists($this, $setter)) {
Qiang Xue committed
99
			$this->$setter($value);
Qiang Xue committed
100
		} elseif (method_exists($this, 'get' . $name)) {
Qiang Xue committed
101
			throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
Qiang Xue committed
102
		} else {
Qiang Xue committed
103
			throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		}
	}

	/**
	 * 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;
120
		if (method_exists($this, $getter)) {
121
			return $this->$getter() !== null;
Qiang Xue committed
122
		} else {
Qiang Xue committed
123
			return false;
124 125 126 127
		}
	}

	/**
mdomba (mdlap) committed
128
	 * Sets an object property to null.
129 130 131 132 133 134 135
	 *
	 * 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
136
	 * @throws InvalidCallException if the property is read only.
137 138 139 140
	 */
	public function __unset($name)
	{
		$setter = 'set' . $name;
141
		if (method_exists($this, $setter)) {
142
			$this->$setter(null);
Qiang Xue committed
143
		} elseif (method_exists($this, 'get' . $name)) {
Qiang Xue committed
144
			throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
145 146 147
		}
	}

Qiang Xue committed
148 149 150 151 152 153 154
	/**
	 * Calls the named method which is not a class method.
	 *
	 * 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
155
	 * @throws UnknownMethodException when calling unknown method
Qiang Xue committed
156 157 158 159
	 * @return mixed the method return value
	 */
	public function __call($name, $params)
	{
Qiang Xue committed
160
		throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
Qiang Xue committed
161 162
	}

163 164
	/**
	 * Returns a value indicating whether a property is defined.
Qiang Xue committed
165 166 167 168
	 * 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);
crtlib committed
169
	 * - the class has a member variable with the specified name (when `$checkVars` is true);
Qiang Xue committed
170
	 *
171
	 * @param string $name the property name
crtlib committed
172
	 * @param boolean $checkVars whether to treat member variables as properties
173
	 * @return boolean whether the property is defined
174 175
	 * @see canGetProperty()
	 * @see canSetProperty()
176
	 */
crtlib committed
177
	public function hasProperty($name, $checkVars = true)
178
	{
crtlib committed
179
		return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
180 181 182 183
	}

	/**
	 * Returns a value indicating whether a property can be read.
Qiang Xue committed
184 185 186 187
	 * A property is readable if:
	 *
	 * - the class has a getter method associated with the specified name
	 *   (in this case, property name is case-insensitive);
crtlib committed
188
	 * - the class has a member variable with the specified name (when `$checkVars` is true);
Qiang Xue committed
189
	 *
190
	 * @param string $name the property name
crtlib committed
191
	 * @param boolean $checkVars whether to treat member variables as properties
192
	 * @return boolean whether the property can be read
193
	 * @see canSetProperty()
194
	 */
crtlib committed
195
	public function canGetProperty($name, $checkVars = true)
196
	{
crtlib committed
197
		return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
198 199 200 201
	}

	/**
	 * Returns a value indicating whether a property can be set.
Qiang Xue committed
202 203 204 205
	 * A property is writable if:
	 *
	 * - the class has a setter method associated with the specified name
	 *   (in this case, property name is case-insensitive);
crtlib committed
206
	 * - the class has a member variable with the specified name (when `$checkVars` is true);
Qiang Xue committed
207
	 *
208
	 * @param string $name the property name
crtlib committed
209
	 * @param boolean $checkVars whether to treat member variables as properties
210
	 * @return boolean whether the property can be written
211
	 * @see canGetProperty()
212
	 */
crtlib committed
213
	public function canSetProperty($name, $checkVars = true)
214
	{
crtlib committed
215
		return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
216
	}
Qiang Xue committed
217

218 219 220 221 222 223 224 225 226 227 228 229 230
	/**
	 * Returns a value indicating whether a method is defined.
	 *
	 * The default implementation is a call to php function `method_exists()`.
	 * You may override this method when you implemented the php magic method `__call()`.
	 * @param string $name the property name
	 * @return boolean whether the property is defined
	 */
	public function hasMethod($name)
	{
		return method_exists($this, $name);
	}

Qiang Xue committed
231
	/**
232 233 234
	 * Converts the object into an array.
	 * The default implementation will return all public property values as an array.
	 * @return array the array representation of the object
Qiang Xue committed
235
	 */
236
	public function toArray()
Qiang Xue committed
237
	{
238
		return Yii::getObjectVars($this);
Qiang Xue committed
239
	}
240
}