PHP code example of technically / callable-reflection
1. Go to this page and download the library: Download technically/callable-reflection library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
technically / callable-reflection example snippets
$function = function (string $abstract, Closure|string|null $concrete): mixed {
// function body
};
$reflection = CallableReflection::fromCallable($function);
var_dump($reflection->isFunction()); // false
var_dump($reflection->isMethod()); // false
var_dump($reflection->isClosure()); // true
[$p1, $p2] = $reflection->getParameters();
var_dump($p2->getName()); // "concrete"
var_dump($p2->isNullable()); // true
var_dump($p2->isOptional()); // false
var_dump($p2->hasTypes()); // true
[$t1, $t2, $t3] = $p2->getTypes();
var_dump($t1->isScalar()); // false
var_dump($t1->isClassName()); // true
var_dump($t1->getType()); // "Closure"
var_dump($t2->isScalar()); // true
var_dump($t2->isClassName()); // false
var_dump($t2->getType()); // "string"
var_dump($t3->isNull()); // true
var_dump($t3->isScalar()); // false
var_dump($t3->isClassName()); // false
var_dump($t3->getType()); // "null"
final class MyService {
public function __construct(LoggerInterface $logger, bool $debug = false)
{
// ...
}
}
$reflection = CallableReflection::fromConstructor(MyService::class);
var_dump($reflection->isConstructor()); // true
var_dump($reflection->isFunction()); // false
var_dump($reflection->isMethod()); // false
var_dump($reflection->isClosure()); // false
[$p1, $p2] = $reflection->getParameters();
var_dump($p1->getName()); // "logger"
var_dump($p1->isNullable()); // false
var_dump($p1->isOptional()); // false
var_dump($p1->hasTypes()); // true
var_dump($p2->getName()); // "debug"
var_dump($p2->isNullable()); // false
var_dump($p2->isOptional()); // true
var_dump($p2->hasTypes()); // true
$function = function (int|string $value = null): mixed {
// function body
};
$reflection = CallableReflection::fromCallable($function);
[$param] = $reflection->getParameters();
var_dump($param->satisfies(null)); // true
var_dump($param->satisfies(1)); // true
var_dump($param->satisfies('Hello')); // true
var_dump($param->satisfies(2.5)); // false
var_dump($param->satisfies([])); // false
var_dump($param->satisfies(true)); // false
$function = function (string $abstract, Closure|string|null $concrete): mixed {
// function body
};
$reflection = CallableReflection::fromCallable($function);
// 1) call with positional parameters
$result = $reflection->call(LoggerInterface::class, MyLogger::class);
// 1) call with named parameters
$result = $reflection->call(concrete: MyLogger::class, abstract: LoggerInterface::class);
// 2) call with positional parameters array
$result = $reflection->apply([LoggerInterface::class, MyLogger::class]);
// 3) call with named parameters array
$result = $reflection->apply(['concrete' => MyLogger::class, 'abstract' => LoggerInterface::class]);
// 4) call with mixed named and positional parameters array
$result = $reflection->apply([LoggerInterface::class, 'concrete' => MyLogger::class]);
// 5) CallableReflection is a callable by itself
$result = $reflection(LoggerInterface::class, MyLogger::class);
final class MyService {
public function __construct(LoggerInterface $logger, bool $debug = false)
{
// ...
}
}
$reflection = CallableReflection::fromConstructor(MyService::class);
$service = $reflection->call(new NullLogger());
// or alternatively:
// $service = $reflection->apply(['logger' => new NullLogger()]);
assert($service instanceof MyService);
$reflection = new ReflectionFunction(Closure::fromCallable([MyRemoteService::class, '__construct']));
$service = $reflection->call($token); // (!) doesn't work
$reflection = CallableReflection::fromConstructor(MyRemoteService::class);
$service = $reflection->call($token);