PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/base/Object.php

https://github.com/FrediL/yii2
PHP | 288 lines | 80 code | 14 blank | 194 comment | 13 complexity | e26126b43e7342eda21586922869a829 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  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. namespace yii\base;
  8. use Yii;
  9. /**
  10. * Object is the base class that implements the *property* feature.
  11. *
  12. * A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example,
  13. * the following getter and setter methods define a property named `label`:
  14. *
  15. * ~~~
  16. * private $_label;
  17. *
  18. * public function getLabel()
  19. * {
  20. * return $this->_label;
  21. * }
  22. *
  23. * public function setLabel($value)
  24. * {
  25. * $this->_label = $value;
  26. * }
  27. * ~~~
  28. *
  29. * Property names are *case-insensitive*.
  30. *
  31. * A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation
  32. * of the corresponding getter or setter method. For example,
  33. *
  34. * ~~~
  35. * // equivalent to $label = $object->getLabel();
  36. * $label = $object->label;
  37. * // equivalent to $object->setLabel('abc');
  38. * $object->label = 'abc';
  39. * ~~~
  40. *
  41. * If a property has only a getter method and has no setter method, it is considered as *read-only*. In this case, trying
  42. * to modify the property value will cause an exception.
  43. *
  44. * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property.
  45. *
  46. * Besides the property feature, Object also introduces an important object initialization life cycle. In particular,
  47. * creating an new instance of Object or its derived class will involve the following life cycles sequentially:
  48. *
  49. * 1. the class constructor is invoked;
  50. * 2. object properties are initialized according to the given configuration;
  51. * 3. the `init()` method is invoked.
  52. *
  53. * In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that
  54. * you perform object initialization in the `init()` method because at that stage, the object configuration
  55. * is already applied.
  56. *
  57. * In order to ensure the above life cycles, if a child class of Object needs to override the constructor,
  58. * it should be done like the following:
  59. *
  60. * ~~~
  61. * public function __construct($param1, $param2, ..., $config = [])
  62. * {
  63. * ...
  64. * parent::__construct($config);
  65. * }
  66. * ~~~
  67. *
  68. * That is, a `$config` parameter (defaults to `[]`) should be declared as the last parameter
  69. * of the constructor, and the parent implementation should be called at the end of the constructor.
  70. *
  71. * @author Qiang Xue <qiang.xue@gmail.com>
  72. * @since 2.0
  73. */
  74. class Object
  75. {
  76. /**
  77. * Returns the fully qualified name of this class.
  78. * @return string the fully qualified name of this class.
  79. */
  80. public static function className()
  81. {
  82. return get_called_class();
  83. }
  84. /**
  85. * Constructor.
  86. * The default implementation does two things:
  87. *
  88. * - Initializes the object with the given configuration `$config`.
  89. * - Call [[init()]].
  90. *
  91. * If this method is overridden in a child class, it is recommended that
  92. *
  93. * - the last parameter of the constructor is a configuration array, like `$config` here.
  94. * - call the parent implementation at the end of the constructor.
  95. *
  96. * @param array $config name-value pairs that will be used to initialize the object properties
  97. */
  98. public function __construct($config = [])
  99. {
  100. if (!empty($config)) {
  101. Yii::configure($this, $config);
  102. }
  103. $this->init();
  104. }
  105. /**
  106. * Initializes the object.
  107. * This method is invoked at the end of the constructor after the object is initialized with the
  108. * given configuration.
  109. */
  110. public function init()
  111. {
  112. }
  113. /**
  114. * Returns the value of an object property.
  115. *
  116. * Do not call this method directly as it is a PHP magic method that
  117. * will be implicitly called when executing `$value = $object->property;`.
  118. * @param string $name the property name
  119. * @return mixed the property value
  120. * @throws UnknownPropertyException if the property is not defined
  121. * @throws InvalidCallException if the property is write-only
  122. * @see __set()
  123. */
  124. public function __get($name)
  125. {
  126. $getter = 'get' . $name;
  127. if (method_exists($this, $getter)) {
  128. return $this->$getter();
  129. } elseif (method_exists($this, 'set' . $name)) {
  130. throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
  131. } else {
  132. throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
  133. }
  134. }
  135. /**
  136. * Sets value of an object property.
  137. *
  138. * Do not call this method directly as it is a PHP magic method that
  139. * will be implicitly called when executing `$object->property = $value;`.
  140. * @param string $name the property name or the event name
  141. * @param mixed $value the property value
  142. * @throws UnknownPropertyException if the property is not defined
  143. * @throws InvalidCallException if the property is read-only
  144. * @see __get()
  145. */
  146. public function __set($name, $value)
  147. {
  148. $setter = 'set' . $name;
  149. if (method_exists($this, $setter)) {
  150. $this->$setter($value);
  151. } elseif (method_exists($this, 'get' . $name)) {
  152. throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
  153. } else {
  154. throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
  155. }
  156. }
  157. /**
  158. * Checks if the named property is set (not null).
  159. *
  160. * Do not call this method directly as it is a PHP magic method that
  161. * will be implicitly called when executing `isset($object->property)`.
  162. *
  163. * Note that if the property is not defined, false will be returned.
  164. * @param string $name the property name or the event name
  165. * @return boolean whether the named property is set (not null).
  166. */
  167. public function __isset($name)
  168. {
  169. $getter = 'get' . $name;
  170. if (method_exists($this, $getter)) {
  171. return $this->$getter() !== null;
  172. } else {
  173. return false;
  174. }
  175. }
  176. /**
  177. * Sets an object property to null.
  178. *
  179. * Do not call this method directly as it is a PHP magic method that
  180. * will be implicitly called when executing `unset($object->property)`.
  181. *
  182. * Note that if the property is not defined, this method will do nothing.
  183. * If the property is read-only, it will throw an exception.
  184. * @param string $name the property name
  185. * @throws InvalidCallException if the property is read only.
  186. */
  187. public function __unset($name)
  188. {
  189. $setter = 'set' . $name;
  190. if (method_exists($this, $setter)) {
  191. $this->$setter(null);
  192. } elseif (method_exists($this, 'get' . $name)) {
  193. throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
  194. }
  195. }
  196. /**
  197. * Calls the named method which is not a class method.
  198. *
  199. * Do not call this method directly as it is a PHP magic method that
  200. * will be implicitly called when an unknown method is being invoked.
  201. * @param string $name the method name
  202. * @param array $params method parameters
  203. * @throws UnknownMethodException when calling unknown method
  204. * @return mixed the method return value
  205. */
  206. public function __call($name, $params)
  207. {
  208. throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
  209. }
  210. /**
  211. * Returns a value indicating whether a property is defined.
  212. * A property is defined if:
  213. *
  214. * - the class has a getter or setter method associated with the specified name
  215. * (in this case, property name is case-insensitive);
  216. * - the class has a member variable with the specified name (when `$checkVars` is true);
  217. *
  218. * @param string $name the property name
  219. * @param boolean $checkVars whether to treat member variables as properties
  220. * @return boolean whether the property is defined
  221. * @see canGetProperty()
  222. * @see canSetProperty()
  223. */
  224. public function hasProperty($name, $checkVars = true)
  225. {
  226. return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
  227. }
  228. /**
  229. * Returns a value indicating whether a property can be read.
  230. * A property is readable if:
  231. *
  232. * - the class has a getter method associated with the specified name
  233. * (in this case, property name is case-insensitive);
  234. * - the class has a member variable with the specified name (when `$checkVars` is true);
  235. *
  236. * @param string $name the property name
  237. * @param boolean $checkVars whether to treat member variables as properties
  238. * @return boolean whether the property can be read
  239. * @see canSetProperty()
  240. */
  241. public function canGetProperty($name, $checkVars = true)
  242. {
  243. return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
  244. }
  245. /**
  246. * Returns a value indicating whether a property can be set.
  247. * A property is writable if:
  248. *
  249. * - the class has a setter method associated with the specified name
  250. * (in this case, property name is case-insensitive);
  251. * - the class has a member variable with the specified name (when `$checkVars` is true);
  252. *
  253. * @param string $name the property name
  254. * @param boolean $checkVars whether to treat member variables as properties
  255. * @return boolean whether the property can be written
  256. * @see canGetProperty()
  257. */
  258. public function canSetProperty($name, $checkVars = true)
  259. {
  260. return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
  261. }
  262. /**
  263. * Returns a value indicating whether a method is defined.
  264. *
  265. * The default implementation is a call to php function `method_exists()`.
  266. * You may override this method when you implemented the php magic method `__call()`.
  267. * @param string $name the property name
  268. * @return boolean whether the property is defined
  269. */
  270. public function hasMethod($name)
  271. {
  272. return method_exists($this, $name);
  273. }
  274. }