ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getEndLine in PHP?
Returns the ending source line of a user-defined class; built-in classes may not have a source line.
ReflectionClass::getEndLine()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
class Report { public const VERSION = 2; public string $title = 'Draft'; protected int $pages = 10; public static string $category = 'docs'; public function publish(): void {} }
$reflectionClass = new ReflectionClass(Report::class);
$result = $reflectionClass->getEndLine();
var_dump($result);
Output:
int(2)
Explanation: Returns the ending source line of a user-defined class; built-in classes may not have a source line.
Example 2: Testing Another Case
<?php
interface Exportable { public function export(): string; }
trait HasId { public function getId(): int { return 12; } }
class Invoice implements Exportable { use HasId; public string $name = 'Invoice'; public const STATE = 'open'; public function export(): string { return 'invoice'; } }
$reflectionClass = new ReflectionClass(Invoice::class);
$result = $reflectionClass->getEndLine();
var_dump($result);
Output:
int(4)
Explanation: The number returned is a source line, not a line count.
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->getEndLine();
var_dump($result);
Output:
int(3)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- The number returned is a source line, not a line count.
- 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::getEndLine | Returns the ending source line of a user-defined class; built-in classes may not have a source line. |
ReflectionClass::getStartLine() | getEndLine() identifies the last class source line, not the number of lines. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getEndLine() as though it executes the reflected class member. | The number returned is a source line, not a line count. |
| Choosing ReflectionClass::getStartLine() without checking whether it provides the same result. | getEndLine() identifies the last class source line, not the number of lines. |
Key Takeaways
- Returns the ending source line of a user-defined class; built-in classes may not have a source line.
- The number returned is a source line, not a line count.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getEndLine fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.