ReflectionClass 是 PHP 內建的反射類,能在執行時動態獲取類的完整資訊(屬性、方法、常數、介面、父類、命名空間等),並支援動態實例化、呼叫方法、存取屬性等操作。它是框架開發、依賴注入容器、註解解析、單元測試等進階場景的核心基石。
<?php
namespace News;
class News {
public $newsid;
public function index() { /* ... */ }
}
// 一般實例化
$news = new News\News();
// 反射實例化(傳入「全限定類名」字串)
$ref = new \ReflectionClass('News\News');
// 或直接傳入物件
$ref = new \ReflectionClass($news);
| 方法 | 說明 | 範例輸出 |
|---|---|---|
getName() | 完整類名(含命名空間) | News\News |
getShortName() | 不含命名空間的類名 | News |
getNamespaceName() | 命名空間名稱 | News |
getProperties() | 所有屬性(ReflectionProperty 陣列) | 含 public/protected/private |
getMethods() | 所有方法(ReflectionMethod 陣列) | 可篩選 public/protected/private |
getConstants() | 所有常數 | 關聯陣列 |
getInterfaces() | 實作的介面 | ReflectionClass 陣列 |
getParentClass() | 父類反射物件 | ReflectionClass 或 false |
isAbstract() / isInterface() / isTrait() | 類型判斷 | bool |
典型應用於「基礎 Controller」或「Model 基類」中,避免硬編碼子類名稱:
<?php
abstract class BaseController {
protected $configs = [
'UserController' => ['auth' => true, 'rate_limit' => 60],
'ApiController' => ['auth' => false, 'rate_limit' => 300],
];
public function init() {
$ref = new \ReflectionClass($this); // $this 是子類實例
$className = $ref->getShortName(); // 取得子類短名
if (!isset($this->configs[$className])) {
return true; // 無配置則跳過
}
$params = $this->configs[$className];
return $this->handle($params);
}
abstract protected function handle(array $params);
}
<?php
class Container {
public function make($className) {
$ref = new \ReflectionClass($className);
$constructor = $ref->getConstructor();
if (!$constructor) {
return $ref->newInstance();
}
$params = $constructor->getParameters();
$dependencies = [];
foreach ($params as $param) {
$type = $param->getType();
if ($type && !$type->isBuiltin()) {
$dependencies[] = $this->make($type->getName()); // 遞歸解析
} elseif ($param->isDefaultValueAvailable()) {
$dependencies[] = $param->getDefaultValue();
} else {
throw new \Exception("無法解析參數: {$param->getName()}");
}
}
return $ref->newInstanceArgs($dependencies);
}
}
<?php
/**
* @Route("/user/{id}", methods={"GET"})
* @Auth(roles={"admin","editor"})
*/
class UserController {}
$ref = new \ReflectionClass(UserController::class);
$docComment = $ref->getDocComment();
// 使用正則或 doctrine/annotations 解析 @Route、@Auth 等註解
ReflectionNamedType、ReflectionUnionType 等,型別提示更完整。ReflectionClass 賦予 PHP「運行時自我檢查」的能力,是現代框架(Laravel、Symfony、Hyperf 等)實現自動裝配、路由掃描、中介軟體解析等核心功能的關鍵。掌握反射 API,能讓你寫出更通用、更解耦、更具擴充性的程式碼。