2014-04-13 7 views
1

/protected/components/document.php에있는 내가 작성한 테스트 구성 요소 : 이제web-app에서 Yii 구성 요소를 사용하는 방법은 무엇입니까?

class Document extends CComponent{ 
    private $_width; 
    public __construct($width){ 
     this->_width=$width; 
    } 
    public function getWidth(){ 
     return $this->_width; 
    } 
} 

나는 다음과 같은 내 컨트롤러의 액션 중 하나에이 구성 요소를 사용하려고 해요 :

$docum=new Document('150'); 
echo $docum->width; 

를하지만 다음과 같은 예외 제기 됨 :

include(Document.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory 

해결 방법?

답변

3

먼저 파일 이름을 document.php에서 Document.php으로 변경해야합니다. 둘째로 new을 통해 인스턴스를 만들려면 CComponent 클래스에서 인스턴스를 확장해서는 안됩니다.

  1. 이 CApplicationComponent에서 클래스를 확장해야 new를 통해 그것을 만들지 않고 구성 요소를 사용합니다. 구성 요소 섹션에서 config.php를에
  2. 구성을 :

    '문서'=> 배열 ( '클래스'=> '문서', // 다른 속성 ),

  3. 를 사용하여 Yii :: app() -> document-> method(); 당신이 CApplicationComponent에서 클래스를 확장 할 경우

또한 대신 모든 초기화는 오버라이드 (override) init() 방법에서 발생한다 방법을 __construct 정의해서는 안된다.

+0

감사합니다. 그러나 나는 너를 이해하지 못한다. 새로운 기능없이 앱에서 구성 요소를 사용하려면 어떻게해야합니까? 나는 http://www.yiiframework.com/doc/guide/1.1/en/basics.component를 읽었으나 애플리케이션에 컴포넌트 클래스를 임베드하는 것에 대한 정보는 없다. –

-1

basicly의 파일 가져없는 : 당신의 답변

Yii::import('application.components.Document'); 
관련 문제