123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace yii\base;
- class Behavior extends BaseObject
- {
-
- public $owner;
-
- private $_attachedEvents = [];
-
- public function events()
- {
- return [];
- }
-
- public function attach($owner)
- {
- $this->owner = $owner;
- foreach ($this->events() as $event => $handler) {
- $this->_attachedEvents[$event] = $handler;
- $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
- }
- }
-
- public function detach()
- {
- if ($this->owner) {
- foreach ($this->_attachedEvents as $event => $handler) {
- $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
- }
- $this->_attachedEvents = [];
- $this->owner = null;
- }
- }
- }
|