haowubai 发表于 2012-7-9 08:23:27

CI是怎么加载类的

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('html');
}
public function index()
{
echo br(3);
}
}
如CI的人口文件是这样的,一个类继承了CI_Controller
在看看CI_Controller这个类的构造函数
public function __construct()
{
self::$instance =& $this;

// Assign all the class objects that were instantiated by the
// bootstrap file (CodeIgniter.php) to local class variables
// so that CI can run as one big super object.
foreach (is_loaded() as $var => $class)
{
   $this->$var =& load_class($class);
}
$this->load =& load_class('Loader', 'core');
$this->load->initialize();

log_message('debug', "Controller Class Initialized");
}
这个is_loaded()函数都没有任何文件加载,那么他怎么可以在CI_Controller中用的呢,请高手解释下,本人新手

浪迹天涯 发表于 2012-7-9 09:04:23

本帖最后由 浪迹天涯 于 2012-7-9 09:10 编辑

LZ显然是没仔细看看CI的代码,CI在执行CI_Controller类之前已经执行了CodeIgniter.php,而is_loaded()函数是在load_class函数中被调用的,再返回到CodeIgniter.php文件中看,该文件中有很多地方调用了load_class(),比如$BM =& load_class('Benchmark', 'core');    $EXT =& load_class('Hooks', 'core');    $CFG =& load_class('Config', 'core');    等等,CI_Controller中调用的is_load()怎么没加载任何文件呢?恰恰是加载了一些核心的类库。 要知道CI_Controller中调用的is_load()有没有加载文件或者它初始加载了哪些文件,其实很简单,你就在CI_Controller中的__construct函数里把is_load()的返回值打印出来不就一目了然吗?
至于CI是如何加载类的,看看load_class吧,然后再稍微认真的看下CI的代码

haowubai 发表于 2012-7-9 12:57:48

浪迹天涯 发表于 2012-7-9 09:04 static/image/common/back.gif
LZ显然是没仔细看看CI的代码,CI在执行CI_Controller类之前已经执行了CodeIgniter.php,而is_loaded()函数 ...

你说的对,我搞错了,我把控制器文件当入口文件看了,说以没留意index.php中已经加载了codeIgniter.php文件,一时浮夸了……
页: [1]
查看完整版本: CI是怎么加载类的