Better Reflection - an improved code reflection API

Installs: 2 090 007

Dependents: 101

Suggesters: 3

Security: 0

Stars: 918

Watchers: 32

Forks: 109

Open Issues: 46

4.12.2 2020-12-17 17:48 UTC


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.

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
  • Better Reflection analyses the DocBlocks (using phpdocumentor/type-resolver)
  • 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
  • Change or remove PHP 7 parameter type and return type declarations from methods and functions
  • Change the body of a function or method to do something different
  • 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.


Require using composer:

$ composer require roave/better-reflection



use Roave\BetterReflection\BetterReflection;

$classInfo = (new BetterReflection())



Please refer to the Upgrade Documentation documentation to see what is required to upgrade your installed BetterReflection version.


  • PHP cannot autoload functions, therefore we cannot statically reflect functions


This package is released under the MIT license.


If you wish to contribute to the project, please read the CONTRIBUTING notes.