亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 語言 > PHP > 正文

PHP的Yii框架中YiiBase入口類的擴展寫法示例

2024-05-04 23:44:10
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP的Yii框架中YiiBase入口類的擴展寫法示例,同時詳細講解了import和autoload這兩個YiiBase中的重要方法,需要的朋友可以參考下
 

通過yiic.php自動創建一個應用后,入口文件初始代碼如下:

<?php// change the following paths if necessary$yii=dirname(__FILE__).'/../yii/framework/yii.php';$config=dirname(__FILE__).'/protected/config/main.php';// remove the following lines when in production modedefined('YII_DEBUG') or define('YII_DEBUG',true);// specify how many levels of call stack should be shown in each log messagedefined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);require_once($yii);Yii::createWebApplication($config)->run();


其中第三行引入了一個yii.php的文件,這個可以在yii核心目錄里的framework/下找到,這個文件中定義了一個Yii類,并且繼承了YiiBase類。

代碼如下

require(dirname(__FILE__).'/YiiBase.php'); /** * Yii is a helper class serving common framework functionalities. * * It encapsulates {@link YiiBase} which provides the actual implementation. * By writing your own Yii class, you can customize some functionalities of YiiBase. * * @author Qiang Xue <qiang.xue@gmail.com> * @package system * @since 1.0 */class Yii extends YiiBase{}

Yii::createWebApplication

這個方法實際上是在YiiBase父類中定義的,所以,Yii為我們預留了擴展的可能。我們只需要在yii.php中添加我們想要擴展的方法即可,在項目中直接使用 Yii::方法名() 調用。 
為了將項目代碼和核心目錄完全分離,我個人覺得在項目目錄下使用另外一個yii.php來替代從核心目錄中包含yii.php更加好。

這里我用了更加極端的方法,我直接將yii這個類定義在了入口文件,并擴展了一個全局工廠函數 instance()方法,請看代碼:

<?php// change the following paths if necessary$yii=dirname(__FILE__).'/../yii/framework/YiiBase.php';$config=dirname(__FILE__).'/protected/config/main.php';// remove the following lines when in production modedefined('YII_DEBUG') or define('YII_DEBUG',true);// specify how many levels of call stack should be shown in each log messagedefined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);require_once($yii);//擴展基類class Yii extends YiiBase{  /**   * 全局擴展方法:工廠函數   * @param type $alias 類庫別名   */  static function instance($alias){    static $_class_ = array();    $key = md5($alias);    if(!isset($_class_[$key])){      $_class_[$key] = self::createComponent($alias);    }    return $_class_[$key];  }}Yii::createWebApplication($config)->run();


這個類是在最后一行Yii::createWebApplication()之前定義的,以保證Yii類能正常使用(不要把這個類放在文件末尾,會出錯。)

在項目中任何地方,使用$obj = Yii::instance($alias);去實例化一個類,并且是單例模式。

YiiBase中的兩個比較重要的方法 (import,autoload)
然后看看YiiBase中的import方法就知道這些靜態變量是干嘛用的了:

 /* Yii::import()* $alias: 要導入的類名或路徑* $forceInclude false:只導入不include類文件,true則導入并include類文件*/ public static function import($alias, $forceInclude = false){   //Yii把所有的依賴放入到這個全局的$_imports數組中,名字不能重復 //如果當前依賴已經被引入過了,那么直接返回 if (isset(self::$_imports[$alias])) {        return self::$_imports[$alias];    }   //class_exists和interface_exists方法的第二個參數的值為false表示不autoload  if (class_exists($alias, false) || interface_exists($alias, false)) {       return self::$_imports[$alias] = $alias;   }   //如果傳進來的是一個php5.3版本的命名空間格式的類(例如:/a/b/c.php) if (($pos = strrpos($alias, '//')) !== false) {      //$namespace = a.b  $namespace = str_replace('//', '.', ltrim(substr($alias, 0, $pos), '//'));   //判斷a.b這個路徑是否存在,或者a.b只是alias里面的一個鍵,調用該方法返回這個鍵對應的值,比如'email' => realpath(__DIR__ . '/../vendor/cornernote/yii-email-module/email')  if (($path = self::getPathOfAlias($namespace)) !== false) {       $classFile = $path . DIRECTORY_SEPARATOR . substr($alias, $pos + 1) . '.php';           if ($forceInclude) {             if (is_file($classFile)) {                 require($classFile);              } else {                throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.', array('{alias}' => $alias)));             }             self::$_imports[$alias] = $alias;           } else {             self::$classMap[$alias] = $classFile;          }          return $alias;      } else {      // try to autoload the class with an autoloader        if (class_exists($alias, true)) {            return self::$_imports[$alias] = $alias;        } else {            throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',          array('{alias}' => $namespace)));        }      }   }  if (($pos = strrpos($alias, '.')) === false) // a simple class name  {      // try to autoload the class with an autoloader if $forceInclude is true      if ($forceInclude && (Yii::autoload($alias, true) || class_exists($alias, true))) {         self::$_imports[$alias] = $alias;       }      return $alias;   }   $className = (string)substr($alias, $pos + 1);   $isClass = $className !== '*';   if ($isClass && (class_exists($className, false) || interface_exists($className, false))) {      return self::$_imports[$alias] = $className;   }   if (($path = self::getPathOfAlias($alias)) !== false) {       if ($isClass) {            if ($forceInclude) {                 if (is_file($path . '.php')) {                       require($path . '.php');                 } else {                       throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.', array('{alias}' => $alias)));                     }                self::$_imports[$alias] = $className;           } else {                self::$classMap[$className] = $path . '.php';           }            return $className;        }     // $alias是'system.web.*'這樣的已*結尾的路徑,將路徑加到include_path中    else // a directory         {             if (self::$_includePaths === null) {              self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path()));             if (($pos = array_search('.', self::$_includePaths, true)) !== false) {                  unset(self::$_includePaths[$pos]);              }          }          array_unshift(self::$_includePaths, $path);          if (self::$enableIncludePath && set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)) === false) {             self::$enableIncludePath = false;           }           return self::$_imports[$alias] = $path;        }    } else {        throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',      array('{alias}' => $alias)));      } }

是的,上面這個方法最后就把要加載的東西都放到$_imports,$_includePaths中去了。這就是Yii的import方法,好的,接下來我們看看autoload方法:

public static function autoload($className, $classMapOnly = false){  // use include so that the error PHP file may appear  if (isset(self::$classMap[$className])) {         include(self::$classMap[$className]);  } elseif (isset(self::$_coreClasses[$className])) {      include(YII_PATH . self::$_coreClasses[$className]);  } elseif ($classMapOnly) {      return false;  } else {     // include class file relying on include_path        if (strpos($className, '//') === false)     // class without namespace        {            if (self::$enableIncludePath === false) {                 foreach (self::$_includePaths as $path) {                          $classFile = $path . DIRECTORY_SEPARATOR . $className . '.php';                      if (is_file($classFile)) {                      include($classFile);                          if (YII_DEBUG && basename(realpath($classFile)) !== $className . '.php') {                              throw new CException(Yii::t('yii', 'Class name "{class}" does not match class file "{file}".', array(                '{class}' => $className,                '{file}' => $classFile,              )));                          }                          break;                     }               }         } else {              include($className . '.php');             }     } else // class name with namespace in PHP 5.3       {           $namespace = str_replace('//', '.', ltrim($className, '//'));         if (($path = self::getPathOfAlias($namespace)) !== false) {        include($path . '.php');           } else {              return false;           }       }    

   return class_exists($className, false) || interface_exists($className, false);    }    return true;}
config文件中的 import 項里的類或路徑在腳本啟動中會被自動導入。用戶應用里個別類需要引入的類可以在類定義前加入 Yii::import() 語句。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
狠狠色狠狠色综合日日五| 97精品国产97久久久久久免费| 亚洲国产精品推荐| 欧美日韩一区二区精品| 久久av在线播放| 国产区精品视频| 亚洲伊人一本大道中文字幕| 久久久精品日本| 97av在线影院| 色999日韩欧美国产| 26uuu久久噜噜噜噜| 亚洲精品久久久久久下一站| 国产在线久久久| 激情成人中文字幕| 午夜美女久久久久爽久久| 一区二区欧美在线| 亚洲第一福利在线观看| 亚洲天堂网在线观看| 亚洲天堂网在线观看| 欧美日韩国内自拍| 日韩电影在线观看永久视频免费网站| 91av在线播放视频| 成人啪啪免费看| 亚洲精品短视频| 亚洲欧洲av一区二区| 日韩久久午夜影院| 亚洲色图日韩av| 国产精品爱久久久久久久| 亚洲欧美国产精品va在线观看| 中文字幕av日韩| 欧美精品999| 亚洲国产精品视频在线观看| 麻豆国产精品va在线观看不卡| 国产精自产拍久久久久久蜜| 2019亚洲男人天堂| 88国产精品欧美一区二区三区| 91在线免费看网站| 国产成人一区二区在线| 国产精品av在线| 亚洲护士老师的毛茸茸最新章节| 日韩成人在线视频观看| 亚洲欧美色婷婷| 亚洲成人激情视频| 欧美一区二区三区艳史| 欧美精品videos| 国产精品夜间视频香蕉| 日韩在线观看高清| xvideos亚洲人网站| 精品中文视频在线| 欧美一区二区.| 久久精品久久久久久国产 免费| 欧洲永久精品大片ww免费漫画| 亚洲最新在线视频| 亚洲成人精品av| 成人美女av在线直播| 国产精品美女免费看| 亚洲欧美在线x视频| 国产精品美女免费视频| 国内精品伊人久久| 国产免费一区视频观看免费| 欧美日韩一区二区在线播放| 欧美乱大交xxxxx另类电影| 国产精品免费网站| 欧美日韩一二三四五区| 久久久久久久久久久av| 欧美性感美女h网站在线观看免费| 亚洲第一精品夜夜躁人人躁| 一区二区三区高清国产| 91亚洲精品久久久| 欧美激情久久久| 亚洲成人在线视频播放| 欧美激情国内偷拍| 3344国产精品免费看| 美女av一区二区| 日韩在线观看免费高清完整版| 久久婷婷国产麻豆91天堂| 久久国产加勒比精品无码| 国产精品99久久99久久久二8| 亚洲最新av网址| 亚洲另类激情图| 国产亚洲精品久久久久动| 欧美激情视频三区| 国产一区二区免费| 国产精品入口日韩视频大尺度| 91香蕉嫩草神马影院在线观看| 国产精品福利片| 热re99久久精品国产66热| 国产成人aa精品一区在线播放| 久久人人看视频| 欧美国产日产韩国视频| 国产精品一久久香蕉国产线看观看| 精品成人久久av| 韩国19禁主播vip福利视频| 久久精品视频在线| 97国产精品视频人人做人人爱| 欧美性视频网站| 国内精品久久影院| 奇米成人av国产一区二区三区| 久热99视频在线观看| 亚洲第一页中文字幕| 午夜精品久久久久久久久久久久| 91国自产精品中文字幕亚洲| 91亚洲人电影| 欧美激情va永久在线播放| 国产日韩欧美视频| 26uuu日韩精品一区二区| 91精品国产综合久久久久久蜜臀| xxxxxxxxx欧美| 亚洲精品中文字| 精品久久久久久久久久久| 国内精品中文字幕| 欧美野外猛男的大粗鳮| 91在线直播亚洲| 精品国产一区二区三区久久| 最近2019好看的中文字幕免费| 亚洲成色999久久网站| 久久久久久久影视| 国产91露脸中文字幕在线| 欧美极品美女视频网站在线观看免费| 欧美成人第一页| 国产精品自产拍高潮在线观看| 欧美高清视频在线播放| 狠狠久久亚洲欧美专区| www.日韩.com| 欧美激情第6页| 久久精品国产成人精品| 国产一区视频在线| 亚洲人成在线一二| 久久中文字幕视频| 国产亚洲精品高潮| 国产在线观看不卡| 欧美激情在线狂野欧美精品| 国产在线拍揄自揄视频不卡99| 亚洲性日韩精品一区二区| 久久久噜噜噜久噜久久| 黑丝美女久久久| 3344国产精品免费看| 日韩av综合中文字幕| 久久在线精品视频| 日韩视频在线免费观看| 国内精品久久久久影院优| 亚洲人成伊人成综合网久久久| 亚洲人成在线观| 国产高清在线不卡| 久久99精品久久久久久琪琪| 琪琪亚洲精品午夜在线| 欧美日韩黄色大片| 一道本无吗dⅴd在线播放一区| 中文字幕欧美日韩精品| 国产精品久久久久aaaa九色| 久久99热精品这里久久精品| 午夜精品一区二区三区在线播放| 国产一区二区三区精品久久久| 亚洲免费影视第一页| 亚洲a级在线播放观看| 91国产视频在线| 久久夜精品va视频免费观看| 91高清视频免费观看| 91国内揄拍国内精品对白| 成人免费高清完整版在线观看| 精品少妇一区二区30p| 青青草成人在线| 久久精品国产亚洲精品2020| 91久久国产综合久久91精品网站|