2014-04-17 4 views
0

으로는이해 YII :: 수입 방법

이 클래스 또는 디렉터리를 가져옵니다 YII 문서에서 말했다.

클래스 가져 오기는 해당 클래스 파일을 포함하는 것과 같습니다. 의 주요 차이점은 클래스를 가져올 때 클래스가 첫 번째로 시간을 참조 할 때 클래스 파일 만 포함하므로 훨씬 더 가볍습니다.

디렉토리를 가져 오는 것은 PHP 경로에 디렉토리를 추가하는 것과 같습니다. 여러 디렉토리를 가져 오는 경우 나중에 가져온 디렉토리가 클래스 파일 검색에서 우선 적용됩니다 (예 : PHP 포함 경로 앞에 추가됨).

는 다음과 같은 코드를 생각해

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 

하지만 Yii::app()->document;처럼 사용하고 이후 수입이 클래스 : 여기

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

가 출력됩니다.

답변

1

함수 Yii :: import는 파일을 포함하지 않지만 $ _import 배열에 간단한 경로를 추가하면 this으로 보입니다.

+0

답변 해 주셔서 감사합니다. 그러나 나는 한 가지를 명확히하고 싶다 : 만약 우리가 컴포넌트에'Yii :: app() -> 문서로 적용한다면,'__get' 마법 메소드가 호출된다. 이 메소드는'_componentConfig' 배열을 사용하지만'_import'를 사용하지 않는'getComponent' 메소드를 호출합니다. 그렇다면 왜 Yii :: import ('application.components.document');가 없으면'Yii-> app() -> document' 컴포넌트를 사용할 수 없습니까? –

+0

config 파일에 클래스 구성 요소를 지정했기 때문에 가능하다고 생각합니다. document => array ('class'=> 'application.components.document') –