It is a good practice to write a documentation comment above the function or class to explain it’s functionality to other developers. In the future, if we need to modify the function, we can easily understand the reason for creating that function without checking the entire code and modify it according to the application requirements. WordPress core classes provides the best example of the PHP documentation comment. Here is an example of a WordPress core class.
![Write php documentation comment](https://bluewindlab.net/wp-content/uploads/2024/07/php_documentation_comment.png)
PHP documentation comment structure
Here is the basic structure of PHP documentation comments for class and function, and we can use it in our PHP code.
Class documentation comment
/** * Default strings and translation support for those. * @package your-package-name * @version 1.0.0 */
Explanation
- The first line is for explaining the functionality of the class.
- @package is for the class package name.
- @version is for describing the class version.
Function documentation comment
/** * Default strings and translation support for those. * @since 0.1.0 * @param string $page_title The page title, with extra context added. * @param string $title The original page title. * @return string */
Explanation
- The first line is for explaining the functionality of the function.
- @since is for the version number to which the function has been added.
- @param is for describing function parameters and types.
- @return is the return type of the function.