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

╰半夏微凉° 2022-07-16 13:44 237阅读 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. * Behavior is the base class for all behavior classes.
  10. * 所有行为的基类
  11. *
  12. * A behavior can be used to enhance the functionality of an existing component without modifying its code.
  13. * In particular, it can "inject" its own methods and properties into the component
  14. * and make them directly accessible via the component. It can also respond to the events triggered in the component
  15. * and thus intercept the normal code execution.
  16. * 用来增强现有组件的功能而不修改它的代码。它可以添加自己的方法和属性组件
  17. * 使他们可以直接通过组件访问。还可以响应组件中触发的事件,拦截正常的代码执行。
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. *
  22. * 继承父类Object
  23. */
  24. class Behavior extends Object
  25. {
  26. /**
  27. * @var Component the owner of this behavior
  28. * 行为组件的所有者,要附加行为对象的组件。
  29. */
  30. public $owner;
  31. /**
  32. * Declares event handlers for the [[owner]]'s events.
  33. * 声明[[owner]]的事件处理程序
  34. *
  35. * Child classes may override this method to declare what PHP callbacks should
  36. * be attached to the events of the [[owner]] component.
  37. * 子类可以重写此方法 php回调应连接 [[owner]]组件。
  38. *
  39. * The callbacks will be attached to the [[owner]]'s events when the behavior is
  40. * attached to the owner; and they will be detached from the events when
  41. * the behavior is detached from the component.
  42. * 当行为被连接到owner时回调将附在[[owner]]的事件中,当行为从组件中分离时,它们将被分离
  43. *
  44. * The callbacks can be any of the following:
  45. *
  46. * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
  47. * - object method: `[$object, 'handleClick']`
  48. * - static method: `['Page', 'handleClick']`
  49. * - anonymous function: `function ($event) { ... }`
  50. *
  51. * The following is an example:
  52. * 下面是一个例子
  53. *
  54. * ```php
  55. * [
  56. * Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
  57. * Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
  58. * ]
  59. * ```
  60. *
  61. * @return array events (array keys) and the corresponding event handler methods (array values).
  62. * 事件和相应的事件处理方法
  63. */
  64. public function events()
  65. {
  66. // Behavior 基类本身没用,主要是子类使用,重载这个函数返回一个数组表
  67. // 示行为所关联的事件
  68. //用于表示行为所有要响应的事件
  69. return [];
  70. }
  71. /**
  72. * Attaches the behavior object to the component. 绑定行为到组件
  73. * The default implementation will set the [[owner]] property
  74. * and attach event handlers as declared in [[events]].
  75. * 默认设置[[owner]]属性并将事件处理程序绑定到组件
  76. * Make sure you call the parent implementation if you override this method. 如果重写方法,确保调用父类去实现
  77. * @param Component $owner the component that this behavior is to be attached to. 行为绑定到$owner组件
  78. */
  79. public function attach($owner)
  80. {
  81. $this->owner = $owner;//设置 $owner ,使得所依附的对象可以访问、操作
  82. foreach ($this->events() as $event => $handler) {
  83. //将准备响应的事件,通过所依附类的 on()方法 绑定到类上
  84. $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
  85. }
  86. }
  87. /**
  88. * Detaches the behavior object from the component. 解除绑定的行为
  89. * The default implementation will unset the [[owner]] property 默认取消 owner的属性
  90. * and detach event handlers declared in [[events]]. 将events中的事件程序解除绑定
  91. * Make sure you call the parent implementation if you override this method. 如果重写方法,确保调用父类去实现
  92. */
  93. public function detach()
  94. {
  95. if ($this->owner) {
  96. foreach ($this->events() as $event => $handler) {
  97. //遍历行为中 events() 返回的数组
  98. //通过Component的 off() 将绑定到类上的事件解除
  99. $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
  100. }
  101. $this->owner = null;//将 $owner 设置为 null ,表示这个解除绑定
  102. }
  103. }
  104. }
  105. /*
  106. * 一个绑定了行为的类,表现起来是这样的:
  107. *
  108. */
  109. // Step 1: 定义一个将绑定行为的类
  110. // class MyClass extends yii\base\Component
  111. // {
  112. // 空的
  113. // }
  114. // Step 2: 定义一个行为类,他将绑定到MyClass上
  115. // class MyBehavior extends yii\base\Behavior
  116. //{
  117. // 行为的一个属性
  118. // public $property1 = 'This is property in MyBehavior.';
  119. // 行为的一个方法
  120. // public function method1()
  121. // {
  122. // return 'Method in MyBehavior is called.';
  123. // }
  124. // }
  125. //$myClass = new MyClass();
  126. //$myBehavior = new MyBehavior();
  127. // Step 3: 将行为绑定到类上
  128. //$myClass->attachBehavior('myBehavior', $myBehavior);
  129. // Step 4: 访问行为中的属性和方法,就和访问类自身的属性和方法一样
  130. //echo $myClass->property1;
  131. //echo $myClass->method1();

发表评论

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

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

相关阅读

    相关 YII2 GirdView详解(二)

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

    相关 yii2学习笔记

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