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

/framework/base/Object.php

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