日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP yii 框架源码阅读(二) - 整体执行流程分析

發布時間:2023/12/9 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP yii 框架源码阅读(二) - 整体执行流程分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載鏈接:http://tech.ddvip.com/2013-11/1384432766205970.html


一 ?程序入口

<?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;}

靜態成員 $_coreClasses 變量中定義了一些系統自身的核心類

private static $_coreClasses=array('CApplication' => '/base/CApplication.php','CApplicationComponent' => '/base/CApplicationComponent.php','CBehavior' => '/base/CBehavior.php','CComponent' => '/base/CComponent.php',

非 coreClasse 的類注冊在YiiBase的$_classes 數組中: ?

private static $_classes=array(); ? ?

其他的類需要用Yii::import()講類路徑導入PHP include paths 中,直接 ?

include($className.'.php')?


三 CWebApplication的創建 ?

Yii::createWebApplication($config)->run(); 調用createWebApplication函數

public static function createWebApplication($config=null){return self::createApplication('CWebApplication',$config); // 函數中調用createApplication} public static function createApplication($class,$config=null){return new $class($config);}

返回 CWebApplication類的實例


現在autoload機制開始工作了。 ?

當系統 執行 new CWebApplication() 的時候,會自動 ?

include(YII_PATH.'/base/CApplication.php') ?

幾個類的繼承關系是 CWebApplication->CApplication->CModule->CComponent

$config 首先傳遞到CApplication的構造函數中,

public function __construct($config=null){Yii::setApplication($this); // 返回自身的實例,之后可以通過 yii::app() 全局調用// set basePath at early as possible to avoid troubleif(is_string($config))$config=require($config);if(isset($config['basePath'])){$this->setBasePath($config['basePath']);unset($config['basePath']);}else$this->setBasePath('protected'); // 設置路徑 指向protected 目錄Yii::setPathOfAlias('application',$this->getBasePath());Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');$this->preinit();$this->initSystemHandlers(); 設置error 和 exception$this->registerCoreComponents(); 注冊核心組件,放入_componentConfig 靜態變量中$this->configure($config); // 把配置文件數組循環,設置為自身屬性$this->attachBehaviors($this->behaviors); // 設置行為$this->preloadComponents(); // 預加載$this->init(); // 加載請求處理模塊,開始處理請求}

大概過程 ?

application構造函數: ?

1 設置當前運行實例 ?

2 獲取配置參數 ?

3 設置basepath ?

4 設置幾個path;application,webroot ,ext ?

5 preinit ?

6 注冊error、exception處理函數 initSystemHandlers ?

7 加載核心組件 registerCoreComponents 包括webapplication的和application的 ?

8 設置配置文件 configure($config) ?

9 附加行為 $this->attachBehaviors($this->behaviors); ?

10處理加載config中的preload,//通過getComponent分別加載并初始化 $this->preloadComponents(); ?

11 初始化init(); //加載CHttpRequest組件?



總結

以上是生活随笔為你收集整理的PHP yii 框架源码阅读(二) - 整体执行流程分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。