/source/class/discuz/discuz_application.php(X2.5)
/source/class/class_core.php(X2.0)
在discuz_application.php的_init_env()方法中定义了$_G,里面存放的是一些系统最频繁用到的相关的全局信息,主要是为了后面对各功能实现的便利性。
例如:
我要得知当前用户的UID,那我直接读取$_G['uid']就行了,而不用再二次查询session或者数据库,这样可以提高效率,编写其余的各模块程序,代码量也会变得非常简洁。
【私人例子:
\source\module\course\
edit.php:
$pathHead = $_G["setting"]["attachurl"]."course/title/";
$query = DB :: query("SELECT grouptitle, groupid FROM {$_G['config'][db][1][tablepre]}common_usergroup WHERE type = 'Member' AND creditshigher >= 0");
】
定义:
/source/class/discuz/
discuz_application.php:
class discuz_application extends discuz_base
{
private function _init_env()
{
(省略一大段)
global $_G; //为了声明这是全局变量
$_G = array(
'uid' => 0,
'username' => '',
'adminid' => 0,
'groupid' => 1,
'sid' => '',
'formhash' => '',
'connectguest' => 0,
'timestamp' => TIMESTAMP,
'starttime' => microtime(true),
'clientip' => $this->_get_client_ip(),
'referer' => '',
'charset' => '',
'gzipcompress' => '',
'authkey' => '',
'timenow' => array(),
'widthauto' => 0,
'disabledwidthauto' => 0,
'PHP_SELF' => '',
'siteurl' => '',
'siteroot' => '',
'siteport' => '',
'config' => array(),
'setting' => array(),
'member' => array(),
'group' => array(),
'cookie' => array(),
'style' => array(),
'cache' => array(),
'session' => array(),
'lang' => array(),
'my_app' => array(),
'my_userapp' => array(),
'fid' => 0,
'tid' => 0,
'forum' => array(),
'thread' => array(),
'rssauth' => '',
'home' => array(),
'space' => array(),
'block' => array(),
'article' => array(),
'action' => array(
'action' => APPTYPEID,
'fid' => 0,
'tid' => 0,
),
'mobile' => '',
);
$_G['PHP_SELF'] = dhtmlspecialchars($this->_get_script_url());
$_G['basescript'] = CURSCRIPT;
$_G['basefilename'] = basename($_G['PHP_SELF']);
$sitepath = substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'));
if(defined('IN_API')) {
$sitepath = preg_replace("/\/api\/?.*?$/i", '', $sitepath);
} elseif(defined('IN_ARCHIVER')) {
$sitepath = preg_replace("/\/archiver/i", '', $sitepath);
}
$_G['siteurl'] = dhtmlspecialchars('http://'.$_SERVER['HTTP_HOST'].$sitepath.'/');
$url = parse_url($_G['siteurl']);
$_G['siteroot'] = isset($url['path']) ? $url['path'] : '';
$_G['siteport'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
if(defined('SUB_DIR')) {
$_G['siteurl'] = str_replace(SUB_DIR, '/', $_G['siteurl']);
$_G['siteroot'] = str_replace(SUB_DIR, '/', $_G['siteroot']);
}
$this->var = & $_G;
}
}
$pathHead = $_G["setting"]["attachurl"]."course/title/";
$query = DB :: query("SELECT grouptitle, groupid FROM {$_G['config'][db][1][tablepre]}common_usergroup WHERE type = 'Member' AND creditshigher >= 0");
】
定义:
/source/class/discuz/
discuz_application.php:
class discuz_application extends discuz_base
{
private function _init_env()
{
(省略一大段)
global $_G; //为了声明这是全局变量
$_G = array(
'uid' => 0,
'username' => '',
'adminid' => 0,
'groupid' => 1,
'sid' => '',
'formhash' => '',
'connectguest' => 0,
'timestamp' => TIMESTAMP,
'starttime' => microtime(true),
'clientip' => $this->_get_client_ip(),
'referer' => '',
'charset' => '',
'gzipcompress' => '',
'authkey' => '',
'timenow' => array(),
'widthauto' => 0,
'disabledwidthauto' => 0,
'PHP_SELF' => '',
'siteurl' => '',
'siteroot' => '',
'siteport' => '',
'config' => array(),
'setting' => array(),
'member' => array(),
'group' => array(),
'cookie' => array(),
'style' => array(),
'cache' => array(),
'session' => array(),
'lang' => array(),
'my_app' => array(),
'my_userapp' => array(),
'fid' => 0,
'tid' => 0,
'forum' => array(),
'thread' => array(),
'rssauth' => '',
'home' => array(),
'space' => array(),
'block' => array(),
'article' => array(),
'action' => array(
'action' => APPTYPEID,
'fid' => 0,
'tid' => 0,
),
'mobile' => '',
);
$_G['PHP_SELF'] = dhtmlspecialchars($this->_get_script_url());
$_G['basescript'] = CURSCRIPT;
$_G['basefilename'] = basename($_G['PHP_SELF']);
$sitepath = substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'));
if(defined('IN_API')) {
$sitepath = preg_replace("/\/api\/?.*?$/i", '', $sitepath);
} elseif(defined('IN_ARCHIVER')) {
$sitepath = preg_replace("/\/archiver/i", '', $sitepath);
}
$_G['siteurl'] = dhtmlspecialchars('http://'.$_SERVER['HTTP_HOST'].$sitepath.'/');
$url = parse_url($_G['siteurl']);
$_G['siteroot'] = isset($url['path']) ? $url['path'] : '';
$_G['siteport'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
if(defined('SUB_DIR')) {
$_G['siteurl'] = str_replace(SUB_DIR, '/', $_G['siteurl']);
$_G['siteroot'] = str_replace(SUB_DIR, '/', $_G['siteroot']);
}
$this->var = & $_G;
}
}
眼神很亮地看到了$this->var = & $_G;
【私人例子:
/
course.php:
$discuz = & discuz_core::instance(); /* & discuz_core:: ??????????*/
$mod = !in_array($discuz->var['mod'], $modarray) ? 'list' : $discuz->var['mod'];
看不懂~~于是下文:
/source/class/discuz/
discuz_application.php:
class discuz_application extends discuz_base
{
static function &instance()
{
static $object;
if(empty($object))
{
$object = new self();
}
return $object;
} 函数&instance创建了一个静态变量(变量??),若他为空则new self()。应该是new了一个class discuz_application的对象。
暂时理解为。。当前实例句柄。。。然后去找了forum.php,home.php。。。没有发现。
应该是为了统一管理像mod这样的变量吧(猜测。mod是为了存储当前的mod(edit,list等等。))
回到$this->var = & $_G;
则$discuz->var['mod']等价于$_G['mod']
于是又牵扯到了$_G['mod'] (烦死了有木有~!!!)
在discuz_application.php中搜索$_G['mod']。
/source/class/discuz/
discuz_application.php:
class discuz_application extends discuz_base
{
public function init()
{
if(!$this->initated)
{
$this->_init_db();
$this->_init_setting();
$this->_init_user();
$this->_init_session();
$this->_init_mobile();
$this->_init_cron();
$this->_init_misc();
}
$this->initated = true;
}
public function __construct()
{
$this->_init_env();
$this->_init_config();
$this->_init_input();
$this->_init_output();
}
private function _init_input()
{
$this->var['mod'] = empty($_GET['mod']) ? '' : dhtmlspecialchars($_GET['mod']);
}
}
推测,当new一个class discuz_application对象时。调用__constract()。就会创建一个$_G['mod']。。可能为空,可能有值。而当每次页面跳转打到course.php。都会对mod重新赋值。
于是再看course.php:
/
course.php:
define('CURMODULE', $mod);
$discuz->init();
看到define。。应该就是他了。'CURMODULE'是什么???再说吧。。。
discuz_application.php中也没有找到 $this->_init_setting();???再说吧。。。
】