PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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