모듈 개발 시 item 클래스를 다중상속 으로 구현하려고 합니다.

 

aa.item.class

aaItem extends Object

{

   var $target_srl = 0;

   function __construct($target_srl = 0)
   {

        $this->target_srl = $target_srl;

        $this->_loadFromDB();

   }

 

   /**
  * Get data from database, and set the value to item object
  *
  * this method is protected
  * @return void
  */
  protected function _loadFromDB()
  {

  }

 

}

 

bb.item.class

bb.item.class extends aaItem

{

     function _loadFromDB()

     {

     }

}

 

위와 같이 다중상속으로 클래스 선언하여 구현 시 class not found 오류가 발생하네요.

별도의 파일 생성 룰이 있는지요?

 

정상

new aaItem($target_srl);


Class not found 오류

new bbItem($target_srl)

 

  • Lv37

    파일명과 클래스명이 매치되지 않아서 오토로딩이 작동하지 않는 것 같습니다.

    aa 모듈 폴더 내에 aa.item.php라는 파일이 있다면 aaitem 클래스를 처음 사용하는 시점에 오토로딩 됩니다.

    bbitem 클래스는 당연히 bb 모듈 폴더 내에 bb.item.php라는 파일을 찾게 되고요.

     

    어떤 형태로든 aaitem을 먼저 한 번 사용한 후에 new bbitem을 호출해 보세요.

    아니면 수동으로 include_once를 써주셔도 되고요.

  • Lv37

    제가 질문 내용을 잘못 올렸네요.

    동일 모듈 내에서의 상속클래스를 구현하려고 합니다.

     

    aa.a.item.class

    aaAItem extends Object

    {

       var $target_srl = 0;

       function __construct($target_srl = 0)
       {

            $this->target_srl = $target_srl;

            $this->_loadFromDB();

       }

     

       /**
      * Get data from database, and set the value to item object
      *
      * this method is protected
      * @return void
      */
      protected function _loadFromDB()
      {

      }

     

    }

     

    aa.b.item.class

    aaBItem extends aaAItem

    {

         function _loadFromDB()

         {

         }

    }

  • Lv37

    정확하게 "모듈명Item"이라는 클래스가 "모듈명"이라는 폴더 내에 "모듈명.item.php"이라는 파일에 정의되어 있나요? 그렇지 않다면 오토로딩이 작동하지 않으므로 필요하실 때마다 직접 include_once 해주셔야 합니다. class_exists 함수로 확인해 보세요. (이것도 라이믹스에서나 통하는 얘기고, XE에서는 item 클래스가 아예 오토로딩되지 않습니다.)

    PHP 버전에 따라서는 클래스 정의 도중 문법 오류가 발생하면 클래스가 정의되지 않은 것으로 취급하기도 하니 주의하시고요.

  • Lv37
    모듈명.item.php, 모듈명.item.a.php 로 정의했습니다.
    동일 모듈인데 class_exists 로 확인해보니 오토로딩이 작동 안하는 것 같네요.
    원인을 모르겠네요.
  • Lv37

    모듈명.item.a.php는 오토로딩이 되지 않지요. 해당 파일에서 정의한 클래스를 상속받는 다른 클래스가 있다면 거기에도 영향을 주고요.

  • Lv37
    네.. 답변 감사합니다.
    상속받는 클래스에 오토로딩이 작동하도록 하려면 클래스명을 어떻게 줘야 되는지요?
  • Lv37

    위에 쓴 것처럼 클래스명은 모듈명Item, 파일명은 모듈명.item.php만 인식합니다.
    정확하게 이 조합이 아니라면 오토로딩이 되지 않으므로

    상속 관계와 무관하게 그 밖의 모든 클래스는 직접 include_once 하셔야 합니다.

  • Lv37
    네.. 여러가지 테스트 해봤는데 잘 안되더군요.
    include_once로 처리하는 방법밖엔 없군요. 답변 감사드려요.
    많은 도움이 됐습니다~^^