如何为复杂的返回值格式化PHPDoc注释

时间:2012-11-15 08:15:06

标签: php comments phpdoc

我有一个返回类似以下结构的函数:

array('form' => $form, 'options' => $options)

如何在函数注释中正确格式化此返回值?

我的猜测是:

@return array('form' => CForm, 'options' => array) ... comment ...

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

当你必须返回一个奇怪的数组时,记录它只是简单易懂。

这是我将如何做到的。您可以随意使用@return array代替mixed[]

<?php
/**
 * Method to do something
 *
 * @param Some_User_Class $user
 * @param string $blockUserId
 * @throws Some_Model_Relations_ErrorException
 * @return mixed[] see example
 * @example The returned array consists of this
 *  and that and might have foo and bar.
 *  so this makes me feel like the code should be refactored.
 */
public function unblockUser(Some_User_Class $user, $unblockUserId) {
}

答案 1 :(得分:0)

也许有点矫枉过正,但你可以返回一个具有特定接口的对象,而不是常规数组。类似的东西:

/**
 * @return ReturnObject
 */
public function yourMethod()
{
    return new ReturnObjectImpl( $theForm, $theOptions );
}

interface ReturnObject
{
    public function getCForm();
    public function getOptions();
}

class ReturnObjectImpl
    implements ReturnObject
{
    protected $_form;
    protected $_options;

    public function __construct( CForm $form, array $options )
    {
        $this->_form = $form;
        $this->_options = $options;
    }

    public function getCForm()
    {
        return $this->_form;
    }

    public function getOptions()
    {
        return $this->_options;
    }
}