代码之家  ›  专栏  ›  技术社区  ›  Zlatan Omerović

如何在PHPDoc中提及属性?

  •  1
  • Zlatan Omerović  · 技术社区  · 7 年前

    我试图在我的类的其他注释中提到我的类的一个属性,即在该类的一个方法中。

    例如,如果我们有以下代码:

    (请搜索: property $mention -- @property Village::mention does not work )

    class Village {
        /**
         * @var array Data container.
         */
        public $data = [];
    
        /**
          *
          */
        public $mention = 'Me';
    
        /**
         * Village constructor which injects data.
         *
         * @param $data
         */
        public function __construct($data) {
            $this->data = $data;
        }
    
        /**
         * A factory for our Villages.
         * 
         * @return Village
         */
        public static function hillbilly() {
            return new Village;
        }
    
        /**
         * Name tells the story...
         *
         * Now somewhere at this exact point I want to mention the
         * $mention property -- @property Village::mention does not work
         * nor does @property $mention either...
         *
         * @return array Rednecks unset.
         */
        public function redneck() {
            if(sizeof($data)) {
                unset($data);
            }
    
            return $data;
        }
    }
    
    $countryside = [
        'important' => 'data',
        'axe' => 'knifes',
        'shovel' => 'hoe',
        'trowel' => 'mixer',
    ];
    
    $village = Village::hillbilly($countryside);
    

    如何在PHPDoc中提及属性?

    1 回复  |  直到 7 年前
        1
  •  3
  •   dops    7 年前

    如果你需要 $mention 在docblock文本中,通常使用内联see {@see element description} :

    /**
     * Name tells the story...
     *
     * Now somewhere at this exact point I want to mention the
     * {@see Village::$mention} property.
     *
     * @return array Rednecks unset.
     * @see Village::$mention
     * @uses Village::$mention
     */
    public function redneck() {
        if(sizeof($data)) {
            unset($data);
        }
    
        return $data;
    }
    

    这个 @see @uses 也可以使用独立标记,但不用于将链接嵌入docblock叙述文本。

    请注意,较旧的phpDocumentor只允许inlink链接标记 {@link url|element description} .