Download the PHP package technically/callable-reflection without Composer
On this page you can find all versions of the php package technically/callable-reflection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download technically/callable-reflection
More information about technically/callable-reflection
Files in technically/callable-reflection
Package callable-reflection
Short Description A handy library to simplify reflecting any callable or constructor. PHP8-ready!
License MIT
Informations about the package callable-reflection
Technically Callable Reflection
Technically\CallableReflection
is a handy library to simplify reflecting any callable
.
It provides a unified interface for reading callable arguments seamlessly supporting PHP8 union types.
It also lets you to easily invoke a callable with call()
and apply()
supporting named parameters.
Features
- Unified and simplified interface to reflect callable parameters
- No dependencies
- PHP 8.0 ready (supports union type hints; see examples below)
- PHP 7.1+ compatible
- Semver
- Tests
Installation
Use Composer package manager to add Technically\CallableReflection to your project:
Examples
Reflecting callable properties
Reflecting arbitrary class constructor
Checking if value satisfies parameter type declaration
Invoking callable via reflection
Invoking constructor via reflection
How is this better than reflecting Closure::fromCallable()
This library's functionality is somewhat similar to what a reflection of the standard Closure::fromCallable()
can provide.
And if that's sufficient for your use case, I recommend using the standard code.
This library, however, does provide some added value on top:
-
It unifies interactions will all kinds of callables, including class constructors. That was actually the primary use case I had in mind for it — building a DI service container.
For example, you cannot instantiate a new instance with Closure::fromCallable():
but you can call a constructor with this library:
-
It can know and tell what type of callable was reflected, while with Closure::fromCallable() this information is lost. This might be important for certain use cases, or not. Depending on the scenario.
-
It has nice additional convenience getters and checkers. Like
satisfies()
,isOptional()
,isNull()
,isScalar()
, etc. -
I also find the API somewhat more intuitive and convenient (which is, of course, subjective and debatable), as the native Reflection API is slightly polluted as a result of BC-preserving additions
- In terms of performance, I am sure this implementation will most definitely be slower than using the native code. Though I didn't test it to provide the exact numbers. It's basically doing almost the same, but with extra code on top — just operating on top of Reflections API.
Changelog
All notable changes to this project will be documented in the CHANGELOG file.
Credits
Implemented by :space_invader: Ivan Voskoboinyk.