HOW TO commenter ses fichiers PHP... avec PHPDoc!

Documenter ses fichiers --  La structure des commentaires dans un document
@author --  décrit l'auteur
@const --  décrit les constantes crées avec la commande define()
@deprecated --  signale les éléments obsolètes de l'API
@global --  documente les variables globales utilisées
@package --  regroupe les classes en packages
@param --  décrit les paramètres d'une fonction
@see --  marque les références à d'autres éléments ou méthodes
@since --  indique la date à laquelle l'élément a été introduit
@static --  indique que la fonction est static
@var --  décrit les variables de la classe
     Sont indiqués en gras les éléments dont vous devez absolument vous servir...
La syntaxe est propre à chaque marqueur. Cela n'est pas à connaître par coeur mais vous devez vous y référer à chaque fois que vous codez.

Documenter ses fichiers

Format des commentaires d'un document

The general format of a doc comment is independent of the element to describe. Make sure that there is nothing between the comment and the element, except blank lines.

/**
* Short description - one line
*
* Detailed description (optional)
* over several lines, the blank lines
* between the description parts are
* not necessary
*
* @keyword parameters_of_keyword
* @keyword the sequence of keywords arbitrary
*/

[element to describe]

Types of elements

Short description part

This line should describe what an element contains or does. Example for a variable: "Count of articles", for a function: "Creates a hyperlink" or for a class: "provides PDF-related functions". This line will normally be rendered in an index page or table of content.

Note: A 'line' means everything between the leading *- character and a newline character.

Detailed description part

In this part, you can write more about your element. Please note that parameters, return values and relationships between elements a covered by the keyword section. Some HTML-tags are allowed here:

But take care, this tags maybe ignored by non-html PHPDoc renderer.

Keyword part

The format of a keyword line is simple

* @keyword <parameters>

Not every keyword is allowed for every element. See the scope section where a keyword is permitted and where not.

Example

/**
* Basket class for my web shop.
*
* Basket is a repository for instances of
* the class Article...
*
* @author Alexander Merz <alexander.merz@php.net>
* @version 1.6
* @access public
* @package MyWebShop
*/
class Basket {

    /**
    * contains the articles
    * @var array
    * @see add(), mod(), del()
    */
    var $articles = array() ;

    /**
    * Adds an article.
    *
    * An instance of the given article and the count is
    * stored in the basket
    *
    * @param object Article $article article to add
    * @param integer        $count   count of the article
    * @return boolean returns false, if error occurs
    * @access public
    * @see $articles
    */
    function add( $article, $count = 1) {
        ...
    }
}

@author

@author --  describes the author

Synopsis

@author { Name } [ <email> ]

Scope

Example

...
* @author The invisible man
* @author Alexander Merz <alexmerz@php.net>
...

@const

@const --  describes constants created with define()

Synopsis

@const { label } [ description ]

Scope

Example

...
/**
* maximum of permitted clients at the same time
* @const  MAX_CLIENTS  maximum clients
*/
define("MAX_CLIENTS", 1000, true);

/**
* maximum request per client
* @const  MAX_REQUESTS_PER_CLIENT
*/
define("MAX_REQUESTS_PER_CLIENT", 5);

...


@deprecated

@deprecated --  markup outdated API elements

Synopsis

@deprecated { label }

Scope

Example

Note that the format of label is not defined by PHPDoc.

...
* @deprecated Rev. 1.8 - 03/10/2001
...
* @deprecated 03-10-2001
...


@global

@global --  document global used variables

Synopsis

@global { type|objectdef } { $varname } [ description ]

Scope

Example

...
* @global  array   $files   list of file
* @global  integer $errcode errorcode
* @global  string  $errmsg  error message
* @global  object FileSystem $fs instance of the FS class
*/
function fooBar() {
  global $files, $errcode, $errmsg, $fs;

...


@package

@package --  group classes

Synopsis

@package { label }

Scope

Example

...
* @package Net
...

@param

@param --  describes a parameter of a function

Synopsis

@param { type|objectdefinition } { $varname } [ description ]

Scope

Note

PHPDoc detects default values and document them from source.

Example

...
* @param string  $name    name of the person
*                         (complete name!)
* @param integer $age     age of the person
* @param float   $size    size of person
* @param object Adresse   $adr adresse of the person
* @param array   $childs  names of the children
* @param boolean $job     if true, person has a job
* @param boolean $gender  use FEMALE
*                         or MALE
*/
function person($name, $age, $size, $adr, $childs, $job=true, $gender=FEMALE) {
...


@see

@see --  mark up reference to other elements

Synopsis

@see { element }

Scope

Example

...
* @see $article, Article::edit()

* @see delete(), print()
...


@since

@since --  mark up the time as an API elements was introduced

Synopsis

@since { label }

Scope

Example

Note that the format of label is not defined by PHPDoc.

...
* @since Rev. 1.8 - 03/10/2001
...
* @since 03-10-2001
...

@static

@static --  marks a function as static

Synopsis

@static

Scope

Example

...
class Foo {
 /**
 ...
 * @static
 */
 function Bar (...
...

// this call is permitted according to the docs:
Foo::Bar() ;

@var

@var --  describes class variables

Synopsis

@var { type|objectdefinition } { $varname } [ description ]

Scope

Note

If no type is given, PHPDoc tries to guess.

Example

...
* @var string  $name    name of the person
*/
var name = "" ;
...

Sources

PEAR : http://pear.php.net/manual/en/packages.phpdoc.php


Valid XHTML 1.0! The end!