Introduction: Why Documentation is Not a Luxury, but a Necessity
Every PHP developer has encountered a situation where someone else's (or even their own) code turns into a "dark forest" after a month. Lack of documentation is one of the main reasons why projects slow down and bugs multiply. Documenting code is not just a courtesy to colleagues, it is an investment in the project's future. In this article, we will break down how to properly write documentation for a PHP project using the PHPDoc standard, automation tools, and best practices.
1. PHPDoc Basics: Structure and Syntax
PHPDoc (or phpDocBlock) is a standard for commenting code in PHP that allows you not only to describe the purpose of a function or class but also to automatically generate documentation. Each documentation block begins with the characters /** and ends with */.
Basic DocBlock Structure
Let's look at a minimal block for a function:
<?php
/** * Calculates the sum of two numbers * * @param int $a The first addend * @param int $b The second addend * @return int The sum of the numbers */function sum(int $a, int $b): int { return $a + $b;}Key elements:
- Short description — the first line after the opening comment. It should be concise and clear.
- Long description (optional) — can follow after a blank line. Allows explaining complex algorithms.
- Tags — start with the
@symbol. Main ones:@param,@return,@throws.
Documenting Classes and Properties
Similar blocks are used for classes and properties:
<?php
/** * Class for working with users * * Provides methods for creating, updating, and deleting users. * All operations undergo validation and logging. */class UserManager {
/** * @var PDO|null Database connection object */ private $db;
/** * @var LoggerInterface Logger instance */ private $logger;
/** * Class constructor * * @param PDO $db PDO object for working with the database * @param LoggerInterface $logger Logger object */ public function __construct(PDO $db, LoggerInterface $logger) { $this->db = $db; $this->logger = $logger; }}2. Main PHPDoc Tags: Complete Reference
To create comprehensive documentation, you need to know the key tags. Here is a list of the most important ones:
Tags for Describing Types and Parameters
@param— describes method/function parameters. Format:@param type $name Description.@return— describes the return value. Format:@return type Description.@var— describes the type of a class property.@throws— indicates which exceptions a method may throw.
Tags for Describing Structure and Metadata
@see— link to another documentation element (class, method).@link— external link (URL).@deprecated— marks a deprecated element.@since— indicates the version from which the element is available.@author— code author.
Complex DocBlock Example
<?php
/** * Gets a list of user orders with pagination * * The method executes a database query using prepared statements. * Returns an array of Order objects. * * @param int $userId User ID * @param int $page Page number (starting from 1) * @param int $limit Number of records per page (max. 100) * @return array Array of Order objects\