web-fu/reflection
最新稳定版本:v2.5.0
Composer 安装命令:
composer require web-fu/reflection
包简介
Reflection API
关键字:
README 文档
README
This library is a type safe wrapper for PHP Reflection API.
This library is born with the purpose to solve the problem of type safety in PHP Reflection API. Reflection API is a very powerful tool, but presents some issues.
For example:
- the original
ReflectionClass::getConstantreturnfalseif the constant does not exist or if the constant is equal tofalse. ReflectionClass::newInstancereturn a generic object, but it is possible to know the type of the object.- New interfaces are added to the Reflection API in different PHP versions, so it is not possible to use them in a cross-version way.
Installation
web-fu/reflection is available on Packagist and can be installed using Composer.
composer require web-fu/reflection
Requires PHP 8.0 or newer.
Usage
This wrapper try to use the same names of the original Reflection API, but with a different namespace.
<?php require_once __DIR__ . '/vendor/autoload.php'; use WebFu\Reflection\ReflectionClass; use MyNamespace\MyClass; $reflection = new ReflectionClass(MyClass::class); echo $reflection->getName(); // MyNamespace\MyClass echo $reflection->getShortName(); // MyClass
Type management
PHP Reflection API use different classes to manage types: ReflectionType, ReflectionNamedType and ReflectionUnionType.
I created a single class to manage all types: WebFu\Reflection\ReflectionType.
I added a helper function to infer the PHPDocType, if specified
<?php require_once __DIR__ . '/vendor/autoload.php'; use WebFu\Reflection\ReflectionClass; class ClassWithTypes { public int $simple; public int|string $union; public $noType; public ?int $nullable; /** @var class-string */ public string $className; } $reflection = new ReflectionClass(MyClass::class); echo $reflection->getProperty('simple')->getType()->getTypeNames(); // ['int'] echo $reflection->getProperty('union')->getType()->getTypeNames(); // ['int','string'] echo $reflection->getProperty('noType')->getType()->getTypeNames(); // ['mixed'] echo $reflection->getProperty('nullable')->getType()->getTypeNames(); // ['int','null'] echo $reflection->getProperty('nullable')->getType()->getPhpDocTypeNames(); // ['class-string']
统计信息
- 总下载量: 5.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-07