top-think / thinkphp

ThinkPHP3.2 ——基于PHP5的简单快速的面向对象的PHP框架
http://www.thinkphp.cn
2.89k stars 1.5k forks source link

系统函数是如何自动加载的? #270

Open kaiyulee opened 9 years ago

kaiyulee commented 9 years ago

thinkphp/ThinkPHP/Mode/Api/functions.php,这里面的方法,看代码没看出来,求指点。

pysnow530 commented 9 years ago

首先,Api是一个mode,只有在定义mode是api时,Api/functions才会被调用,默认使用的是common mode,也就是引入Common底下的functions。


ThinkPHP.php判断tp的mode。

if(function_exists('saeAutoLoader')){// 自动识别SAE环境
    defined('APP_MODE')     or define('APP_MODE',      'sae');
    defined('STORAGE_TYPE') or define('STORAGE_TYPE',  'Sae');
}else{
    defined('APP_MODE')     or define('APP_MODE',       'common'); // 应用模式 默认为普通模式    
    defined('STORAGE_TYPE') or define('STORAGE_TYPE',   'File'); // 存储类型 默认为File    
}

Think.class.php引入配置并加载配置里面的core,core数组里有functions的路径,加载之。

  // 读取应用模式
  $mode   =   include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php';
  // 加载核心文件
  foreach ($mode['core'] as $file){
      if(is_file($file)) {
        include $file;
        if(!APP_DEBUG) $content   .= compile($file);
      }
  }

如果你在index.php define('APP_MODE', 'api')然后在这里打印$file变量 就能看到你的thinkphp/ThinkPHP/Mode/Api/functions.php

kaiyulee commented 9 years ago

嗯, 已经解决了。谢谢你的回答! @pysnow530