欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    2022年Yii_PHP_框架分析 .pdf

    • 资源ID:27189242       资源大小:208.79KB        全文页数:32页
    • 资源格式: PDF        下载积分:4.3金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要4.3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    2022年Yii_PHP_框架分析 .pdf

    第 1 页 共 32 页Yii PHP 框架分析Yii PHP 框架分析作者: jjjj http:/ 基于 yii1.0.8 的代码分析的。用了一个下午整理的,流水账,感兴趣的凑合着先看,国庆期间推出个整理修改版,然后再完成后两个部分(MVC 和 Yii 的整体结构分析) 。1. 启动网站的唯一入口程序index.php : $yii=dirname(_FILE_)././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 , Yii 类是 YiiBase 类的完全继承:class Yii extends YiiBase 系统的全局访问都是通过Yii 类(即 YiiBase 类)来实现的,Yii 类的成员和方法都是static类型。2. 类加载Yii 利用 PHP5 提供的 spl 库来完成类的自动加载。在YiiBase.php 文件结尾处spl_autoload_register(array(YiiBase,autoload); 将 YiiBase 类的静态方法autoload 注册为类加载器。PHP autoload 的简单原理就是执行new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include 。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 32 页 - - - - - - - - - 第 2 页 共 32 页下面是 YiiBase 类的 autoload 方法:public static function autoload($className) / use include so that the error PHP file may appear if(isset(self:$_coreClasses$className) include(YII_PATH.self:$_coreClasses$className); else if(isset(self:$_classes$className) include(self:$_classes$className); else include($className.php); 可以看到 YiiBase 的静态成员 $_coreClasses 数组里预先存放着Yii 系统自身用到的类对应的文件路径:private static $_coreClasses=array( CApplication = /base/CApplication.php, CBehavior = /base/CBehavior.php, CComponent = /base/CComponent.php, . ) 非 coreClasse 的类注册在YiiBase 的$_classes 数组中:private static $_classes=array(); 其他的类需要用Yii:import() 讲类路径导入PHP include paths 中,直接include($className.php) 3. CWebApplication 的创建回到前面的程序入口的Yii:createWebApplication($config)-run(); public static function createWebApplication($config=null) return new CWebApplication($config); 现在 autoload 机制开始工作了。当系统执行new CWebApplication() 的时候,会自动include(YII_PATH./base/CApplication.php) 将 main.php 里的配置信息数组$config 传递给CWebApplication 创建出对象,并执行对象的名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 32 页 - - - - - - - - - 第 3 页 共 32 页run() 方法启动框架。CWebApplication 类的继承关系CWebApplication - CApplication - CModule - CComponent $config 先被传递给CApplication 的构造函数public function _construct($config=null) Yii:setApplication($this); / set basePath at early as possible to avoid trouble if(is_string($config) $config=require($config); if(isset($configbasePath) $this-setBasePath($configbasePath); unset($configbasePath); else $this-setBasePath(protected); Yii:setPathOfAlias(application,$this-getBasePath(); Yii:setPathOfAlias(webroot,dirname($_SERVERSCRIPT_FILENAME); $this-preinit(); $this-initSystemHandlers(); $this-registerCoreComponents(); $this-configure($config); $this-attachBehaviors($this-behaviors); $this-preloadComponents(); $this-init(); Yii:setApplication($this); 将自身的实例对象赋给Yii的静态成员$_app,以后可以通过Yii:app() 来取得。后面一段是设置CApplication 对象的 _basePath , 指向 proteced 目录。Yii:setPathOfAlias(application,$this-getBasePath(); Yii:setPathOfAlias(webroot,dirname($_SERVERSCRIPT_FILENAME); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 32 页 - - - - - - - - - 第 4 页 共 32 页设置了两个系统路径别名application 和 webroot,后面再import 的时候可以用别名来代替实际的完整路径。别名配置存放在YiiBase 的 $_aliases 数组中。$this-preinit(); 预初始化。 preinit() 是在CModule 类里定义的,没有任何动作。$this-initSystemHandlers() 方法内容:/* * Initializes the class autoloader and error handlers. */ protected function initSystemHandlers() if(YII_ENABLE_EXCEPTION_HANDLER) set_exception_handler(array($this,handleException); if(YII_ENABLE_ERROR_HANDLER) set_error_handler(array($this,handleError),error_reporting(); 设置系统exception_handler 和 error_handler,指向对象自身提供的两个方法。4. 注册核心组件$this-registerCoreComponents(); 代码如下:protected function registerCoreComponents() parent:registerCoreComponents(); $components=array( urlManager=array( class=CUrlManager, ), request=array( class=CHttpRequest, ), session=array( class=CHttpSession, ), assetManager=array( class=CAssetManager, ), 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 32 页 - - - - - - - - - 第 5 页 共 32 页user=array( class=CWebUser, ), themeManager=array( class=CThemeManager, ), authManager=array( class=CPhpAuthManager, ), clientScript=array( class=CClientScript, ), ); $this-setComponents($components); 注册了几个系统组件(Components) 。Components 是在 CModule 里定义和管理的,主要包括两个数组private $_components=array(); private $_componentConfig=array(); 每个Component 都是IApplicationComponent接口的实例,Componemt 的实例存放在$_components 数组里,相关的配置信息存放在$_componentConfig数组里。配置信息包括Component 的类名和属性设置。CWebApplication 对象注册了以下几个Component:urlManager, request,session,assetManager,user,themeManager,authManager,clientScript。CWebApplication 的parent 注册了以下几个Component:coreMessages,db,messages,errorHandler,securityManager,statePersister 。Component 在 YiiPHP里是个非常重要的东西,它的特征是可以通过CModule 的 _get() 和 _set() 方法来访问。Component 注册的时候并不会创建对象实例,而是在程序里被第一次访问到的时候,由CModule 来负责(实际上就是Yii:app() )创建。5. 处理$config 配置继续,$this-configure($config); configure() 还是在 CModule 里:public function configure($config) if(is_array($config) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 32 页 - - - - - - - - - 第 6 页 共 32 页 foreach($config as $key=$value) $this-$key=$value; 实际上是把 $config 数组里的每一项传给CModule 的 父类 CComponent _set() 方法。public function _set($name,$value) $setter=set.$name; if(method_exists($this,$setter) $this-$setter($value); else if(strncasecmp($name,on,2)=0 & method_exists($this,$name) /duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this-_e$name) $this-_e$name=new CList; $this-_e$name-add($value); else if(method_exists($this,get.$name) throw new CException(Yii:t(yii,Property class.property is read only., array(class=get_class($this), property=$name); else throw new CException(Yii:t(yii,Property class.property is not defined., array(class=get_class($this), property=$name); 我们来看看:if(method_exists($this,$setter) 根据这个条件,$config 数组里的basePath, params, modules, import, components 都被传递给相应的setBasePath(), setParams() 等方法里进行处理。6、$config 之 import 其中import 被传递给CModule 的 setImport:public function setImport($aliases) foreach($aliases as $alias) Yii:import($alias); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 32 页 - - - - - - - - - 第 7 页 共 32 页 Yii:import($alias) 里的处理:public static function import($alias,$forceInclude=false) / 先判断 $alias 是否存在于YiiBase:$_imports 中,已存在的直接return, 避免重复import。if(isset(self:$_imports$alias) / previously imported return self:$_imports$alias; / $alias 类已定义,记入$_imports ,直接返回if(class_exists($alias,false) return self:$_imports$alias=$alias; / 类似urlManager 这样的已定义于$_coreClasses的类,或不含.的直接类名,记入$_imports ,直接返回if(isset(self:$_coreClasses$alias) | ($pos=strrpos($alias,.)=false) / a simple class name self:$_imports$alias=$alias; if($forceInclude) if(isset(self:$_coreClasses$alias) / a core class require(YII_PA TH.self:$_coreClasses$alias); else require($alias.php); return $alias; / 产生一个变量$className,为 $alias最后一个 .后面的部分/ 这样的: x.y.ClassNamer / $className 不等于*, 并且ClassNamer 类已定义的,ClassNamer 记入$_imports ,直接返回if($className=(string)substr($alias,$pos+1)!=* & class_exists($className,false) return self:$_imports$alias=$className; / 取得$alias 里真实的路径部分并且路径有效if($path=self:getPathOfAlias($alias)!=false) / $className!=* ,$className 记入$_imports if($className!=*) self:$_imports$alias=$className; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 32 页 - - - - - - - - - 第 8 页 共 32 页if($forceInclude) require($path.php); else self:$_classes$className=$path.php; return $className; / $alias 是system.web.*这样的已 *结尾的路径,将路径加到include_path 中else / a directory set_include_path(get_include_path().PA TH_SEPARATOR.$path); return self:$_imports$alias=$path; else throw new CException(Yii:t(yii,Alias alias is invalid. Make sure it points to an existing directory or file., array(alias=$alias); 7. $config 之 components $config 数组里的$components 被传递给CModule 的 setComponents($components) public function setComponents($components) foreach($components as $id=$component) if($component instanceof IApplicationComponent) $this-setComponent($id,$component); else if(isset($this-_componentConfig$id) $this-_componentConfig$id=CMap:mergeArray($this-_componentConfig$id,$component); else $this-_componentConfig$id=$component; $componen 是 IApplicationComponen的实例的时候,直接赋值:$this-setComponent($id,$component) ,public function setComponent($id,$component) $this-_components$id=$component; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 32 页 - - - - - - - - - 第 9 页 共 32 页if(!$component-getIsInitialized() $component-init(); 如果 $id 已存在于 _componentConfig 中(前面注册的coreComponent),将$component 属性加进入。其他的 component 将 component 属性存入 _componentConfig 中。8. $config 之 params 这个很简单public function setParams($value) $params=$this-getParams(); foreach($value as $k=$v) $params-add($k,$v); configure 完毕!9. attachBehaviors $this-attachBehaviors($this-behaviors); 空的,没动作预创建组件对象$this-preloadComponents(); protected function preloadComponents() foreach($this-preload as $id) $this-getComponent($id); getComponent() 判 断 _components 数 组 里 是 否 有$id的 实 例 , 如 果 没 有 , 就 根 据_componentConfig$id 里 的 配 置 来 创 建 组 件 对 象 , 调用 组 件 的init() 方法 , 然 后 存 入_components$id 中。10. init() $this-init(); 函数内: $this-getRequest(); 创建了 Reques 组件并初始化。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 32 页 - - - - - - - - - 第 10 页 共 32 页11. run() public function run() $this-onBeginRequest(new CEvent($this); $this-processRequest(); $this-onEndRequest(new CEvent($this); Yii PHP 框架分析(二)作者: wdy http:/ Yii 是基于组件( component-based)的 web 框架, CComponent 类是所有组件的基类。CComponent 类为子类提供了基于属性(property) 、事件 (event)、行为 (behavior)编程接口。组件的属性 (property) Ccomponent 类并没有提供属性的变量存储,需要由子类来提供两个方法来实现。子类的getPropertyName() 方 法 提 供 $component-PropertyName的 取 值 操 作 数 据 , 子 类 的setPropertyName($val)方法提供 $component-PropertyName 赋值操作。$width=$component-textWidth; / 获取textWidth 属性实现方式为调用子类提供的方法$width=$component-getTextWidth() $component-textWidth=$width; / 设置textWidth 属性实现方式为调用子类提供的方法$component-setTextWidth($width) public function getTextWidth() return $this-_textWidth; public function setTextWidth($value) $this-_textWidth=$value; 组件的属性值是大小写不敏感的(类的成员时大小写敏感的)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 32 页 - - - - - - - - - 第 11 页 共 32 页组件的事件( event)组件事件是一种特殊的属性,它可以将事件处理句柄(可以是函数名、类方法或对象方法)注册(绑定)到一个事件名上,句柄在事件被唤起的时候被自动调用。组件事件存放在CComponent 的$_e数组里,数组的键值为事件的名字,键值的数值为一个 Clist 对象, Clist 是 Yii 提供的一个队列容器,Clist 的方法 add()添加事件的回调handle。/添加一个全局函数到事件处理$component- onBeginRequest=” logRequest”;/添加一个类静态方法到事件处理$component- onBeginRequest=array(“ CLog”, ” logRequest”);/添加一个对象方法到事件处理$component- onBeginRequest=array($mylog,” logRequest”);唤起事件:$component -raiseEvent(onBeginRequest , $event); 会自动调用 : logRequest($event), Clog: logRequest($event) 和$mylog.logRequest($event) 事件句柄必须按照如下来定义: function methodName($event) . $event 参数是CEvent 或其子类的实例,它至少包含了是谁挂起了这个事件的信息。事件的名字以”on”开头,在 _get()和_set()里可以通过这个来区别属性和事件。组件行为( behavior)组件的行为是一种不通过继承而扩展组件功能的方法(参见设计模式里的策略模式)。行为类必须实现IBehavior 接口,大多数行为可以从CBehavior 基类扩展而来。IBehavior 接口提供了4 个方法。attach($component) 将 自 身 关 联 到 组 件 , detach($component) 解 除 $component关 联 ,getEnabled()和 setEnabled()设置行为对象的有效性。行为对象存放在组件的$_m 数组里,数组键值为行为名字符串,数组值为行为类对象。组件通过attachBehavior ($name,$behavior) 来扩展一个行为:$component- attachBehavior (,render?,$htmlRender)为$component 添加了一个名字为render 的行为,$htmlRender 需是一个实现IBehavior 接口名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 32 页 - - - - - - - - - 第 12 页 共 32 页的对象,或是一个数组:array( class=path.to.BehaviorClass, property1=value1, property2=value2, * ) 会根据数组的class 来创建行为对象并设置属性值。$htmlRender 被存储到 $_mrender中。外部调用一个组件未定义的方法时,魔术方法_call() 会遍历所有行为对象,如果找到同名方法就调用之。例如$htmlRender 有个方法renderFromFile() ,则可以直接当做组件的方法来访问:$component- renderFromFile () CComponent 源码分析/所有部件的基类class CComponent private $_e; private $_m; /获取部件属性、事件和行为的magic method public function _get($name) $getter=get.$name; /是否存在属性的get 方法if(method_exists($this,$getter) return $this-$getter(); /以 on 开头,获取事件处理句柄else if(strncasecmp($name,on,2)=0 & method_exists($this,$name) / 事件名小写$name=strtolower($name); / 如果 _e$name 不存在,返回一个空的CList 事件句柄队列对象if(!isset($this-_e$name) $this-_e$name=new CList; / 返回 _e$name里存放的句柄队列对象return $this-_e$name; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 32 页 - - - - - - - - - 第 13 页 共 32 页/ _m$name 里存放着行为对象则返回else if(isset($this-_m$name) return $this-_m$name; else throw new CException(Yii:t(yii,Property class.property is not defined., array(class=get_class($this), property=$name); /* * PHP magic method * 设置组件的属性和事件*/ public function _set($name,$value) $setter=set.$name; /是否存在属性的set 方法if(method_exists($this,$setter) $this-$setter($value); /name 以 on 开头,这是事件处理句柄else if(strncasecmp($name,on,2)=0 & method_exists($this,$name) / 事件名小写$name=strtolower($name); / _e$name 不存在则创建一个CList 对象if(!isset($this-_e$name) $this-_e$name=new CList; / 添加事件处理句柄$this-_e$name-add($value); / 属性没有 set方法,只有get 方法,为只读属性,抛出异常else if(method_exists($this,get.$name) throw new CException(Yii:t(yii,Property class.property is read only., array(class=get_class($this), property=$name); else throw new CException(Yii:t(yii,Property class.property is not defined., array(class=get_class($this), property=$name); /* * PHP magic method * 为 isset()函数提供是否存在属性和事件处理句柄的判断*/ public function _isset($name) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 32 页 - - - - - - - - - 第 14 页 共 32 页$getter=get.$name; if(method_exists($this,$getter) return $this-$getter()!=null; else if(strncasecmp($name,on,2)=0 & method_exists($this,$name) $name=strtolower($name); return isset($this-_e$name) & $this-_e$name-getCount(); else return false; /* * PHP magic method * 设置属性值为空或删除事件名字对应的处理句柄*/ public function _unset($name) $setter=set.$name; if(method_exists($this,$setter) $this-$setter(null); else if(strncasecmp($name,on,2)=0 & method_exists($this,$name) unset($this-_estrtolower($name); else if(method_exists($this,get.$name) throw new CException(Yii:t(yii,Property class.property is read only., array(class=get_class($this), property=$name); /* * PHP magic method * CComponent 未定义的类方法,寻找行为类里的同名方法,实现行为方法的调用*/ public function _call($name,$parameters) / 行为类存放的 $_m 数组不空if($this-_m!=null) / 循环取出 $_m 数组里存放的行为类foreach($this-_m as $object) / 行为类对象有效,并且方法存在,调用之if($object-enabled & method_exists($object,$name) return call_user_func_array(array($object,$name),$parameters); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 32 页 - - - - - - - - - 第 15 页 共 32 页 throw new CException(Yii:t(yii,class does not have a method named name., array(class=get_class($this), name=$name); /* * 根据行为名返回行为类对象*/ public function asa($behavior) return isset($this-_m$behavior) ? $this-_m$behavior : null; /* * Attaches a list of behaviors to the component. * Each behavior is indexed by its name and should be an instance of * link IBehavior, a string specifying the behavior class, or an * array of the following structure: * * array

    注意事项

    本文(2022年Yii_PHP_框架分析 .pdf)为本站会员(Che****ry)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开