The goal of this tutorial is to introduce you in writing and subsequently generating effective documentation with phpDocumentor.
A DocBlock is a piece of inline documentation in your source code that informs you what a class, method or other Structural Element its function is.
Before we discuss what a DocBlock looks like, let’s first zoom into what you can document with them. phpDocumentor follows the PHPDoc definition and recognizes the following Structural Elements:
In addition to the above the PHPDoc standard also supports DocBlocks for Files and include/require statements, even though PHP itself does not know this concept.
Each of these elements can have exactly one DocBlock associated with it, which directly precedes it. No code or comments may be between a DocBlock and the start of an element’s definition.
DocBlocks are always enclosed in a comment-type, called DocComment, that starts with /** and ends with */. Each line in between the opening and closing statement should start with an asterisk (*). Every DocBlock precedes exactly one Structural Element and all contents of the DocBlock apply to that associated element.
For example:
<?php
/**
* This is a DocBlock.
*/
function associatedFunction()
{
}
Note
File-level DocBlocks
Quite often projects will want to document the license or function for an entire file instead of a single element. This can be accomplished by having a DocBlock as the first element encountered in a file. It is important to note that whenever another Structural Element directly follows the DocBlock that it is no longer recognized as a File-level DocBlock but belonging to the subsequent element.
The following DocBlock is a File-level DocBlock:
<?php
/**
* I belong to a file
*/
/**
* I belong to a class
*/
class Def
{
}
However in the following example the DocBlock belongs to the class:
<?php
/**
* I belong to a class
*/
class Def
{
}
DocBlocks are divided into the following three parts. Each of these parts is optional, except that a Description may not exist without a Summary.
Sometimes called a short description, provides a brief introduction into the function of the associated element. A Summary ends in one of these situations:
- A dot is following by a line break, or
- Two subsequent line breaks are encountered.
A DocBlock looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php
/**
* A summary informing the user what the associated element does.
*
* A *description*, that can span multiple lines, to go _in-depth_ into the details of this element
* and to provide some background information or textual references.
*
* @param string $myArgument With a *description* of this argument, these may also
* span multiple lines.
*
* @return void
*/
function myFunction($myArgument)
{
}
|
Let’s go through this example line by line and discuss which is which,
If you’d like to know more about what DocBlocks do for you, visit the chapter Inside DocBlocks for more in-depth information.
After you have installed phpDocumentor you can use the phpdoc command to generate your documentation.
In this document we expect that the phpdoc command is available; thus whenever we ask you to run a command it would be in the following form:
$ phpdoc
Hint
When you have installed a version via composer or manually you should invoke the phpdoc script in the bin folder of your phpDocumentor installation.
Under Linux / MacOSX that would be:
$ [PHPDOC_FOLDER]/bin/phpdoc
And under Windows that would be:
$ [PHPDOC_FOLDER]\bin\phpdoc.bat
The basic usage of phpDocumentor is to provide an input location using the command line options (-d for a directory, -f for a file) and tell it to output your documentation to a folder of your liking (-t).
For example:
$ phpdoc -d ./src -t ./docs/api
What the above example does is scan all files in the src directory and its subdirectories, perform an analysis and generate a website containing the documentation in the folder docs/api. If you want you can even omit the -t option, in which case the output will be written to a subfolder called output.
Hint
phpDocumentor features several templates with which you can change the appearance of your documentation. See the chapter Changing the Look and Feel for more information on how to switch between templates.
There are a lot more options to phpDocumentor and you can define them all in a Configuration file and include that in your project but that is out of scope for this tutorial. If you’d like to know more on running phpDocumentor; see the guide on Running phpDocumentor for more information.