Why declare PHP variable type in a comment?

前端 未结 5 2071
[愿得一人]
[愿得一人] 2021-02-02 09:51

I\'m fairly new to PHP, and I just started using NetBeans to develop my PHP code.

Out of the blue, as I entered a variable in a query, a dialog popped up and asked me to

5条回答
  •  借酒劲吻你
    2021-02-02 10:28

    If you want to declare the type of a variable in the case where the variable is not a class property but just a variable that holds some returned value, use single star comments followed by @var followed by your variable name and finally followed by the type of that variable. For example:

    /* @var $errorMessage NotificationMessage */
    $errorMessage= $allMessages->rewind()->current();
    

    will tell NetBeans or PhpStorm that $errorMessage is an instance of NotificationMessage, and you should get the code completion for that variable.

提交回复
热议问题