ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getStartLine in PHP?
Returns the source line at which a user-defined class begins, or false for an internal class.
ReflectionClass::getStartLine()
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->getStartLine();
var_dump($result);
Output:
int(2)
Explanation: Returns the source line at which a user-defined class begins, or false for an internal class.
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->getStartLine();
var_dump($result);
Output:
int(4)
Explanation: Line numbering depends on the source file and formatting.
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->getStartLine();
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
- Line numbering depends on the source file and formatting.
- 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::getStartLine | Returns the source line at which a user-defined class begins, or false for an internal class. |
ReflectionClass::getEndLine() | getStartLine() identifies where a user-defined class begins, while getEndLine() marks its final source line. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getStartLine() as though it executes the reflected class member. | Line numbering depends on the source file and formatting. |
| Choosing ReflectionClass::getEndLine() without checking whether it provides the same result. | getStartLine() identifies where a user-defined class begins, while getEndLine() marks its final source line. |
Key Takeaways
- Returns the source line at which a user-defined class begins, or false for an internal class.
- Line numbering depends on the source file and formatting.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getStartLine fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.