2012-10-03 2 views
2

Magento를 배우고 있는데이 문제가 발생했습니다.Magento는이 주석 처리 된 메서드를 어떻게 호출합니까?

템플릿 파일의 코드

<?php $testimonials = $this->getTestimonials(); ?> 
<?php $i = 0;?> 
<?php if ($testimonials->count() > 0): ?> 
<div class="block testimonials_sidebar"> 
    <div class="block-title"> 
    <strong><span><?php echo $this->__('Testimonials') ?></span></strong> 
    </div> 
    <div class="block-content"> 
     <?php foreach ($testimonials as $testimonial): ?> 
      <div class="testimonial_sidebar_box"> 
       <div class="testimonial_sidebar_text"><?php echo $testimonial->getTestimonialText(); ?></div> 
       <div class="testimonial_sidebar_name"><?php echo $testimonial->getTestimonialName(); ?></div> 
      </div> 
     <?php endforeach; ?> 
     <div class="actions"> 
      <a href="<?php echo $this->getUrl('testimonials'); ?>"><?php echo $this->__('View All Testimonials'); ?></a> 
     </div> 
    </div> 
</div> 
<?php endif;?> 

내가

<?php $testimonials = $this->getTestimonials(); ?> 

있는 템플릿 파일에 코드의 첫 번째 줄을 볼 차단에 가서 내가 그 블록에서 선언이 방법을 찾을 수 없습니다 클래스 대신이 메서드를 볼 수 있지만 주석 처리 된 섹션에서 볼 수 있습니다. 그러나이 방법은 모듈의 어디에도 선언되어 있지 않습니다. 어떻게 된 일인가? 아래의 클래스 코드를 차단하십시오.

/** 
* Frontend block for testimonials 
* 
* @method Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection getTestimonials() 
*/ 
class Turnkeye_Testimonial_Block_Testimonial extends Mage_Core_Block_Template 
{ 

    /** 
    * Before rendering html, but after trying to load cache 
    * 
    * @return Turnkeye_Testimonial_Block_Testimonial 
    */ 
    protected function _beforeToHtml() 
    { 
     $this->_prepareCollection(); 
     return parent::_beforeToHtml(); 
    } 

    /** 
    * Prepare testimonial collection object 
    * 
    * @return Turnkeye_Testimonial_Block_Testimonial 
    */ 
    protected function _prepareCollection() 
    { 
     /* @var $collection Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection */ 
     $collection = Mage::getModel("turnkeye_testimonial/testimonial")->getCollection(); 
     if ($this->getSidebar()){ 
      $collection->addFieldToFilter('testimonial_sidebar', '1'); 
     } 

     $collection->setOrder('testimonial_position', 'ASC') 
        ->load(); 
     $this->setTestimonials($collection); 
     return $this; 
    } 

} 

템플릿 파일에서 해당 방법을 Ctrl 키를 누른 상태로 클릭하면 주석이 표시됩니다. 콜렉션을 가리키고있는 것을 볼 수 있습니다. 콜렉션 코드는 여기에 있습니다.

class Turnkeye_Testimonial_Model_Mysql4_Testimonial_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract 
{ 

/** 
* Initialization here 
* 
*/ 
public function _construct() 
{ 
    parent::_construct(); 
    $this->_init('turnkeye_testimonial/testimonial'); 
} 

}

답변

6

Magento는 magento 객체의 비공개 (숨겨진) 데이터에 대한 접근 자 메서드를 동적으로 "생성"하는 magic __call() 메서드를 사용합니다.

Magento의 대부분의 클래스는 마법 __call() 메서드가 정의 된 Varien_Object에서 상속됩니다.

PHP의 마법 __call() 기능에 대해 자세히 알고 싶다면 여기에서 http://www.php.net/manual/en/language.oop5.overloading.php#object.call을 읽어보십시오.

기타 마법 방법은 http://www.php.net/manual/en/language.oop5.magic.php에서 찾을 수 있습니다. (__call()과 유사하게 마법 방법은 __get()__set()입니다.

이 모든 것이 Magento에서 어떻게 작동하는지에 대한 설명을 제공하는 기사를 발견했습니다 : http://codemagento.com/2011/02/where-are-my-getters-and-setters/.

@method으로 시작하는 주석 행은 생성자, IDE 및이 메서드가 코드에 정의되어 있지 않지만 마법 __call() 메서드로 액세스 할 수 있어야한다는 것을 문서화하는 힌트입니다. Netbeans 또는 Eclipse와 같은 IDE를 사용하는 경우 해당 메소드에 대한 코드 완성을 얻어야합니다.

관련 문제