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