Home >Backend Development >PHP Tutorial >PDP Document Code Comment Specification
1. What is phpDocumentor?
PHPDocumentor is a tool written in PHP. For PHP programs with standard annotations, it can quickly generate API documents with cross-reference, index and other functions. The old version is phpdoc. Starting from 1.3.0, it has been renamed phpDocumentor. The new version adds support for php5 syntax. At the same time, documents can be generated by operating on the client browser, and the documents can be converted to PDF, HTML, There are several forms of CHM, which are very convenient.
When PHPDocumentor works, it will scan the PHP source code under the specified directory, scan the keywords, intercept the comments that need to be analyzed, then analyze the special tags in the comments, generate an xml file, and then based on the analyzed classes and modules Information, establish corresponding index, and generate xml files. For the generated xml files, use customized templates to output files in the specified format.
2. Install phpDocumentor
Like other modules under pear, the installation of phpDocumentor is also divided into two methods: automatic installation and manual installation. Both methods are very convenient:
a. Automatically install through pear
Enter
pear install PhpDocumentor
b. Manual installation
Download the latest version of PhpDocumentor (now 1.4.0) at http://manual.phpdoc.org/ and unzip the content.
3. How to use PhpDocumentor to generate documents
Command line method:
In the directory where phpDocumentor is located, enter
Php –h
and you will get a detailed parameter list. Several important parameters are as follows:
-f The name of the file to be analyzed, more Each file is separated by commas
-d The directory to be analyzed, multiple directories are separated by commas
-t The storage path of the generated document
-o The output document format, the structure is output format: converter name: template directory.
For example: phpdoc -o HTML:frames:earthli -f test.php -t docs
Web interface generation
In the new phpdoc, in addition to generating documents under the command line, you can also generate documents on the client browser , the specific method is to first put the content of PhpDocumentor in the apache directory so that it can be accessed through the browser. After access, the following interface will be displayed:
Click the files button, select the php file or folder to be processed, and you can also specify the Files to ignore under the interface to ignore the processing of certain files.
Then click the output button to select the storage path and format of the generated document.
Finally click create, and phpdocumentor will automatically start generating the document. The progress and status of the generation will be displayed at the bottom. If successful, it will display
Total Documentation Time: 1 seconds
done
Operation Completed!!
Then, we can view the generated document. If it is in pdf format, the name defaults to documentation.pdf.
4. Add standardized comments to PHP code
PHPDocument generates documents from the comments of your source code, so the process of commenting on your program is also the process of compiling documentation.
From this point of view, PHPdoc encourages you to develop good programming habits and try to use specifications and clear text to annotate your program. At the same time, it more or less avoids some problems of out-of-synchronization between document preparation and document updates afterwards. .
In phpdocumentor, comments are divided into documentation comments and non-documentation comments.
So-called documentation comments are multi-line comments placed in front of specific keywords. Specific keywords refer to keywords that can be analyzed by phpdoc, such as class, var, etc. For details, please refer to Appendix 1.
Those that are not in the key Comments that precede the word or are not standardized are called non-documentation comments. These comments will not be analyzed by phpdoc and will not appear in the API document you generate.
3.2 How to write documentation comments:
All documentation comments are a multi-line comment starting with /**, which is called DocBlock in phpDocumentor. DocBlock refers to help information about a certain keyword written by software developers. , so that others can know the specific purpose of this keyword and how to use it. PhpDocumentor stipulates that a DocBlock contains the following information:
1. Function brief description area
2. Detailed description area
3. Mark tag
The first line of the documentation comment is the function description area, and the text generally describes the class and methods concisely Or the function of the function, the text of the function brief description will be displayed in the index area in the generated document. The content of the function description area can be ended by a blank line or.
After the function description area is a blank line, followed by a detailed description area. This part mainly describes the function and purpose of your API in detail, and if possible, also You can give examples of usage, etc. In this section, you should focus on clarifying the general purpose and usage of your API functions or methods, and indicate whether it is cross-platform (if involved). For platform-related information, you should treat it differently from general information. , the usual approach is to start a new line, and then write Notes or special information on a specific platform. This information should be enough so that your readers can write corresponding test information, such as boundary conditions, parameters ranges, breakpoints, etc.
After that, there is also a blank line, and then the document tag, indicating some technical information, mainly the call parameter type, return value and type, inheritance relationship, related methods/functions, etc.
Regarding document marking, please refer to Section 4: Document Marking for details.
You can also use tags such as in document comments. Please refer to Appendix 2 for details.
The following is an example of a documentation comment
/**
* Function add, implements the addition of two numbers
*
* A simple addition calculation, the function accepts two numbers a, b, and returns their sum c
*
* @param int The addend
* @param int is Addend
* @return integer
*/
function Add($a, $b)
{
return $a+$b;
}
The generated documentation is as follows:
Add
integer Add( int $a, int $b)
[line 45]
Function add, implements the addition of two numbers
Constants A simple addition calculation, the function accepts two numbers a, b, and returns their sum c
Parameters
? int $ a - addend
? int $b - addend
5. Document tags:
The scope of use of document tags refers to the keywords or other document tags that the tag can be used to modify.
All documentation tags start with @ after * on each line. If the @ mark appears in the middle of a paragraph, the mark will be treated as normal content and ignored.
@access
Usage scope: class, function, var, define, module
This tag is used to indicate the access permission of keywords: private, public or protected
@author
Indicate the author
@copyright
Usage scope: class, function , var, define, module, use
Indicate copyright information
@deprecated
Usage scope: class, function, var, define, module, constent, global, include
Indicate unused or obsolete keywords
@example
This tag Used to parse a file content and highlight them. Phpdoc will try to read the file content from the file path given by this tag @const
Use scope: define
Used to specify the constants defined in php
@final
Use scope: class, function, var
Specify keywords It is a final class, method, and attribute, and is prohibited from being derived or modified.
@filesource
Similar to example, except that this tag will directly read the content of the currently parsed php file and display it.
@global
Indicates the
global variable referenced in this function@ingore
is used to ignore the specified keyword in the document
@license
Equivalent to in the html tag, first the URL, then The content to be displayed
For example
Baiducan be written as @license http://www.baidu.com
Baidu @link
Similar to license
but you can also point to any keyword in the document through link
@name
Specify an alias for the keyword.
@package
Usage scope: page level -> define, function,
include Class level -> class, var, methods
Used to logically group one or several keywords into a group.
@abstrcut
Indicates that the current class is an abstract class
@param
Indicates the parameters of a function
@return
Indicates the return pointer of a method or function
@static
Indicates that the keyword is static.
@var
Indicate the variable type
@version
Indicate the version information
@todo
Indicate areas that should be improved or not implemented
@throws
Indicate the error exceptions that this function may throw, and the extreme situations
As mentioned above, Ordinary document tags must be marked with @ at the beginning of each line. In addition, there is also a tag called inline tag, represented by {@}, which includes the following types:
{@link}
Usage is the same as @link
{@source}
Display the content of a function or method
6. Some comment specifications
a. Comments must be in the form
/**
* XXXXXXX
*/
b. For functions that reference
global variables, glboal tags must be used. c. For variables, their types (int, string, bool...) must be marked with var
d. Functions must indicate their parameters and return values through param and return markers
e. For variables that appear twice or more Keywords should be ignored through ingore and only one should be kept. Where other functions or classes are called, link or other tags should be used to link to the corresponding part to facilitate the reading of the document.
g. Use non-documentation comments where necessary to improve code readability.
h. Keep descriptive content concise and to the point, using phrases rather than sentences whenever possible.
i.
Global variables
, static variables and constants must be described with corresponding tags7. SummaryphpDocumentor is a very powerful automatic document generation tool. It can help us write standardized comments and generate easy-to-understand structures. Clear documentation is very helpful for our code upgrades, maintenance, handover, etc.
For more detailed instructions about phpDocumentor, you can check it out on its official website
http://manual.phpdoc.org/
8. Appendix
Appendix 1:
Keywords that can be recognized by phpdoc:
include
require
include_once
require_once
define
function
global
class
Appendix 2
Tags that can be used in the document
Appendix 3:
A piece of php code with canonical comments
/**
* Sample File 2, phpDocumentor Quickstart
*
* This file demonstrates the rich information that can be included in
* in-code documentation through DocBlocks and tags.
* @author Greg Beaver
* @version 1.0
* @package sample
*/
// sample file #1
/**
* Dummy include value, to demonstrate the parsing power of phpDocumentor
*/
include_once 'sample3.php';
/ **
* Special global variable declaration DocBlock
* @global integer $GLOBALS['_myvar']
* @name $_myvar
*/
$GLOBALS['_myvar'] = 6;
/**
* Constants
*/
/**
* first constant
*/
define('testing', 6);
/**
* second constant
*/
define('anotherconstant', strlen('hello'));
/**
* A sample function docblock
* @global string document the fact that this function uses $_myvar
* @staticvar integer $staticvar this is actually what is returned
* @param string $param1 name to declare
* @param string $param2 value of the name
* @return integer
*/
function firstFunc($param1, $param2 = 'optional')
{
static $staticvar = 7;
global $_myvar;
return $staticvar;
}
/**
* The first example class, this is in the same package as the
* procedural stuff in the start of the file
* @package sample
* @subpackage classes
*/
class myclass {
/**
* A sample private variable, this can be hidden with the --parseprivate
* option
* @access private
* @var integer|string
*/
var $firstvar = 6;
/**
* @link http://www.example.com Example link
* @see myclass()
* @uses testing, anotherconstant
* @var array
*/
var $secondvar =
array(
'stuff' =>
array(
6,
17,
'armadillo'
),
testing => anotherconstant
);
/**
* Constructor sets up {@link $firstvar}
* /
function myclass()
{
$this->firstvar = 7;
}
/**
* Return a thingie based on $paramie
* @param boolean $paramie
* @return integer|babyclass
*/
function parentfunc($paramie)
{
if ($paramie) {
return 6;
} else {
return new babyclass;
}
}
}
/**
* @package sample1
*/
class babyclass extends myclass {
/**
* The answer to Life, the Universe and Everything
* @var integer
*/
var $secondvar = 42;
/**
* Configuration values
* @var array
*/
var $thirdvar;
/**
* Calls parent constructor, then increments {@link $firstvar}
*/
function babyclass()
{
parent::myclass();
$this->firstvar++;
}
/**
* This always returns a myclass
* @param ignored $paramie
* @return myclass
*/
function parentfunc($paramie)
{
return new myclass;
}
}
?>
The above introduces the PDP Document code comment specifications, including require, include, global variables, precautions, and Baidu content. I hope it will be helpful to friends who are interested in PHP tutorials.