Object.php 7.4 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
/**
Qiang Xue committed
13
 * @include @yii/base/Object.md
14 15 16
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
17
class Object implements Arrayable
18
{
19 20 21 22 23 24 25 26
	/**
	 * @return string the fully qualified name of this class.
	 */
	public static function className()
	{
		return get_called_class();
	}

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

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

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

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

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

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

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

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

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

215 216 217 218 219 220 221 222 223 224 225 226 227
	/**
	 * 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
228
	/**
229 230 231
	 * 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
232
	 */
233
	public function toArray()
Qiang Xue committed
234
	{
235
		return Yii::getObjectVars($this);
Qiang Xue committed
236
	}
237
}