YiiBase.php

开始阅读Yii框架的源代码,顺便把心得记录下来,方便今后的进一步学习。

在阅读Yii的源码之前,我对Yii框架有一个表面的了解,知道一些基本的用法。我发现没有通晓源码,就不能熟练运用框架。因此,下定决心研究一下Yii的源码。

 

Yii::app()的用法解析

Yii::app()这种用法,不看源代码就无法知道为什么可以这样用。下面解析一下这个用法。

<?php
// change the following paths if necessary
$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();

Yii框架建立Web应用的开端,一般形式如上面代码。

在yii.php文件中,定义Yii类,继承YiiBase类,方便扩展YiiBase类。

Yii::createWebApplication($config)创建Web应用实例,即CWebApplication的实例。CWebApplication继承自CApplication,其构造函数第一行是Yii::setApplication($this),因此这个创建的Web应用实例已经被赋值给私有静态变量_app了。可以知道,这是一个单例模式,应用只能被实例化一次,再次实例化抛出异常。YiiBase类中有静态方法app(),现在可以知道为什么可以使用Yii::app()了,Yii::app()即是指唯一可被创建的Web应用实例。

 

Yii框架的懒加载

懒加载,即要用到哪个类则加载哪个类。

Yii调用spl_autoload_register,把自定义的autoload作为__autoload的实现。autoload函数的作用是把需要的类中途include进来。

 

导入文件或路径

public static function import($alias,$forceInclude=false)

如果是路径,且self::$enableIncludePath为true,加入到include_path。

如果是类文件,且$forceInclude为true,直接require进来,否则存入self::$classMap,第一次引用时再载入。

已经导入的文件或路径,保存到self::$_imports,避免重复导入。

 

其他重要函数

createApplication 创建应用实例。

createComponent 创建组件。

getPathOfAlias 获取路径别名的实际路径。

setPathOfAlias 设置实际路径的路径别名。

还有一群与日志有关的函数,学习CLogger类的时候再回头看。

 


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($config['basePath']))
        {
            $this->setBasePath($config['basePath']);
            unset($config['basePath']);
        }
        else
            $this->setBasePath('protected');
        Yii::setPathOfAlias('application',$this->getBasePath());
        Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
        if(isset($config['extensionPath']))
        {
            $this->setExtensionPath($config['extensionPath']);
            unset($config['extensionPath']);
        }
        else
            Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
        if(isset($config['aliases']))
        {
            $this->setAliases($config['aliases']);
            unset($config['aliases']);
        }

        $this->preinit();

        $this->initSystemHandlers();
        $this->registerCoreComponents();

        $this->configure($config);
        $this->attachBehaviors($this->behaviors);
        $this->preloadComponents();

        $this->init();
    类中

转载于:https://www.cnblogs.com/zhanglielie/p/6287293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值