Yii2源码学习之yii\base\Object代码详解

蔚落 2022-07-15 12:27 242阅读 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. * Object 是一个实现属性功能的基类
  12. *
  13. * A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example,
  14. * the following getter and setter methods define a property named `label`:
  15. *定义了getter和setter方法。例如:
  16. *
  17. *
  18. * ```php
  19. * private $_label;
  20. *
  21. * public function getLabel()
  22. * {
  23. * return $this->_label;
  24. * }
  25. *
  26. * public function setLabel($value)
  27. * {
  28. * $this->_label = $value;
  29. * }
  30. * ```
  31. *
  32. * Property names are *case-insensitive*.
  33. * 属性名大小写不敏感
  34. *
  35. * A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation
  36. * of the corresponding getter or setter method. For example,
  37. * 可以访问对象的属性,如对象的成员变量。读或写一个属性将导致调用相应的getter或setter方法
  38. *
  39. * ```php
  40. * // equivalent to $label = $object->getLabel();
  41. * $label = $object->label;
  42. * // equivalent to $object->setLabel('abc');
  43. * $object->label = 'abc';
  44. * ```
  45. *
  46. * If a property has only a getter method and has no setter method, it is considered as *read-only*. In this case, trying
  47. * to modify the property value will cause an exception.
  48. * 如果一个属性只有getter方法,就只能读,如果写会出现异常。
  49. *
  50. * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property.
  51. * 通过hasProperty canGetProperty或canSetProperty 检查属性是否存在
  52. *
  53. * Besides the property feature, Object also introduces an important object initialization life cycle. In particular,
  54. * creating an new instance of Object or its derived class will involve the following life cycles sequentially:
  55. * 除了属性特征,对象还引入了一个重要的对象初始化生命周期,
  56. * 创建一个新的对象或其派生类的实例,将涉及下列生命周期
  57. *
  58. * 1. the class constructor is invoked;
  59. * 2. object properties are initialized according to the given configuration;
  60. * 3. the `init()` method is invoked.
  61. * 调用构造函数;
  62. * 根据给定的对象属性初始化配置;
  63. * init()调用的方法.
  64. *
  65. * In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that
  66. * you perform object initialization in the `init()` method because at that stage, the object configuration
  67. * is already applied.
  68. * 2和3发生在类构造函数的末端。建议
  69. * 你完成对象的初始化在` init()`方法因为在那个阶段,对象配置已经应用。
  70. *
  71. * In order to ensure the above life cycles, if a child class of Object needs to override the constructor,
  72. * it should be done like the following:
  73. * 为了保证的生命周期,如果一个子类的对象需要重写构造函数,他应该像下面这样
  74. *
  75. * ```php
  76. * public function __construct($param1, $param2, ..., $config = [])
  77. * {
  78. * ...
  79. * parent::__construct($config);
  80. * }
  81. * ```
  82. *
  83. * That is, a `$config` parameter (defaults to `[]`) should be declared as the last parameter
  84. * of the constructor, and the parent implementation should be called at the end of the constructor.
  85. * 一个配置的参数应该声明为最后一个参数,构造函数和父类的实现应该在结尾调用
  86. *
  87. * @author Qiang Xue <qiang.xue@gmail.com>
  88. * @since 2.0
  89. */
  90. class Object implements Configurable
  91. {
  92. /**
  93. * Returns the fully qualified name of this class. 获取静态方法调用的类名,返回类的名称
  94. * @return string the fully qualified name of this class.
  95. */
  96. public static function className()
  97. {
  98. //哪个类调用,就返回哪个类,
  99. return get_called_class();
  100. }
  101. /**
  102. * Constructor.
  103. * The default implementation does two things:
  104. *
  105. * - Initializes the object with the given configuration `$config`.
  106. * - Call [[init()]].
  107. *
  108. * If this method is overridden in a child class, it is recommended that
  109. *
  110. * - the last parameter of the constructor is a configuration array, like `$config` here.
  111. * - call the parent implementation at the end of the constructor.
  112. *
  113. * @param array $config name-value pairs that will be used to initialize the object properties
  114. */
  115. public function __construct($config = [])
  116. {
  117. //根据$config初始化对象
  118. if (!empty($config)) {
  119. Yii::configure($this, $config);//将配置文件里面的所有配置信息赋值给Object,由于Object是大部分类的基类,实际上也就是交给了yii\web\Application 您可以Yii::$app->配置参数来访问配置文件中的内容
  120. }
  121. //调用 init()方法,用于初始化,可以被重写。
  122. $this->init();
  123. }
  124. /**
  125. * Initializes the object.
  126. * This method is invoked at the end of the constructor after the object is initialized with the
  127. * given configuration.
  128. *
  129. * 初始化结束时调用,与给定的配置初始化。
  130. */
  131. //这句实际上执行的是yii\base\Module.php
  132. public function init()
  133. {
  134. }
  135. /**
  136. * Returns the 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 `$value = $object->property;`.
  140. *
  141. * 不要直接调用这个方法,因为它是一个PHP魔术方法,要隐式调用
  142. *
  143. * @param string $name the property name
  144. * @return mixed the property value
  145. * @throws UnknownPropertyException if the property is not defined
  146. * @throws InvalidCallException if the property is write-only
  147. * @see __set()
  148. */
  149. public function __get($name)
  150. {
  151. $getter = 'get' . $name; //定义$getter
  152. if (method_exists($this, $getter)) { //存在方法,直接调用
  153. return $this->$getter();
  154. } elseif (method_exists($this, 'set' . $name)) { // 如果存在 'set' . $name 方法,就认为属性是只写
  155. throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
  156. } else {
  157. // 否则认为该属性不存在 未定义
  158. throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
  159. }
  160. }
  161. /**
  162. * Sets value of an object property.
  163. *
  164. * Do not call this method directly as it is a PHP magic method that
  165. * will be implicitly called when executing `$object->property = $value;`.
  166. * @param string $name the property name or the event name
  167. * @param mixed $value the property value
  168. * @throws UnknownPropertyException if the property is not defined
  169. * @throws InvalidCallException if the property is read-only
  170. * @see __get()
  171. */
  172. public function __set($name, $value)
  173. {
  174. $setter = 'set' . $name;
  175. if (method_exists($this, $setter)) { //对象存在$setter方法,直接调用
  176. $this->$setter($value);
  177. } elseif (method_exists($this, 'get' . $name)) {
  178. // 存在 'get' . $name 方法,就认为该属性是只读
  179. throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
  180. } else {
  181. // 否则认为该属性不存在 未定义
  182. throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
  183. }
  184. }
  185. /**
  186. * 检查属性是否设置
  187. * Checks if a property is set, i.e. defined and not null.
  188. *
  189. * Do not call this method directly as it is a PHP magic method that
  190. * will be implicitly called when executing `isset($object->property)`.
  191. *
  192. * Note that if the property is not defined, false will be returned. //未定义返回false
  193. * @param string $name the property name or the event name
  194. * @return boolean whether the named property is set (not null).
  195. * @see http://php.net/manual/en/function.isset.php
  196. */
  197. public function __isset($name)
  198. {
  199. $getter = 'get' . $name;
  200. if (method_exists($this, $getter)) { //如果存在这个get.$name的方法,则返回ture
  201. return $this->$getter() !== null;
  202. } else { //否则返回false
  203. return false;
  204. }
  205. }
  206. /**
  207. * Sets an object property to null.
  208. * 设置一个属性为空
  209. *
  210. * Do not call this method directly as it is a PHP magic method that
  211. * will be implicitly called when executing `unset($object->property)`.
  212. *
  213. * Note that if the property is not defined, this method will do nothing.
  214. * If the property is read-only, it will throw an exception.
  215. * @param string $name the property name
  216. * @throws InvalidCallException if the property is read only.
  217. * @see http://php.net/manual/en/function.unset.php
  218. */
  219. public function __unset($name)
  220. {
  221. $setter = 'set' . $name;
  222. if (method_exists($this, $setter)) {
  223. $this->$setter(null);//如果存在,由$setter设置为null
  224. } elseif (method_exists($this, 'get' . $name)) { //如果是只读的,抛出异常
  225. throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
  226. }
  227. }
  228. /**
  229. * Calls the named method which is not a class method.
  230. * 调用指定的方法而不是一个类方法.
  231. *
  232. * Do not call this method directly as it is a PHP magic method that
  233. * will be implicitly called when an unknown method is being invoked.
  234. *
  235. * 该方法为PHP中的魔术方法,不要直接调用,当一个未知的方法被调用时,会触发这个函数
  236. *
  237. * @param string $name the method name
  238. * @param array $params method parameters
  239. * @throws UnknownMethodException when calling unknown method
  240. * @return mixed the method return value
  241. */
  242. public function __call($name, $params)
  243. {
  244. throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
  245. }
  246. /**
  247. * Returns a value indicating whether a property is defined.
  248. * A property is defined if:
  249. *
  250. * - the class has a getter or setter method associated with the specified name
  251. * (in this case, property name is case-insensitive);
  252. * - the class has a member variable with the specified name (when `$checkVars` is true);
  253. * 检查查对象或类是否具有 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter或setter
  254. *
  255. * @param string $name the property name
  256. * @param boolean $checkVars whether to treat member variables as properties 是否将成员变量作为属性对待 true/false
  257. * @return boolean whether the property is defined 属性是否定义
  258. * @see canGetProperty()
  259. * @see canSetProperty()
  260. */
  261. public function hasProperty($name, $checkVars = true)
  262. {
  263. return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
  264. }
  265. /**
  266. * Returns a value indicating whether a property can be read.
  267. * A property is readable if:
  268. * 返回一个值指示是否可以读取属性.
  269. *
  270. * - the class has a getter method associated with the specified name
  271. * (in this case, property name is case-insensitive);
  272. * - the class has a member variable with the specified name (when `$checkVars` is true);
  273. * 检查对象或类是否能够获取 $name 属性,如果 $checkVars 为 true,则不局限于是否有 getter
  274. *
  275. * @param string $name the property name
  276. * @param boolean $checkVars whether to treat member variables as properties
  277. * @return boolean whether the property can be read
  278. * @see canSetProperty()
  279. */
  280. public function canGetProperty($name, $checkVars = true)
  281. {
  282. //是否存在该属性或其get方法
  283. return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
  284. }
  285. /**
  286. * Returns a value indicating whether a property can be set.
  287. * 属性是否可设置
  288. *
  289. * A property is writable if:
  290. *
  291. * - the class has a setter method associated with the specified name
  292. * (in this case, property name is case-insensitive);
  293. * - the class has a member variable with the specified name (when `$checkVars` is true);
  294. * 检查对象或类是否能够设置 $name 属性,如果 $checkVars 为 true,则不局限于是否有 setter
  295. *
  296. * @param string $name the property name
  297. * @param boolean $checkVars whether to treat member variables as properties
  298. * @return boolean whether the property can be written
  299. * @see canGetProperty()
  300. */
  301. public function canSetProperty($name, $checkVars = true)
  302. {
  303. //是否存在该属性或其set方法
  304. return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
  305. }
  306. /**
  307. * Returns a value indicating whether a method is defined.
  308. * 方法是否定义
  309. *
  310. * The default implementation is a call to php function `method_exists()`.
  311. * You may override this method when you implemented the php magic method `__call()`.
  312. * @param string $name the method name
  313. * @return boolean whether the method is defined //是否具有 $name 方法
  314. */
  315. public function hasMethod($name)
  316. {
  317. return method_exists($this, $name);
  318. }
  319. }

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读

    相关 YII2 GirdView详解(二)

    其他都没变,URL 只显示「请求地址」(因为地址太长了不方便显示),但是同时能满足URL模糊搜索的需求。 <?= GridView::widget([

    相关 yii2学习笔记

    yii2 model 每个模型对应一张表,rules方法定义对应的字段格式,长度。attributeLabels设置对应字段名的默认显示名。 yii2 form 每个