yii源代码之旅(2)

第一步,加载常量。

第二步,声明YiiBase类

第三步,注册自动导入类的方法

第四步,导入/base/interfaces.php文件

 

 涉及系统函数:

microtime,日历和时间扩展下的Date/Time函数

defined 判断YII_BEGIN_TIME常量是否存在,不存在的话,设置该常量为当前时间。

spl_autoload_register  spl函数

导入接口文件,如下:

IApplicationComponent  所有的应用组件都必须实现该接口。应用完成配置后,将调用该接口的init方法。

          ICache  所有的缓存组件必须实现该接口。

          ICacheDependency 所有的缓存从属类必须实现该接口。

 

           IStatePersister

           IFilter  actiion filter必须实现此接口

           IAction  controller action必须实现此接口

           IWebServiceProvider

           IViewRenderer    view render类必须实现此接口

           IUserIdentity 

           IWebUser

           IAuthManager 

           IBehavior 所有的behavior类必须实现此接口。

           IWidgetFactory

           IDataProvider

           ILogFilter

            

 

<!--EndFragment-->

 

 回到index.php

看最后一行代码:

Yii::createWebApplication($config)->run();

 

由于Yii继承YiiBase,且Yii没有createWebApplication方法,因此调用YiiBase类的方法。

CreateWebApplication代码如下:

 

 

/**
	 * Creates a Web application instance.
	 * @param mixed $config application configuration.
	 * If a string, it is treated as the path of the file that contains the configuration;
	 * If an array, it is the actual configuration information.
	 * Please make sure you specify the {@link CApplication::basePath basePath} property in the configuration,
	 * which should point to the directory containing all application logic, template and data.
	 * If not, the directory will be defaulted to 'protected'.
	 * @return CWebApplication
	 */
	public static function createWebApplication($config=null)
	{
		return self::createApplication('CWebApplication',$config);
	}

 

 调用自身的createApplication,将字符串CWebApplication传递进去。

createApplication方法如下:

/**
	 * Creates an application of the specified class.
	 * @param string $class the application class name
	 * @param mixed $config application configuration. This parameter will be passed as the parameter
	 * to the constructor of the application class.
	 * @return mixed the application instance
	 */
	public static function createApplication($class,$config=null)
	{
		return new $class($config);
	}

 初始化一个CWebApplication。由于spl_autoload_register 注册了YiiBase的autoload方法。因此调用该方法

YiiBase 的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 appear
		if(isset(self::$classMap[$className])){
			include(self::$classMap[$className]);
		}
		elseif(isset(self::$_coreClasses[$className])){
			include(YII_PATH.self::$_coreClasses[$className]);
			
		}
		else
		{
			// include class file relying on include_path
			if(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);
							if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
								throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
									'{class}'=>$className,
									'{file}'=>$classFile,
								)));
							break;
						}
					}
				}
				else
					include($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');
				else
					return false;
			}
			return class_exists($className,false) || interface_exists($className,false);
		}
		return true;
	}

 参数$className就是要初始化的类名,这里就是CWebApplication.

判断属性$classMap是否存在$className的这个key,如果存在,include导入,因为$classMap里面类名=>文件路径。

判断属性$_coreClasses是否存在$className的这个key,如果存在,include导入,这里导入的路径要加常量YII_PATH,因为$_coreClasses里面是相对路径。

如果都不成立的话,执行else:

 这里涉及到系统函数

strpos 文本处理--字符串函数下

           查找字符串首次出现的位置。如果没有将返回false

          相关函数:

         strrpos:查找字符串在目标字符串中最后出现的位置。

         stripos : 查找字符串首次出现的位置(不区分大小写)

         strripos :查找字符串在目标字符串中最后出现的位置(不区分大小写)

         strrchr :查找指定字符在字符串中的最后一次出现

         substr :截取字符串

         strstr ,返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。

         stristr    和strstr相比,不区分大小写。

is_file 函数:

              FileSystem函数

              判断文件是否为正常的文件。

realpath :返回规范化的绝对路径名.

basename:返回路径的文件名,如果有文件后缀,第二个参数指定后缀,可以去掉文件后缀

str_replace:将第三个参数中的所有第一个参数替换成第二个参数。

ltrim:去掉字符串开始位置的字符。默认空字符

          相似:rtrime,右边的字符。

          trime ,去除字符串两端的字符。

class_exists,interface_exists   【class/object函数】

 

 由于CWebApplication 继承了CApplication,CApplication又继承了CModule,CModule又继承了CComponent。故自动导入要调用4次。由于CWebApplication没有构造函数,因此调用其父类CApplication的构造函数来完成初始化。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值