☜
☞
documentation
PHP documentation comments, also known as DocBlocks, are used to provide structured documentation for PHP code. These comments are typically used by tools like PHPDoc to generate API documentation automatically. DocBlocks start with /** and end with */, and they can include various tags to describe the code.
Basic Structure
Here's a basic example of a DocBlock for a PHP function:
/**
* Short description of the function.
*
* Longer description of the function, explaining its purpose and behavior.
*
* @param type $paramName Description of the parameter.
* @return type Description of the return value.
*/
function exampleFunction($param) {
// Function code here
}
Common Tags
Here are some of the most commonly used tags in PHP DocBlocks:
- @param - Describes a function parameter. Includes the type and a description.
- @return - Describes the return type and value of the function.
- @throws - Describes exceptions that the function might throw.
- @var - Describes the type of a variable.
- @deprecated - Indicates that the function or method is deprecated and should not be used.
- @see - References another element, like a related function or class.
- @since - Indicates the version of the project when the function was introduced.
- @author - Provides information about the author of the code.
- @version - Provides version information.
Example with Multiple Tags
Here’s a more comprehensive example showing various tags:
/**
* Adds two numbers together.
*
* This function takes two numeric parameters and returns their sum.
*
* @param int $a The first number.
* @param int $b The second number.
* @return int The sum of the two numbers.
* @throws InvalidArgumentException If non-numeric parameters are provided.
* @since 1.0.0
* @see subtract()
*/
function add($a, $b) {
if (!is_numeric($a) || !is_numeric($b)) {
throw new InvalidArgumentException('Both parameters must be numeric.');
}
return $a + $b;
}
Using DocBlocks with Classes and Properties
DocBlocks can also be used to document classes and their properties:
/**
* Represents a person.
*
* This class provides methods to get and set the person's name and age.
*
* @package MyPackage
*/
class Person {
/**
* @var string The name of the person.
*/
private $name;
/**
* @var int The age of the person.
*/
private $age;
/**
* Constructor for the Person class.
*
* @param string $name The name of the person.
* @param int $age The age of the person.
*/
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
/**
* Gets the name of the person.
*
* @return string The name of the person.
*/
public function getName() {
return $this->name;
}
/**
* Sets the name of the person.
*
* @param string $name The new name of the person.
* @return void
*/
public function setName($name) {
$this->name = $name;
}
/**
* Gets the age of the person.
*
* @return int The age of the person.
*/
public function getAge() {
return $this->age;
}
/**
* Sets the age of the person.
*
* @param int $age The new age of the person.
* @return void
*/
public function setAge($age) {
$this->age = $age;
}
}
By using DocBlocks, you can make your code more understandable and maintainable while also providing the necessary information for automatic documentation generation.
☜
☞