PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/yii/base/Object.php

https://github.com/stefan321/yii2
PHP | 219 lines | 80 code | 12 blank | 127 comment | 14 complexity | 9a67d6508c919a6f88329698d1d8c6c5 MD5 | raw file
  1<?php
  2/**
  3 * @link http://www.yiiframework.com/
  4 * @copyright Copyright (c) 2008 Yii Software LLC
  5 * @license http://www.yiiframework.com/license/
  6 */
  7
  8namespace yii\base;
  9
 10/**
 11 * @include @yii/base/Object.md
 12 * @author Qiang Xue <qiang.xue@gmail.com>
 13 * @since 2.0
 14 */
 15class Object
 16{
 17	/**
 18	 * @return string the fully qualified name of this class.
 19	 */
 20	public static function className()
 21	{
 22		return get_called_class();
 23	}
 24
 25	/**
 26	 * Constructor.
 27	 * The default implementation does two things:
 28	 *
 29	 * - Initializes the object with the given configuration `$config`.
 30	 * - Call [[init()]].
 31	 *
 32	 * If this method is overridden in a child class, it is recommended that
 33	 *
 34	 * - the last parameter of the constructor is a configuration array, like `$config` here.
 35	 * - call the parent implementation at the end of the constructor.
 36	 *
 37	 * @param array $config name-value pairs that will be used to initialize the object properties
 38	 */
 39	public function __construct($config = array())
 40	{
 41		foreach ($config as $name => $value) {
 42			$this->$name = $value;
 43		}
 44		$this->init();
 45	}
 46
 47	/**
 48	 * Initializes the object.
 49	 * This method is invoked at the end of the constructor after the object is initialized with the
 50	 * given configuration.
 51	 */
 52	public function init()
 53	{
 54	}
 55
 56	/**
 57	 * Returns the value of an object property.
 58	 *
 59	 * Do not call this method directly as it is a PHP magic method that
 60	 * will be implicitly called when executing `$value = $object->property;`.
 61	 * @param string $name the property name
 62	 * @return mixed the property value, event handlers attached to the event,
 63	 * the named behavior, or the value of a behavior's property
 64	 * @throws UnknownPropertyException if the property is not defined
 65	 * @see __set
 66	 */
 67	public function __get($name)
 68	{
 69		$getter = 'get' . $name;
 70		if (method_exists($this, $getter)) {
 71			return $this->$getter();
 72		} else {
 73			throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
 74		}
 75	}
 76
 77	/**
 78	 * Sets value of an object property.
 79	 *
 80	 * Do not call this method directly as it is a PHP magic method that
 81	 * will be implicitly called when executing `$object->property = $value;`.
 82	 * @param string $name the property name or the event name
 83	 * @param mixed $value the property value
 84	 * @throws UnknownPropertyException if the property is not defined
 85	 * @throws InvalidCallException if the property is read-only.
 86	 * @see __get
 87	 */
 88	public function __set($name, $value)
 89	{
 90		$setter = 'set' . $name;
 91		if (method_exists($this, $setter)) {
 92			$this->$setter($value);
 93		} elseif (method_exists($this, 'get' . $name)) {
 94			throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
 95		} else {
 96			throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
 97		}
 98	}
 99
100	/**
101	 * Checks if the named property is set (not null).
102	 *
103	 * Do not call this method directly as it is a PHP magic method that
104	 * will be implicitly called when executing `isset($object->property)`.
105	 *
106	 * Note that if the property is not defined, false will be returned.
107	 * @param string $name the property name or the event name
108	 * @return boolean whether the named property is set (not null).
109	 */
110	public function __isset($name)
111	{
112		$getter = 'get' . $name;
113		if (method_exists($this, $getter)) {
114			return $this->$getter() !== null;
115		} else {
116			return false;
117		}
118	}
119
120	/**
121	 * Sets an object property to null.
122	 *
123	 * Do not call this method directly as it is a PHP magic method that
124	 * will be implicitly called when executing `unset($object->property)`.
125	 *
126	 * Note that if the property is not defined, this method will do nothing.
127	 * If the property is read-only, it will throw an exception.
128	 * @param string $name the property name
129	 * @throws InvalidCallException if the property is read only.
130	 */
131	public function __unset($name)
132	{
133		$setter = 'set' . $name;
134		if (method_exists($this, $setter)) {
135			$this->$setter(null);
136		} elseif (method_exists($this, 'get' . $name)) {
137			throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
138		}
139	}
140
141	/**
142	 * Calls the named method which is not a class method.
143	 * If the name refers to a component property whose value is
144	 * an anonymous function, the method will execute the function.
145	 *
146	 * Do not call this method directly as it is a PHP magic method that
147	 * will be implicitly called when an unknown method is being invoked.
148	 * @param string $name the method name
149	 * @param array $params method parameters
150	 * @throws UnknownMethodException when calling unknown method
151	 * @return mixed the method return value
152	 */
153	public function __call($name, $params)
154	{
155		$getter = 'get' . $name;
156		if (method_exists($this, $getter)) {
157			$func = $this->$getter();
158			if ($func instanceof \Closure) {
159				return call_user_func_array($func, $params);
160			}
161		}
162		throw new UnknownMethodException('Unknown method: ' . get_class($this) . "::$name()");
163	}
164
165	/**
166	 * Returns a value indicating whether a property is defined.
167	 * A property is defined if:
168	 *
169	 * - the class has a getter or setter method associated with the specified name
170	 *   (in this case, property name is case-insensitive);
171	 * - the class has a member variable with the specified name (when `$checkVar` is true);
172	 *
173	 * @param string $name the property name
174	 * @param boolean $checkVar whether to treat member variables as properties
175	 * @return boolean whether the property is defined
176	 * @see canGetProperty
177	 * @see canSetProperty
178	 */
179	public function hasProperty($name, $checkVar = true)
180	{
181		return $this->canGetProperty($name, $checkVar) || $this->canSetProperty($name, false);
182	}
183
184	/**
185	 * Returns a value indicating whether a property can be read.
186	 * A property is readable if:
187	 *
188	 * - the class has a getter method associated with the specified name
189	 *   (in this case, property name is case-insensitive);
190	 * - the class has a member variable with the specified name (when `$checkVar` is true);
191	 *
192	 * @param string $name the property name
193	 * @param boolean $checkVar whether to treat member variables as properties
194	 * @return boolean whether the property can be read
195	 * @see canSetProperty
196	 */
197	public function canGetProperty($name, $checkVar = true)
198	{
199		return method_exists($this, 'get' . $name) || $checkVar && property_exists($this, $name);
200	}
201
202	/**
203	 * Returns a value indicating whether a property can be set.
204	 * A property is writable if:
205	 *
206	 * - the class has a setter method associated with the specified name
207	 *   (in this case, property name is case-insensitive);
208	 * - the class has a member variable with the specified name (when `$checkVar` is true);
209	 *
210	 * @param string $name the property name
211	 * @param boolean $checkVar whether to treat member variables as properties
212	 * @return boolean whether the property can be written
213	 * @see canGetProperty
214	 */
215	public function canSetProperty($name, $checkVar = true)
216	{
217		return method_exists($this, 'set' . $name) || $checkVar && property_exists($this, $name);
218	}
219}