ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getName in PHP?
Returns the fully qualified class name, including its namespace.
ReflectionClass::getName()
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->getName();
var_dump($result);
Output:
string(6) "Report"
Explanation: Returns the fully qualified class name, including its namespace.
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->getName();
var_dump($result);
Output:
string(7) "Invoice"
Explanation: Namespaced classes return fully qualified names rather than only the final class segment.
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->getName();
var_dump($result);
Output:
string(12) "CompletedJob"
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Namespaced classes return fully qualified names rather than only the final class segment.
- 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::getName | Returns the fully qualified class name, including its namespace. |
ReflectionClass::getShortName() | getName() returns the fully qualified class name; getShortName() omits its namespace. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getName() as though it executes the reflected class member. | Namespaced classes return fully qualified names rather than only the final class segment. |
| Choosing ReflectionClass::getShortName() without checking whether it provides the same result. | getName() returns the fully qualified class name; getShortName() omits its namespace. |
Key Takeaways
- Returns the fully qualified class name, including its namespace.
- Namespaced classes return fully qualified names rather than only the final class segment.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getName fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.