Thinkphp3.2 框架加載包含文件爲什麼require而不是include,include 和 require 語句是相同的,除了錯誤處理方面:
require 會生成致命錯誤(E_COMPILE_ERROR)並停止腳本, include 只生成警告(E_WARNING),並且腳本會繼續。
1,例如入口文件 index.php
// 引入ThinkPHP入口文件
require './ThinkPHP/ThinkPHP.php';
2,自動加載文件 用include
/**
* 類庫自動加載
* @param string $class 對象類名
* @return void
*/
public static function autoload($class) {
// 檢查是否存在映射
if(isset(self::$_map[$class])) {
include self::$_map[$class];
}elseif(false !== strpos($class,'\\')){
$name = strstr($class, '\\', true);
if(in_array($name,array('Think','Org','Behavior','Com','Vendor')) || is_dir(LIB_PATH.$name)){
// Library目錄下面的命名空間自動定位
$path = LIB_PATH;
}else{
// 檢測自定義命名空間 否則就以模塊爲命名空間
$namespace = C('AUTOLOAD_NAMESPACE');
$path = isset($namespace[$name])? dirname($namespace[$name]).'/' : APP_PATH;
}
$filename = $path . str_replace('\\', '/', $class) . EXT;
if(is_file($filename)) {
// Win環境下面嚴格區分大小寫
if (IS_WIN && false === strpos(str_replace('/', '\\', realpath($filename)), $class . EXT)){
return ;
}
include $filename;
}
}elseif (!C('APP_USE_NAMESPACE')) {
// 自動加載的類庫層
foreach(explode(',',C('APP_AUTOLOAD_LAYER')) as $layer){
if(substr($class,-strlen($layer))==$layer){
if(require_cache(MODULE_PATH.$layer.'/'.$class.EXT)) {
return ;
}
}
}
// 根據自動加載路徑設置進行嘗試搜索
foreach (explode(',',C('APP_AUTOLOAD_PATH')) as $path){
if(import($path.'.'.$class))
// 如果加載類成功則返回
return ;
}
}
}
文章來自(www.dc3688.com)