国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 21:50:53
字體:
供稿:網(wǎng)友

通過yiic.php自動創(chuàng)建一個應(yīng)用后,入口文件初始代碼如下:

  1. // change the following paths if necessary 
  2.  
  3. $yii=dirname(__FILE__).'/../yii/framework/yii.php'
  4.  
  5. $config=dirname(__FILE__).'/protected/config/main.php'
  6.  
  7. // remove the following lines when in production mode 
  8.  
  9. defined('YII_DEBUG'or define('YII_DEBUG',true); 
  10.  
  11. // specify how many levels of call stack should be shown in each log message 
  12.  
  13. defined('YII_TRACE_LEVEL'or define('YII_TRACE_LEVEL',3); 
  14.  
  15. require_once($yii); 
  16.  
  17. Yii::createWebApplication($config)->run(); 

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

代碼如下:

  1. require(dirname(__FILE__).'/YiiBase.php'); 
  2.  
  3. /** 
  4.  
  5.  * Yii is a helper class serving common framework functionalities. 
  6.  
  7.  * 
  8.  
  9.  * It encapsulates {@link YiiBase} which provides the actual implementation. 
  10.  
  11.  * By writing your own Yii class, you can customize some functionalities of YiiBase. 
  12.  
  13.  * 
  14.  
  15.  * @author Qiang Xue <qiang.xue@gmail.com> 
  16.  
  17.  * @package system 
  18.  
  19.  * @since 1.0 
  20.  
  21.  */ 
  22.  
  23. class Yii extends YiiBase 
  24.  
  25.  
  26.  
  27. </qiang.xue@gmail.com> 

而:

Yii::createWebApplication

這個方法實際上是在YiiBase父類中定義的,所以,Yii為我們預(yù)留了擴(kuò)展的可能。我們只需要在yii.php中添加我們想要擴(kuò)展的方法即可,在項目中直接使用 Yii::方法名() 調(diào)用。

為了將項目代碼和核心目錄完全分離,我個人覺得在項目目錄下使用另外一個yii.php來替代從核心目錄中包含yii.php更加好。

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

  1. // change the following paths if necessary 
  2.  
  3. $yii=dirname(__FILE__).'/../yii/framework/YiiBase.php'
  4.  
  5. $config=dirname(__FILE__).'/protected/config/main.php'
  6.  
  7. // remove the following lines when in production mode 
  8.  
  9. defined('YII_DEBUG'or define('YII_DEBUG',true); 
  10.  
  11. // specify how many levels of call stack should be shown in each log message 
  12.  
  13. defined('YII_TRACE_LEVEL'or define('YII_TRACE_LEVEL',3); 
  14.  
  15. require_once($yii); 
  16.  
  17. //擴(kuò)展基類 
  18.  
  19. class Yii extends YiiBase{ 
  20.  
  21.   /** 
  22.  
  23.    * 全局?jǐn)U展方法:工廠函數(shù) 
  24.  
  25.    * @param type $alias 類庫別名 
  26.  
  27.    */ 
  28.  
  29.   static function instance($alias){ 
  30.  
  31.     static $_class_ = array(); 
  32.  
  33.     $key = md5($alias); 
  34.  
  35.     if(!isset($_class_[$key])){ 
  36.  
  37.       $_class_[$key] = self::createComponent($alias); 
  38.  
  39.     } 
  40.  
  41.     return $_class_[$key]; 
  42.  
  43.   } 
  44.  
  45.  
  46. Yii::createWebApplication($config)->run(); 

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

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

YiiBase中的兩個比較重要的方法 (import,autoload)

然后看看YiiBase中的import方法就知道這些靜態(tài)變量是干嘛用的了:

  1. /* Yii::import() 
  2.  
  3. * $alias: 要導(dǎo)入的類名或路徑 
  4.  
  5. * $forceInclude false:只導(dǎo)入不include類文件,true則導(dǎo)入并include類文件 
  6.  
  7. */ 
  8.  
  9.  public static function import($alias$forceInclude = false){   
  10.  
  11.  //Yii把所有的依賴放入到這個全局的$_imports數(shù)組中,名字不能重復(fù) 
  12.  
  13.  //如果當(dāng)前依賴已經(jīng)被引入過了,那么直接返回 
  14.  
  15.  if (isset(self::$_imports[$alias])) {     
  16.  
  17.     return self::$_imports[$alias];   
  18.  
  19.   }   
  20.  
  21.  //class_exists和interface_exists方法的第二個參數(shù)的值為false表示不autoload  
  22.  
  23.  if (class_exists($alias, false) || interface_exists($alias, false)) {     
  24.  
  25.    return self::$_imports[$alias] = $alias;   
  26.  
  27.  }   
  28.  
  29.  //如果傳進(jìn)來的是一個php5.3版本的命名空間格式的類(例如:a/b/c.php) 
  30.  
  31.  if (($pos = strrpos($alias'//')) !== false) {     
  32.  
  33.   //$namespace = a.b 
  34.  
  35.   $namespace = str_replace('//', '.', ltrim(substr($alias, 0, $pos), '//'));  
  36.  
  37.   //判斷a.b這個路徑是否存在,或者a.b只是alias里面的一個鍵,調(diào)用該方法返回這個鍵對應(yīng)的值,比如'email' => realpath(__DIR__ . '/../vendor/cornernote/yii-email-module/email') 
  38.  
  39.   if (($path = self::getPathOfAlias($namespace)) !== false) {    
  40.  
  41.     $classFile = $path . DIRECTORY_SEPARATOR . substr($alias$pos + 1) . '.php';        
  42.  
  43.     if ($forceInclude) {         
  44.  
  45.      if (is_file($classFile)) {           
  46.  
  47.        require($classFile);         
  48.  
  49.       } else {           
  50.  
  51.       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)));         
  52.  
  53.      }         
  54.  
  55.      self::$_imports[$alias] = $alias;       
  56.  
  57.      } else {         
  58.  
  59.      self::$classMap[$alias] = $classFile;       
  60.  
  61.     }       
  62.  
  63.     return $alias;     
  64.  
  65.   } else {       
  66.  
  67. // try to autoload the class with an autoloader       
  68.  
  69.   if (class_exists($alias, true)) {         
  70.  
  71.     return self::$_imports[$alias] = $alias;       
  72.  
  73.   } else {         
  74.  
  75.     throw new CException(Yii::t('yii''Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',          array('{alias}' => $namespace)));       
  76.  
  77.   }     
  78.  
  79.   }   
  80.  
  81.  }   
  82.  
  83. if (($pos = strrpos($alias'.')) === false) // a simple class name  
  84.  
  85.  {     
  86.  
  87.   // try to autoload the class with an autoloader if $forceInclude is true     
  88.  
  89.   if ($forceInclude && (Yii::autoload($alias, true) || class_exists($alias, true))) {       
  90.  
  91.    self::$_imports[$alias] = $alias;     
  92.  
  93.    }     
  94.  
  95.   return $alias;   
  96.  
  97.  }   
  98.  
  99.  $className = (string)substr($alias$pos + 1);   
  100.  
  101.  
  102.  
  103.  $isClass = $className !== '*';   
  104.  
  105.  if ($isClass && (class_exists($className, false) || interface_exists($className, false))) {     
  106.  
  107.   return self::$_imports[$alias] = $className;   
  108.  
  109.  }   
  110.  
  111.  if (($path = self::getPathOfAlias($alias)) !== false) {     
  112.  
  113.    if ($isClass) {       
  114.  
  115.       if ($forceInclude) {         
  116.  
  117.          if (is_file($path . '.php')) {           
  118.  
  119.              require($path . '.php');         
  120.  
  121.          } else {           
  122.  
  123.              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)));         
  124.  
  125.              }         
  126.  
  127.         self::$_imports[$alias] = $className;       
  128.  
  129.      } else {         
  130.  
  131.         self::$classMap[$className] = $path . '.php';       
  132.  
  133.      }       
  134.  
  135.       return $className;     
  136.  
  137.     }  
  138.  
  139.     // $alias是'system.web.*'這樣的已*結(jié)尾的路徑,將路徑加到include_path中 
  140.  
  141.     else // a directory     
  142.  
  143.      {       
  144.  
  145.        if (self::$_includePaths === null) {     
  146.  
  147.           self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path()));   
  148.  
  149.            if (($pos = array_search('.', self::$_includePaths, true)) !== false) {           
  150.  
  151.         unset(self::$_includePaths[$pos]);         
  152.  
  153.       }       
  154.  
  155.     }       
  156.  
  157.     array_unshift(self::$_includePaths$path);       
  158.  
  159.     if (self::$enableIncludePath && set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)) === false) {         
  160.  
  161.      self::$enableIncludePath = false;       
  162.  
  163.      }       
  164.  
  165.      return self::$_imports[$alias] = $path;     
  166. //Vevb.com 
  167.     }   
  168.  
  169.   } else {     
  170.  
  171.     throw new CException(Yii::t('yii''Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',      array('{alias}' => $alias)));   
  172.  
  173.     } 
  174.  
  175.  } 

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

  1. public static function autoload($className$classMapOnly = false){  // use include so that the error PHP file may appear   
  2.  
  3. if (isset(self::$classMap[$className])) {        
  4.  
  5.   include(self::$classMap[$className]);   
  6.  
  7. elseif (isset(self::$_coreClasses[$className])) {     
  8.  
  9.   include(YII_PATH . self::$_coreClasses[$className]);   
  10.  
  11. elseif ($classMapOnly) {     
  12.  
  13.   return false;   
  14.  
  15. else {     
  16.  
  17.  // include class file relying on include_path     
  18.  
  19.     if (strpos($className'//') === false)  
  20.  
  21.     // class without namespace     
  22.  
  23.     {       
  24.  
  25.       if (self::$enableIncludePath === false) {         
  26.  
  27.          foreach (self::$_includePaths as $path) {               
  28.  
  29.             $classFile = $path . DIRECTORY_SEPARATOR . $className . '.php';           
  30.  
  31.             if (is_file($classFile)) {        
  32.  
  33.                include($classFile);             
  34.  
  35.               if (YII_DEBUG && basename(realpath($classFile)) !== $className . '.php') {               
  36.  
  37.                 throw new CException(Yii::t('yii''Class name "{class}" does not match class file "{file}".'array(                '{class}' => $className,                '{file}' => $classFile,              )));             
  38.  
  39.               }             
  40.  
  41.               break;           
  42.  
  43.            }         
  44.  
  45.        }       
  46.  
  47.    } else {         
  48.  
  49.       include($className . '.php');       
  50.  
  51.        }     
  52.  
  53.  } else // class name with namespace in PHP 5.3     
  54.  
  55.    {       
  56.  
  57.      $namespace = str_replace('//', '.', ltrim($className, '//'));     
  58.  
  59.      if (($path = self::getPathOfAlias($namespace)) !== false) {   
  60.  
  61.       include($path . '.php');       
  62.  
  63.      } else {         
  64.  
  65.       return false;       
  66.  
  67.      }     
  68.  
  69.    }     
  70.  
  71. return class_exists($className, false) || interface_exists($className, false); } return true;} 

config文件中的 import 項里的類或路徑在腳本啟動中會被自動導(dǎo)入。用戶應(yīng)用里個別類需要引入的類可以在類定義前加入 Yii::import() 語句。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巩留县| 会同县| 甘泉县| 本溪市| 登封市| 丰台区| 六盘水市| 临猗县| 沂水县| 封开县| 伊通| 洪江市| 安吉县| 原阳县| 陕西省| 肃南| 雷州市| 平舆县| 襄汾县| 东兰县| 漳浦县| 伊宁市| 德庆县| 思茅市| 周宁县| 塘沽区| 城固县| 博白县| 嘉义县| 神木县| 乐至县| 苗栗县| 开封县| 黄山市| 蒙城县| 英吉沙县| 嵊泗县| 固始县| 封丘县| 吴桥县| 汝城县|