Getting Started With PHPDoc

26th January 2009 - 5 minutes read time

If you followed the tutorial on installing PHPDoc then you are probably wondering how to get started actually writing the documentation. PHPDoc will parse all the files given to it and look for comment blocks, it will then use these blocks to create the documentation for your application. A valid PHPDoc comment block must start with a '/**', have a '*' on every line and finish with '*/'. The comment must appear before the section that is being commented upon and any lines within the comment without a '*' will be ignored by the parser.

To create some sample documentation we will use the following useless class.

  1. <?php
  2. include('another_file.php');
  4. class MyClass
  5. {
  7. public $publicVariable = 123;
  8. private $privateVaraible;
  10. public function aFunciton($parameter)
  11. {
  12. // some code here...
  13. }
  14. }

The first thing we need to do is create a file comment, this comment is intended to tell people what this file contains. In this example the file contains a single class definition and an include statement.

  1. /**
  2.  * MyClass File (short description about the file)
  3.  *
  4.  * This file contains the definition of the file MyClass (a longer description about the file).
  5.  * @author A.N. Other
  6.  * @version 1.0
  7.  * @package MyPackage
  8.  */

Notice the use of the @ symbol. This is called a tag and allows PHPDoc to assign values to internal variables. So when we use @version, we are setting the version variable for our file.

define() statements, functions, classes, class methods, and class vars, include() statements, and global variables can all be documented. So the next step here is to document what the include statement includes. PHPDoc will attempt to find any files referenced through include or require statements and generate documentation for those files. If present, a link will be created from this file to that file in the documentation.

  1. /**
  2.  * Include the another_file.php file.
  3.  */
  4. include('another_file.php');

Next is our class definition, which consists of two forms of description(short and long) as well as some tags relating to things like what package the class is in.

  1. /**
  2.  * This is a short description about the class.
  3.  *
  4.  * This is a longer description which should detail exactly what the class does.
  5.  * @package test
  6.  * @author A.N. Other
  7.  * @version 1.0
  8.  * @copyright A copyright notice, if any.
  9.  */
  10. class MyClass
  11. {
  12. }

The two variables within the class must also be documented.

  1. /**
  2.  * A description of the variable.
  3.  * @var integer
  4.  */
  5. public $publicVariable = 123;
  7. /**
  8.  * This variable will only appear in our documentation is we set the --parseprivate attribute on the command line or it has been set up in the INI file.
  9.  * @access private
  10.  * @var string
  11. */
  12. private $privateVaraible;

Finally, we document the function. The functions are documented using the two tags of @param and @return. The @param can be defined multiple times and is a descriptive list of the parameters that the function takes. The @return value can only be defined once and describes what the function returns. If more than one datatype is returned from the function then they can be separated by a pipe (|).

  1. /**
  2.  * This class either returns an integer or a string.
  3.  * @param A description about the parameter $parameter
  4.  * @return string|int This function returns a string or an integer.
  5.  */
  6. public function aFunciton($parameter)
  7. {
  8. // some code here...
  9. }

To add more than one parameter just add another line in that starts with @param.

You can run PHPDoc on this file by using the following command (assuming that your project is kept in a folder called "MyProject" and that you want the documentation to go in "docs" within that directory.

C:\MyProject>phpdoc -t "C:\MyProject\docs" -o HTML:frames:default -d "C:\MyProject"

Add new comment

The content of this field is kept private and will not be shown publicly.