You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
10KB

  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. * ```php
  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. * ```php
  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. * ```php
  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 implements Configurable
  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 a property is set, i.e. defined and 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. * @see http://php.net/manual/en/function.isset.php
  167. */
  168. public function __isset($name)
  169. {
  170. $getter = 'get' . $name;
  171. if (method_exists($this, $getter)) {
  172. return $this->$getter() !== null;
  173. } else {
  174. return false;
  175. }
  176. }
  177. /**
  178. * Sets an object property to null.
  179. *
  180. * Do not call this method directly as it is a PHP magic method that
  181. * will be implicitly called when executing `unset($object->property)`.
  182. *
  183. * Note that if the property is not defined, this method will do nothing.
  184. * If the property is read-only, it will throw an exception.
  185. * @param string $name the property name
  186. * @throws InvalidCallException if the property is read only.
  187. * @see http://php.net/manual/en/function.unset.php
  188. */
  189. public function __unset($name)
  190. {
  191. $setter = 'set' . $name;
  192. if (method_exists($this, $setter)) {
  193. $this->$setter(null);
  194. } elseif (method_exists($this, 'get' . $name)) {
  195. throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
  196. }
  197. }
  198. /**
  199. * Calls the named method which is not a class method.
  200. *
  201. * Do not call this method directly as it is a PHP magic method that
  202. * will be implicitly called when an unknown method is being invoked.
  203. * @param string $name the method name
  204. * @param array $params method parameters
  205. * @throws UnknownMethodException when calling unknown method
  206. * @return mixed the method return value
  207. */
  208. public function __call($name, $params)
  209. {
  210. throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
  211. }
  212. /**
  213. * Returns a value indicating whether a property is defined.
  214. * A property is defined if:
  215. *
  216. * - the class has a getter or setter method associated with the specified name
  217. * (in this case, property name is case-insensitive);
  218. * - the class has a member variable with the specified name (when `$checkVars` is true);
  219. *
  220. * @param string $name the property name
  221. * @param boolean $checkVars whether to treat member variables as properties
  222. * @return boolean whether the property is defined
  223. * @see canGetProperty()
  224. * @see canSetProperty()
  225. */
  226. public function hasProperty($name, $checkVars = true)
  227. {
  228. return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
  229. }
  230. /**
  231. * Returns a value indicating whether a property can be read.
  232. * A property is readable if:
  233. *
  234. * - the class has a getter method associated with the specified name
  235. * (in this case, property name is case-insensitive);
  236. * - the class has a member variable with the specified name (when `$checkVars` is true);
  237. *
  238. * @param string $name the property name
  239. * @param boolean $checkVars whether to treat member variables as properties
  240. * @return boolean whether the property can be read
  241. * @see canSetProperty()
  242. */
  243. public function canGetProperty($name, $checkVars = true)
  244. {
  245. return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
  246. }
  247. /**
  248. * Returns a value indicating whether a property can be set.
  249. * A property is writable if:
  250. *
  251. * - the class has a setter method associated with the specified name
  252. * (in this case, property name is case-insensitive);
  253. * - the class has a member variable with the specified name (when `$checkVars` is true);
  254. *
  255. * @param string $name the property name
  256. * @param boolean $checkVars whether to treat member variables as properties
  257. * @return boolean whether the property can be written
  258. * @see canGetProperty()
  259. */
  260. public function canSetProperty($name, $checkVars = true)
  261. {
  262. return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
  263. }
  264. /**
  265. * Returns a value indicating whether a method is defined.
  266. *
  267. * The default implementation is a call to php function `method_exists()`.
  268. * You may override this method when you implemented the php magic method `__call()`.
  269. * @param string $name the method name
  270. * @return boolean whether the method is defined
  271. */
  272. public function hasMethod($name)
  273. {
  274. return method_exists($this, $name);
  275. }
  276. }