Behavior.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * A behavior can be used to enhance the functionality of an existing component without modifying its code.
  12. * In particular, it can "inject" its own methods and properties into the component
  13. * and make them directly accessible via the component. It can also respond to the events triggered in the component
  14. * and thus intercept the normal code execution.
  15. *
  16. * For more details and usage information on Behavior, see the [guide article on behaviors](guide:concept-behaviors).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class Behavior extends Object
  22. {
  23. /**
  24. * @var Component the owner of this behavior
  25. */
  26. public $owner;
  27. /**
  28. * Declares event handlers for the [[owner]]'s events.
  29. *
  30. * Child classes may override this method to declare what PHP callbacks should
  31. * be attached to the events of the [[owner]] component.
  32. *
  33. * The callbacks will be attached to the [[owner]]'s events when the behavior is
  34. * attached to the owner; and they will be detached from the events when
  35. * the behavior is detached from the component.
  36. *
  37. * The callbacks can be any of the following:
  38. *
  39. * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
  40. * - object method: `[$object, 'handleClick']`
  41. * - static method: `['Page', 'handleClick']`
  42. * - anonymous function: `function ($event) { ... }`
  43. *
  44. * The following is an example:
  45. *
  46. * ```php
  47. * [
  48. * Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
  49. * Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
  50. * ]
  51. * ```
  52. *
  53. * @return array events (array keys) and the corresponding event handler methods (array values).
  54. */
  55. public function events()
  56. {
  57. return [];
  58. }
  59. /**
  60. * Attaches the behavior object to the component.
  61. * The default implementation will set the [[owner]] property
  62. * and attach event handlers as declared in [[events]].
  63. * Make sure you call the parent implementation if you override this method.
  64. * @param Component $owner the component that this behavior is to be attached to.
  65. */
  66. public function attach($owner)
  67. {
  68. $this->owner = $owner;
  69. foreach ($this->events() as $event => $handler) {
  70. $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
  71. }
  72. }
  73. /**
  74. * Detaches the behavior object from the component.
  75. * The default implementation will unset the [[owner]] property
  76. * and detach event handlers declared in [[events]].
  77. * Make sure you call the parent implementation if you override this method.
  78. */
  79. public function detach()
  80. {
  81. if ($this->owner) {
  82. foreach ($this->events() as $event => $handler) {
  83. $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
  84. }
  85. $this->owner = null;
  86. }
  87. }
  88. }