Introduction to PhpDoc
Core points
- PhpDoc (PhpDocumentor) is a powerful tool that helps developers write code documents through special format annotations. It can generate documents in multiple formats, such as HTML, PDF, and CHM, which can be extracted through a web interface or command line interface.
- PhpDoc uses DocBlocks (multi-line C-style comments) to document code blocks. DocBlocks contains three optional parts: a short description, a detailed description, and a tag. The tag begins with the
@
symbol, which specifies additional information about the code. - The PhpDoc package is used to group relevant code elements in the generated document. You can specify packages for files and classes using the
@package
and@subpackage
tags in the file-level or class-level DocBlock. - PhpDoc can write documents for various code elements, including files, classes, functions and methods, class properties, global variables,
include()/require()
anddefine()
. These elements can use certain common tags, but each has a specific tag. - PhpDoc's command line tool is used to generate user-friendly documents based on PHP code that has been written. This tool offers a variety of document formats. For users who are not familiar with the command line interface, PhpDoc also provides a web interface.
Reading code written by others (who hasn't experienced it?) is a difficult task. The messy "pasta-style code" is mixed with a large number of weirdly named variables, making it dizzy. Does this function expect strings or arrays? Does this variable store integers or objects? After countless hours of code tracking and trying to understand the functionality of each part, it is common to give up and rewrite the entire code from scratch – it’s a waste of your precious time. PhpDoc (the short name for PhpDocumentor) is a powerful tool that allows you to easily write code documents with comments in special formats. Documents are not only available in source code, but also professional documents extracted through the web interface or command line interface. The result can be in a variety of formats such as HTML, PDF, and CHM. In addition, many IDEs that provide code completion can parse PhpDoc comments and provide practical features such as type prompts. By using PhpDoc, you can make it easier for others (and yourself) to understand your code—even after weeks, months, or even years after writing it. The easiest way to install PhpDoc is to use PEAR. Of course, PEAR must be installed before you do so. If you do not have PEAR installed, follow the instructions at pear.php.net/manual/en/installation.php. In this article, I will show you how to generate beautiful and user-friendly documents from beginning to end with PhpDoc.
DocBlocks
DocBlock is a multi-line C-style comment used to write documents for code blocks. It starts with /**
and each line has an asterisk. Here is an example:
<?php /** * 计算数组中每个元素的平方和 * * 循环遍历数组中的每个元素,将其平方,并将其添加到总和中。返回总和。 * * 此函数也可以使用 array_reduce() 实现; * * @param array $arr * @return int * @throws Exception 如果数组中的元素不是整数 */ function sumOfSquares($arr) { $total = 0; foreach ($arr as $val) { if (!is_int($val)) { throw new Exception("Element is not an integer!"); } $total += $val * $val; } return $total; }
DocBlocks contains three parts: a short description, a detailed description, and a label. All three parts are optional. A short description is a concise description that ends with a newline or period. PhpDoc's analytical routine is smart; it ends with a short description only when the period is at the end of the sentence. A detailed description is the main content of a document; it can be multi-line and can be arbitrarily long. Both detailed descriptions and short descriptions can contain certain HTML elements for formatting. Unsupported HTML tags will be displayed as plain text. PhpDoc can generate documents in multiple formats, so HTML tags do not necessarily render as they do in HTML files; the actual format depends on the format of the generated document. If you need to display HTML tags as text, use double brackets. For example:
<?php /** * 这里是斜体标签的示例: >Hello, world!> */
DocBlock contains any number of special tags represented by the @
symbol. Tags are used to specify additional information, such as expected parameters and their types. Most tags must be on their own rows, but some tags can be inlined. Inline tags are enclosed in curly braces and can appear in detailed descriptions and brief descriptions. For a complete list of tags, check out the relevant PhpDoc documentation. If you need a line to start with the @
symbol but don't want to interpret it as a label, you can escape it with a backslash. PhpDoc will automatically identify and parse the text list in the detailed description and the short description. However, it does not parse nested lists correctly. If you want to use nested lists, use HTML tags. Here is an example to illustrate what I mean:
<?php /** * 使用列表的示例 * * PhpDoc 将正确解析此列表: * - 项目 #1 * - 项目 #2 * - 项目 #3 * * 但不是这个列表: * - 项目 1 * - 项目 1.1 * - 项目 1.2 * - 项目 2 * * 请改用此方法创建嵌套列表: *
(The following content will be briefly summarized due to space limitations and retained key information)
Bag
The PhpDoc package is used to group relevant code elements in the generated document. You can specify packages for files and classes that contain code written to inherit those packages. To specify a package, set the @package
tag in the file-level or class-level DocBlock. (File-level and class-level DocBlocks will be discussed further in the next section). Package names can contain letters, numbers, dash, underscores, and square brackets ("[" and "]"). Here is an example of how to define a file package:
<?php /** * 这是一个文件级 DocBlock * * @package Some_Package */
If you have multiple levels of packages and subpackages, you can use the @subpackage
tag to define subpackages. Here is an example:
<?php /** * 这是一个类级 DocBlock * * @package Some_Package * @subpackage Other */ class SomeClass { }
If the file or class does not specify a package, it will be set to the default package "default". You can specify other packages to use by default through the -dn
command line option.
What documents can be written?
Not all code elements can be written using DocBlocks. Here is a list of code elements that can be written in the document:
- File
- Category
- Functions and methods
- Class attributes
- Global variables
include()/require()
define()
All of these elements can use certain common labels, but each element has a label specific to that element. I'll cover some elements and tags that are usually used to write documentation for them.
(The documentation examples of files, classes, functions and methods will be brief, only key tag descriptions will be retained)
Generate document
After writing the documentation for your PHP code, you need to generate user-friendly documents from it. To do this, run the PhpDoc command line tool.
<?php /** * 计算数组中每个元素的平方和 * * 循环遍历数组中的每个元素,将其平方,并将其添加到总和中。返回总和。 * * 此函数也可以使用 array_reduce() 实现; * * @param array $arr * @return int * @throws Exception 如果数组中的元素不是整数 */ function sumOfSquares($arr) { $total = 0; foreach ($arr as $val) { if (!is_int($val)) { throw new Exception("Element is not an integer!"); } $total += $val * $val; } return $total; }
(The command line parameter description will be brief)
For users who are not familiar with the command line interface, PhpDoc also provides a web interface. This document does not discuss it in detail, but you can learn more on PhpDoc's official website, phpdoc.org.
Summary
In this article, I introduce you to PhpDoc and its many powerful features. I've explained the purpose of DocBlocks and its components; I've shown you how to use packages to organize your documentation; I've explained which code elements can be written and some common examples; and finally, I've shown you how to generate documentation based on your source code. I highly recommend that you start using PhpDoc in your own project, even if it’s just writing the documentation for the most important parts. It's very simple and can save you and your colleagues countless hours of tension and pain.
(The FAQ section will be brief, retaining core questions and short answers)
The above is the detailed content of Introduction to PhpDoc. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
