English | 中文文档
super-kernel/reflector returns PHP's native reflection objects and keeps them, so that asking one reflector again for the same class, method, or other symbol returns the same object instead of reflecting it anew.
composer require super-kernel/reflectorThe package needs the reflection extension, which PHP includes by default.
- A reflector for classes, methods, properties, constants, enums and their cases, functions, parameters, extensions, objects, generators, fibers, and references.
- The native
Reflection*objects with their complete API, reused within one reflector. - Name matching as PHP does it: class aliases, a leading backslash, and case variants of class and method names find the same object.
- A factory provider that supplies
ReflectorInterfaceto a SuperKernel container. - A static facade for code that cannot receive the reflector by injection.
It does not:
- wrap the results in its own models or translate PHP's
ReflectionException; - share its cache between reflector instances or processes;
- notice that code changed: it has no invalidation, clearing, or serialization of its cache.
Reflect a class of the project. Save it as src/Billing/Invoice.php:
<?php
declare(strict_types=1);
namespace Src\Billing;
final class Invoice
{
public function __construct(public readonly string $number)
{
}
public function total(): int
{
return 0;
}
}Ask a reflector for one method in three spellings, and a second reflector once. Save the script as bin/reflect.php:
<?php
declare(strict_types=1);
use Src\Billing\Invoice;
use SuperKernel\Reflector\Reflector;
require dirname(__DIR__) . '/vendor/autoload.php';
$reflector = new Reflector();
$method = $reflector->reflectMethod(Invoice::class, 'total');
echo $method->getReturnType(), PHP_EOL;
var_dump($method === $reflector->reflectMethod('Src\Billing\Invoice::total'));
var_dump($method === $reflector->reflectMethod('\src\billing\INVOICE', 'TOTAL'));
var_dump($method === new Reflector()->reflectMethod(Invoice::class, 'total'));Build the project and run the script:
skernel build
php bin/reflect.phpIt prints:
int
bool(true)
bool(true)
bool(false)
| Name | Kind | Purpose |
|---|---|---|
SuperKernel\Contract\ReflectorInterface |
Interface | The reflector that services receive by injection. |
SuperKernel\Reflector\Reflector |
Class | The reflector; new Reflector() creates one with an empty cache. |
SuperKernel\Reflector\Provider\ReflectorProvider |
Class | The factory provider of ReflectorInterface; each call creates a new reflector and makes it the facade's. |
SuperKernel\Reflector\Facade\ReflectorFacade |
Class | Static calls with the methods of ReflectorInterface, forwarded to the reflector of the last ReflectorProvider call. |
- Failures are PHP's own: a missing class raises
ReflectionExceptionwith PHP's message, such asClass "Src\Nope" does not exist. ReflectorFacaderaisesLogicExceptionwith the messageReflectorFacade requires initialization through ReflectorProvider.untilReflectorProviderhas been called in the process.super-kernel/dicalls it when it creates the root container.- Class, method, function, and extension names are matched without regard to case; property, constant, parameter, and enum case names are matched exactly.
- A reflector keeps its reflections as long as it lives; reflections of objects, closures, generators, and fibers do not keep those objects alive.
The developer documentation describes every entry point, the cache rules, the container, and the facade with complete examples: SuperKernel Reflector.
Released under the MIT License. See LICENSE.