ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getDocComment in PHP?
Returns the class-level docblock text, or false when none is present.
ReflectionClass::getDocComment()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
/** A document entity for reporting. */
class Document {}
$reflectionClass = new ReflectionClass(Document::class);
var_dump($reflectionClass->getDocComment());
Output:
string(39) "/** A document entity for reporting. */"
Explanation: Returns the class-level docblock text, or false when none is present.
Example 2: Testing Another Case
<?php
/** Represents a queued reporting job. */
class QueuedJob {}
$reflectionClass = new ReflectionClass(QueuedJob::class);
var_dump($reflectionClass->getDocComment());
Output:
string(41) "/** Represents a queued reporting job. */"
Explanation: Missing docblocks return false; they do not become empty strings.
Practical Example: Applying the Operation
<?php
abstract class BaseJob { protected string $name = 'job'; abstract public function run(): void; }
class CompletedJob extends BaseJob { public function run(): void {} }
$reflectionClass = new ReflectionClass(CompletedJob::class);
$result = $reflectionClass->getDocComment();
var_dump($result);
Output:
bool(false)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Missing docblocks return false; they do not become empty strings.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClass::getDocComment | Returns the class-level docblock text, or false when none is present. |
ReflectionClass::getAttributes() | getDocComment() reads a PHPDoc comment string or false; getAttributes() returns native attribute descriptors. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getDocComment() as though it executes the reflected class member. | Missing docblocks return false; they do not become empty strings. |
| Choosing ReflectionClass::getAttributes() without checking whether it provides the same result. | getDocComment() reads a PHPDoc comment string or false; getAttributes() returns native attribute descriptors. |
Key Takeaways
- Returns the class-level docblock text, or false when none is present.
- Missing docblocks return false; they do not become empty strings.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getDocComment fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.