magento如何调用此注释方法

Ric*_*rma 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;?>
Run Code Online (Sandbox Code Playgroud)

当我去阻止看到模板文件中的第一行代码时

 <?php $testimonials = $this->getTestimonials(); ?>
Run Code Online (Sandbox Code Playgroud)

而且我找不到在该块类中声明的这个方法,而是在注释部分中我可以看到这个方法.但是此方法尚未在模块中的任何位置声明.这是怎么回事?阻止下面的类代码.

/**
 * 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

如果我是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');
}
Run Code Online (Sandbox Code Playgroud)

}

Luk*_*lls 6

Magento使用魔术__call()方法为Magento对象中的私有(隐藏)数据动态"创建"访问器方法.

Magento中的大多数类都继承自定义Varien_Object魔术__call()方法的类.

如果你想进一步了解__call()PHP中的魔术函数,可以在这里阅读: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以及您的提示,虽然此方法未在代码中定义,但它应该可以通过magic __call()方法访问.如果您使用的是像Netbeans或Eclipse这样的IDE,那么您应该获得该方法的代码完成.