dongmeng9048 2014-04-17 03:51
浏览 115
已采纳

理解Yii :: import方法

As said in the Yii documentation

Imports a class or a directory.

Importing a class is like including the corresponding class file. The main difference is that importing a class is much lighter because it only includes the class file when the class is referenced the first time.

Importing a directory is equivalent to adding a directory into the PHP include path. If multiple directories are imported, the directories imported later will take precedence in class file searching (i.e., they are added to the front of the PHP include path).

Consider the following code snippet:

Yii::import('application.components.document');
echo "This is included file:";
foreach(get_included_files() as $value){
    echo "<div>".$value."</div>";
}

Here is an output:

Z:\home\localhost\www\index.php
Z:\home\localhost\www\yii\framework\yii.php
Z:\home\localhost\www\yii\framework\YiiBase.php
Z:\home\localhost\www\yii\framework\base\interfaces.php
Z:\home\localhost\www\yii\framework\web\CWebApplication.php
Z:\home\localhost\www\yii\framework\base\CApplication.php
Z:\home\localhost\www\yii\framework\base\CModule.php
Z:\home\localhost\www\yii\framework\base\CComponent.php
Z:\home\localhost\www\web-config.php
Z:\home\localhost\www\yii\framework\logging\CLogger.php
Z:\home\localhost\www\yii\framework\web\CHttpRequest.php
Z:\home\localhost\www\yii\framework\base\CApplicationComponent.php
Z:\home\localhost\www\yii\framework\collections\CMap.php
Z:\home\localhost\www\yii\framework\web\CUrlManager.php
Z:\home\localhost\www\protected\controllers\tranController.php
Z:\home\localhost\www\yii\framework\web\CController.php
Z:\home\localhost\www\yii\framework\web\CBaseController.php
Z:\home\localhost\www\protected\controllers\tranAction.php
Z:\home\localhost\www\yii\framework\web\actions\CAction.php

But this class imported after I'm using it like Yii::app()->document;.

  • 写回答

1条回答 默认 最新

  • duanou2016 2014-04-17 05:01
    关注

    function Yii::import don't include file, but simple add path to $_import array, look this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?