什么';php注释中@var的含义


What's the meaning of @var in php comments

我在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>

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

数据类型应该是有效的PHP类型(int、string、bool等)对象类型的类名;混合";。php文档将显示未修改的可选描述,并默认为"混合的";如果数据类型不存在

https://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.var.pkg.html

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

@var标签可能有多行描述,不需要显式定界。

在记录时,建议将此标签与所有物

此标记在PHPDoc中的每个属性不得出现一次以上,并且仅限于类型属性的结构元素。

示例:

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();
}

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

它在内联类型提示中。

例如

/* @var bool */
$switch

在这种情况下,这意味着$thisCategoriesController类型,而$dataCategories 类型

IDE经常用于类型提示。

这就是phpdoc,用于制作自动化文档:

phpdoc

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

/**
 * @var MyDto[]
 */