日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

yii2阅读随笔14

發(fā)布時間:2023/12/1 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yii2阅读随笔14 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

繼續(xù)來看Event.php

/*** Triggers a class-level event.* 觸發(fā)類級別事件。* This method will cause invocation of event handlers that are attached to the named event* for the specified class and all its parent classes.* 觸發(fā)某個類或者對象的某個事件* @param string|object $class the object or the fully qualified class name specifying the class-level event.* @param string $name the event name.* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.*/public static function trigger($class, $name, $event = null){if (empty(self::$_events[$name])) {return;}if ($event === null) {// 事件不存在,就創(chuàng)建一個 Event 對象$event = new static;}// 設(shè)置event對象的屬性,默認是未被處理的$event->handled = false;$event->name = $name;if (is_object($class)) {if ($event->sender === null) {// 如果 $class 是個對象,并且是 sender 為空,就將 $class 賦給 sender,即 $class 就是觸發(fā)事件的對象$event->sender = $class;}$class = get_class($class);} else {$class = ltrim($class, '\\');}// 循環(huán)類的 $_event,直到遇到 $event->handled 為真或者沒有父類了為止do {if (!empty(self::$_events[$name][$class])) {foreach (self::$_events[$name][$class] as $handler) {// 將參數(shù)賦到 event 對象的 data 屬性上$event->data = $handler[1];// 調(diào)用 $handler 方法// 在方法中,可以用 $this->data 取到相應(yīng)的參數(shù)// 也可以在其中設(shè)置 $this->handled 的值,中斷后續(xù)事件的觸發(fā)call_user_func($handler[0], $event);// 當某個 handled 被設(shè)置為 true 時,執(zhí)行到這個事件的時候,會停止,并忽略剩下的事件if ($event->handled) {return;}}}} while (($class = get_parent_class($class)) !== false);} }Component.php少分析了幾個方法,現(xiàn)在添加上去!/*** Detaches an existing event handler from this component.* 在該組件中,將現(xiàn)有的事件處理,* This method is the opposite of [[on()]].* 這種方法與on[]方法相反。* @param string $name event name* @param callable $handler the event handler to be removed.* If it is null, all handlers attached to the named event will be removed.* @return boolean if a handler is found and detached* @see on()*/public function off($name, $handler = null){// 保證 behaviors 都加載進來了$this->ensureBehaviors();// 相應(yīng)的事件不存在,就返回falseif (empty($this->_events[$name])) {return false;}// 沒有handler,就意味著要全部去掉if ($handler === null) {unset($this->_events[$name]);return true;} else {$removed = false;//如果$handler不為空。foreach ($this->_events[$name] as $i => $event) {// $event[0]是handler,$event[1]是數(shù)據(jù)if ($event[0] === $handler) {unset($this->_events[$name][$i]);$removed = true;}}if ($removed) {// 如果有移出事件的handler,就需要重新構(gòu)建以下索引,否則會出現(xiàn)index為1,3,4的情況$this->_events[$name] = array_values($this->_events[$name]);}return $removed;}}/*** Triggers an event.* 觸發(fā)一個事件。* This method represents the happening of an event. It invokes* all attached handlers for the event including class-level handlers.* 這種方法代表事件的發(fā)生。它調(diào)用的事件包括類級別的處理程序所包含的所有附加處理程序。* @param string $name the event name* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.*/public function trigger($name, Event $event = null){// 保證 behaviors 都加載進來了$this->ensureBehaviors();if (!empty($this->_events[$name])) {// 構(gòu)建Event對象,為傳入到handler函數(shù)中做準備if ($event === null) {$event = new Event;}if ($event->sender === null) { //如果$event->sender值為null的話,把$this賦值給它。$event->sender = $this;}$event->handled = false; //使handled值為空$event->name = $name;foreach ($this->_events[$name] as $handler) {// 給event的data屬性賦值$event->data = $handler[1];// handler的函數(shù)中傳入了一個Event對象call_user_func($handler[0], $event);// stop further handling if the event is handled// 事件是否被handle,當handled被設(shè)置為true時,執(zhí)行到這個event的時候,會停止,并忽略剩下的eventif ($event->handled) {return;}}}// invoke class-level attached handlers// 觸發(fā)class級別的handlerEvent::trigger($this, $name, $event);}

?

轉(zhuǎn)載于:https://www.cnblogs.com/taokai/p/5427660.html

總結(jié)

以上是生活随笔為你收集整理的yii2阅读随笔14的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。