php php注释中@var是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12158987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:58:03  来源:igfitidea点击:

What's the meaning of @var in php comments

php

提问by eric

I see this (@var) in php comments a lot and have no clue what it means. Please tell.

我经常在 php 评论中看到这个(@var),但不知道它是什么意思。请告诉。

// example.php (taken from yii framework application code)

<?php
/* @var $this CategoriesController */
/* @var $data Categories */
?>

<div class="view">

    <b><?php echo CHtml::encode($data->getAttributeLabel('idCategory')); ?>:</b>
    <?php echo CHtml::link(CHtml::encode($data->idCategory), array('view', 'id'=>$data->idCategory)); ?>
    <br />
</div>

回答by CosminO

You may use the @var tag to document the data type of class variables.

The datatype should be a valid PHP type (int, string, bool, etc), a class name for the type of object, or simply "mixed". phpDocumentor will display the optional description unmodified, and defaults to "mixed" if the datatype is not present

您可以使用@var 标记来记录类变量的数据类型。

数据类型应该是有效的 PHP 类型(int、string、bool 等)、对象类型的类名,或者只是“混合”。phpDocumentor 将显示未修改的可选描述,如果数据类型不存在,则默认为“混合”

http://www.phpdoc.org/docs/latest/references/phpdoc/tags/var.html

http://www.phpdoc.org/docs/latest/references/phpdoc/tags/var.html

With the @var tag it is possible to document the type and function of a class property. When provided it MUST contain a Type to indicate what is expected; the description on the other hand is OPTIONAL yet RECOMMENDED in case of complicated structures, such as associative arrays.

The @var tag MAY have a multi-line description and does not need explicit delimiting.

It is RECOMMENDED when documenting to use this tag with every property.

This tag MUST NOT occur more than once per property in a PHPDoc and is limited to Structural Elements of type property.

使用@var 标签可以记录类属性的类型和功能。当提供时,它必须包含一个类型来指示预期的内容;另一方面,在复杂结构(例如关联数组)的情况下,描述是可选的但建议使用。

@var 标签可以有一个多行描述并且不需要显式分隔。

建议在记录时将此标签用于每个属性。

这个标签不能在 PHPDoc 中的每个属性出现超过一次,并且仅限于类型属性的结构元素。

Example:

例子:

class DemoVar
{
   /**
    * Summary
    *
    * @var object Description
    */
   protected $varWithDescriptions;

   /**
    * @var \DemoVar $instance The class instance.
    */
   protected static $instance;

   /**
    * Summary for varWithWrongType
    *
    * @var boolean The varWithWrongType. Boolean will be put in the type.
    */
   protected $varWithWrongType = array();
}

回答by Lusitanian

They are PHPdoc comments and are generally used for IDE-typehinting/code completion (also sometimes documentation-generation, but not in this scenario). They have no relevance on the application itself and can be removed without incident.

它们是 PHPdoc 注释,通常用于 IDE 类型提示/代码完成(有时也用于文档生成,但不是在这种情况下)。它们与应用程序本身无关,可以毫无意外地删除。

回答by George Reith

It is in an inline type hint.

它位于内联类型提示中。

e.g.

例如

/* @var bool */
$switch

In this case it means $thisis of type CategoriesControllerand $datais of type Categories

在这种情况下,它意味着$this是类型CategoriesController并且$data是类型Categories

Often used by IDEs for type hinting.

通常由 IDE 用于类型提示。

回答by Sune Trudslev

That is phpdoc, to make automated documentation:

也就是说phpdoc,要制作自动化文档:

phpdoc

文档

回答by jfx

This is how it needs to be specified for arrays:

这是需要为数组指定的方式:

/**
 * @var MyDto[]
 */