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

首頁 > CMS > Discuz > 正文

關于discuz X3.2中$discuz->var['mod']的理解

2024-09-11 09:03:24
字體:
來源:轉載
供稿:網友

在member.php中有這么一段代碼:

  1. $mod = !in_array($discuz->var['mod'], $modarray) && (!preg_match('/^/w+$/'$discuz->var['mod']) || !file_exists(DISCUZ_ROOT.'./source/module/member/member_'.$discuz->var['mod'].'.php')) ? 'register' : $discuz->var['mod']; 

從上面的紅色部分我們可以看出,這是一個3元運算,好了說重點:$discuz->var['mod']

首先從結構(->)上我們可以看出這是一個類的對象,但具體又是什么呢?我們來看$discuz,在member.php中有如下代碼:

  1. require './source/class/class_core.php';//引入class_core.php 
  2. $discuz = C::app();//調用類C中的app方法 

打開引入class_core.php文件,在該文件中有如下代碼:

  1. C::creatapp(); 
  2. class core{....} 
  3. class C extends core {} 

從上可知,類c繼承自core,所以C::creatapp()等價于core::creatapp(),現在來看core::creatapp()

  1. public static function creatapp() { 
  2. if(!is_object(self::$_app)) {//判斷$_app是不是對象 
  3. self::$_app = discuz_application::instance(); //如果不是對象,把類discuz_application中instance方法返回結果賦值給$_app; 
  4. //Vevb.com 
  5. return self::$_app;//返回$_app 

這里又出現了一個類discuz_application,這個類定義在./source/class/discuz/discuz_application.php文件中,這里并沒有引用該文件,那么這個類是如何實現調用的呢?這里就涉及到了類的另外一種加載方法:類自動加載,當類discuz_application不存在是就會調用下面的:

  1. if(function_exists('spl_autoload_register')) { 
  2. spl_autoload_register(array('core''autoload')); 
  3. else { 
  4. function __autoload($class) { 
  5. return core::autoload($class); 

這里最終會執行的是ore::autoload().

這里都會調用 core::autoload('discuz_application');下面進入到autoload方法:

  1. public static function autoload($class) { 
  2. //分析類名,獲取類所在的目錄 
  3. $class = strtolower($class); 
  4. if(strpos($class'_') !== false) { 
  5. list($folder) = explode('_'$class); 
  6. $file = 'class/'.$folder.'/'.substr($classstrlen($folder) + 1); 
  7.  
  8. else { 
  9. $file = 'class/'.$class
  10. //執行后$file='class/discuz/application' 
  11. try { 
  12. //調用import方法 
  13. self::import($file); 
  14. return true; 
  15.  
  16. } catch (Exception $exc) { 
  17.  
  18. $trace = $exc->getTrace(); 
  19. foreach ($trace as $log) { 
  20. if(emptyempty($log['class']) && $log['function'] == 'class_exists') { 
  21. return false; 
  22. discuz_error::exception_error($exc); 

下面該進入self::import($file);self表示類本身,所以就等價于:core::import($file),也就是core::import('class/discuz/application'):

  1. public static function import($name$folder = ''$force = true) { 
  2. //分析傳入的參數$file,獲取類所在的文件名及路徑 
  3. $key = $folder.$name
  4. if(!isset(self::$_imports[$key])) { 
  5. $path = DISCUZ_ROOT.'/source/'.$folder
  6. if(strpos($name'/') !== false) { 
  7. $pre = basename(dirname($name)); 
  8. $filename = dirname($name).'/'.$pre.'_'.basename($name).'.php'
  9. else { 
  10. $filename = $name.'.php'
  11. //確定文件名及路徑$filename=‘class/discuz/discuz_application.php’ 
  12. //引入文件 
  13. if(is_file($path.'/'.$filename)) { 
  14. include $path.'/'.$filename
  15. self::$_imports[$key] = true; 
  16.  
  17. return true; 
  18. elseif(!$force) { 
  19. return false; 
  20. else { 
  21. throw new Exception('Oops! System file lost: '.$filename); 
  22. return true; 

上面已近分析了discuz_application的自動加載,下面回到上面講到的self::$_app=discuz_application::instance()部分,現在已經知道這個類的位置,我們進入到/source/class/discuz/discuz_application.php,找到instance()方法:

  1. //這是個引用返回方法.
  2. static function &instance() {  
  3. static $object
  4. if(emptyempty($object)) { 
  5. $object = new self(); 
  6. return $object
  7. //通過這個方法將類discuz_application實例化,實例對象即:$object,那么self::$_app=discuz_application::instance()也就是相當于把對象賦給了self::$_app 

至此,class_core.php文件中的C::creatapp();也就執行完畢了,現在回到member.php中.

  1. require './source/class/class_core.php';//引入class_core.php 
  2. $discuz = C::app();//調用類C中的app方法 

我們再來看看class_core.php文件中的C::app();

  1. public static function app() { 
  2. return self::$_app

這個很簡單啦,就是返回其成員$_app,前面已近說了$_app就是類discuz_application的實例化.

ok,現在再來看$discuz = C::app(),這也就是說又將類discuz_application的實例化賦值給了$discuz,那么$discuz->var['mod']就很好理解了.

$discuz->var['mod']就表示類discuz_application的對象$discuz中的成員var['mod'];

我們再來看看類discuz_application中的var:

  1. global $_G
  2. $_G = array(....此處省略5km代碼) 
  3. //....此處再省略5km代碼 
  4. if(defined('SUB_DIR')) { 
  5. $_G['siteurl'] = str_replace(SUB_DIR, '/'$_G['siteurl']); 
  6. $_G['siteroot'] = str_replace(SUB_DIR, '/'$_G['siteroot']); 
  7.  
  8. $this->var = & $_G

現在明白了var實際上就是全局變量$_G,至于鍵值可以mod在$_G是沒有的,繼續往下看:

在方法_init_input()中有如下定義:

$this->var['mod'] = empty($_GET['mod']) ? '' : dhtmlspecialchars($_GET['mod']);

又是一個3元運算符,這下徹底明白了var['mod']表示的就是url中變量mod參數,自此總算搞清楚了,在構造函數中包含了這個函數,所以實例化時就已經執行了這個函數:

  1. public function __construct() { 
  2. $this->_init_env(); 
  3. $this->_init_config(); 
  4. $this->_init_input(); 
  5. $this->_init_output(); 

這里有一個方法:dhtmlspecialchars():

  1. function dhtmlspecialchars($string) { 
  2. if(is_array($string)) { 
  3. foreach($string as $key => $val) { 
  4. $string[$key] = dhtmlspecialchars($val); 
  5. else { 
  6. $string = str_replace(array('&''"''<''>'), array('&''"''<''>'), $string); 
  7. if(strpos($string, '

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁南县| 浏阳市| 齐河县| 民权县| 万全县| 昭通市| 从化市| 江城| 同心县| 渝中区| 神农架林区| 河北省| 清远市| 册亨县| 黄浦区| 宁夏| 通江县| 赤水市| 资溪县| 道真| 独山县| 鄂托克旗| 德令哈市| 柳江县| 阿坝县| 陇西县| 克山县| 绵竹市| 湟源县| 桦甸市| 新野县| 长乐市| 广德县| 洮南市| 比如县| 余庆县| 鄂伦春自治旗| 界首市| 西盟| 武强县| 巴马|