What's the meaning of @var in php comments

后端 未结 6 1562
Happy的楠姐
Happy的楠姐 2021-02-18 20:01

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

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



        
相关标签:
6条回答
  • 2021-02-18 20:41

    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.

    0 讨论(0)
  • 2021-02-18 20:42

    This is how it needs to be specified for arrays:

    /**
     * @var MyDto[]
     */
    
    0 讨论(0)
  • 2021-02-18 20:46

    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

    https://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags.var.pkg.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.

    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();
    }
    
    0 讨论(0)
  • 2021-02-18 20:47

    It is in an inline type hint.

    e.g.

    /* @var bool */
    $switch
    

    In this case it means $this is of type CategoriesController and $data is of type Categories

    Often used by IDEs for type hinting.

    0 讨论(0)
  • 2021-02-18 20:51

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

    0 讨论(0)
  • 2021-02-18 20:53

    That is phpdoc, to make automated documentation:

    phpdoc

    0 讨论(0)
提交回复
热议问题