Tomazi
Tomazi

Reputation: 847

Symfony2 comments above variables and functions

I begun learning Symfony2 one of the things that struck me are the numerous comments above variables and functions like so:

variables example:

    /**
 * @var string
 */
private $baseUrl = 'news/';

OR:

/**
 * @var FeedRepository
 */
private $feedRepo;

Functions:

/**
 * @param ArticleQuery $query
 * @return QueryBuilder
 */
public function test(){


}

Could someone explain to me why use these and why do the do ..?

Upvotes: 0

Views: 480

Answers (1)

Francesco Casula
Francesco Casula

Reputation: 27130

PHPDoc is an adaptation of Javadoc for the PHP programming language. It is still an informal standard for commenting PHP code, but it is in the progress of being formalized. It allows external document generators like phpDocumentor to generate documentation of APIs and helps some IDEs such as Zend Studio, NetBeans, JetBrains PhpStorm, ActiveState Komodo Edit and IDE, PHPEdit and Aptana Studio to interpret variable types and other ambiguities in the loosely typed language and to provide improved code completion, type hinting and debugging.

Try for example to use an IDE like PHPStorm and write the following PHP code:

/**
 * @param array $test
 */
function mytest($test) {
    // ... nothing here
}

Now when you start typing myte you get the autocompletion to suggest mytest(test : array). As you can see now PHPStorm knows the type of the $test variable thanks to the PHPDoc above.

Upvotes: 5

Related Questions