reflection
0.4.4
reflection ของพายุไต้ฝุ่นเป็นอีกทางเลือกหนึ่งแทน reflection ของ PHP ดั้งเดิม มันคือ:
composer require typhoon/ reflection typhoon/phpstorm- reflection -stubs
typhoon/phpstorm- reflection -stubs
เป็นสะพานเชื่อมสำหรับ jetbrains/phpstorm-stubs
หากไม่มีคลาสและฟังก์ชันภายในแพ็คเกจนี้ จะสะท้อนให้เห็นจาก reflection ดั้งเดิมโดยไม่มีเทมเพลต
use Typhoon reflection TyphoonReflector ;
use Typhoon Type types ;
use function Typhoon Type stringify ;
/**
* @template TTag of non-empty-string
*/
final readonly class Article
{
/**
* @param list<TTag> $tags
*/
public function __construct (
private array $ tags ,
) {}
}
$ reflector = TyphoonReflector:: build ();
$ class = $ reflector -> reflectClass (Article::class);
$ tagsType = $ class -> properties ()[ ' tags ' ]-> type ();
var_dump ( stringify ( $ tagsType )); // "list<TTag#Article>"
$ templateResolver = $ class -> createTemplateResolver ([
types:: union (
types:: string ( ' PHP ' ),
types:: string ( ' Architecture ' ),
),
]);
var_dump ( stringify ( $ tagsType -> accept ( $ templateResolver ))); // "list<'PHP'|'Architecture'>"
เอกสารยังห่างไกลจากความสมบูรณ์ อย่าลังเลที่จะสร้างประเด็นเพื่อชี้แจงว่าสิ่งต่างๆ ทำงานอย่างไร