PHP yii框架源码阅读(二) 整体执行流程分析2014-08-16 cnblogs mashuaimama一程序入口
<?php// change the following paths if necessary $yii=dirname(__FILE__)."/http://www.cnblogs.com/framework/yii.php";$config=dirname(__FILE__)."/protected/config/main.php";// remove the following line when in production mode// defined("YII_DEBUG") or define("YII_DEBUG",true);require_once($yii);Yii::createWebApplication($config)->run();
require_once($yii) 语句包含了yii.php 文件,该文件是Yii bootstrap file,包含了 yiibase的基础类,yii完全继承了yiibase
<?php/** * Yii bootstrap file. * * @author Qiang Xue <qiang.xue@gmail.com> * @link http://www.yiiframework.com/ * @copyright Copyright © 2008-2011 Yii Software LLC * @license http://www.yiiframework.com/license/ * @version $Id: yii.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @package system * @since 1.0 */require(dirname(__FILE__)."/YiiBase.php");/** * Yii is a helper class serving common framework functionalities. * * It encapsulates {@link YiiBase} which provides the actual implementation. * By writing your own Yii class, you can customize some functionalities of YiiBase. * * @author Qiang Xue <qiang.xue@gmail.com> * @version $Id: yii.php 2799 2011-01-01 19:31:13Z qiang.xue $ * @package system * @since 1.0 */class Yii extends YiiBase{}在 YiiBase 类中 定义了一些 比如:
public static function createWebApplication($config=null) // 创建启动public static function import($alias,$forceInclude=false) // 类导入public static function createComponent($config) // 创建组件public static function setApplication($app)// 创建类的实例 yii::app()
二 自动加载机制还有比较重要的yii自动加载机制,在yiibase的最后引用了php的标准库函数 spl_autoload_register(array("YiiBase","autoload")) 调用框架中的autoload方法
/** * Class autoload loader. * This method is provided to be invoked within an __autoload() magic method. * @param string $className class name * @return boolean whether the class has been loaded successfully */public static function autoload($className){// use include so that the error PHP file may appearif(isset(self::$classMap[$className]))include(self::$classMap[$className]);else if(isset(self::$_coreClasses[$className]))include(YII_PATH.self::$_coreClasses[$className]);else{// include class file relying on include_pathif(strpos($className,"\")===false)// class without namespace{if(self::$enableIncludePath===false){foreach(self::$_includePaths as $path){$classFile=$path.DIRECTORY_SEPARATOR.$className.".php";if(is_file($classFile)){include($classFile);break;}}}elseinclude($className.".php");}else// class name with namespace in PHP 5.3{$namespace=str_replace("\",".",ltrim($className,"\"));if(($path=self::getPathOfAlias($namespace))!==false)include($path.".php");elsereturn false;}return class_exists($className,false) || interface_exists($className,false);}return true;}