roave/better-reflection
Better Reflection - an improved code reflection API
Maintainers
Requires
- php: ~8.4.1 || ~8.5.0
- ext-json: *
- jetbrains/phpstorm-stubs: 2026.1
- nikic/php-parser: ^5.7.0
Requires (Dev)
- phpbench/phpbench: ^1.6.1
- phpunit/phpunit: ^13.1.8
Suggests
- composer/composer: Required to use the ComposerSourceLocator
Provides
None
Conflicts
- thecodingmachine/safe: <1.1.3
Replaces
None
MIT 3ec176d4a161b4e8764edc625d69ff624ca390b1
- James Titcumb <james.woop@asgrim.com>
- Marco Pivetta <ocramius.woop@gmail.com>
- Gary Hockin <gary.woop@roave.com>
- Jaroslav HanslΓk <kukulich.woop@kukulich.cz>
This package is auto-updated.
Last update: 2026-06-17 08:08:49 UTC
README
π Mutation testing badge
π Type Coverage
π Latest Stable Version
π License
Better Reflection is a reflection API that aims to improve and provide more features than PHP's built-in reflection API.
Tip
Are you reflecting on how to improve your application? We offer consulting on this library, as well as plenty of other development work. If you need any help, feel free to get in touch.
- The Roave Team
Why is it better?
- You can reflect on classes that are not already loaded, without loading them
- Ability to reflect on classes directly from a string of PHP code
- Reflecting directly on closures
- Ability to extract AST from methods and functions
- Ability to return AST representation of a class or function
- Fetch return type declaration and parameter type declarations in PHP 7 code
- Moar stuff coming soon!
Typically you would use Better Reflection for static analysis tooling. It can serve as a baseline to access type information (e.g. doc blocks, type declarations), method/function body AST fetching etc. for static analysis.
Better Reflection is NOT suited to runtime usage, since performance is much worse than PHP built-in reflection. If you do not want to do anything that native PHP reflection can't do, then just use native PHP reflection! The "Better" in Better Reflection refers to feature, not speed!
Be sure to read more in the feature documentation.
Installation
Require using composer:
composer require roave/better-reflection
Usage
<?php use Roave\BetterReflection\BetterReflection; $classInfo = (new BetterReflection()) ->reflector() ->reflectClass(\Foo\Bar\MyClass::class);
Documentation
Upgrading
Please refer to the Upgrade Documentation documentation to see what is required to upgrade your installed
BetterReflection version.
Limitations
- PHP cannot autoload functions, therefore we cannot statically reflect functions
License
This package is released under the MIT license.
Contributing
If you wish to contribute to the project, please read the CONTRIBUTING notes.
