Parser Reflection API - Provides source code analysis without loading classes into the PHP memory
This library is deprecated. Please use BetterReflection.
Parser Reflection API library provides a set of classes that extend original internal Reflection classes, but powered by PHP-Parser library thus allowing to create a reflection instance without loading classes into the memory.
This library can be used for analysing the source code; for automatic proxy creation and much more.
Library can be installed with Composer. Installation is quite easy:
$ composer require goaop/parser-reflection
Composer will install the library to your project’s vendor/goaop/parser-reflection
directory.
Prior to the first use library can be optionally initialized. If you use Composer for installing packages and loading classes,
then you shouldn’t worry about initialization, library will be initialized automatically.
If project uses a custom autoloader then you should follow the next steps:
\Go\ParserReflection\LocatorInterface
ReflectionEngine::init()
method for initial configurationJust use Go\ParserReflection
package reflection classes like traditional ones:
$parsedClass = new \Go\ParserReflection\ReflectionClass(SomeClass::class);
var_dump($parsedClass->getMethods());
$parsedMethod = new \Go\ParserReflection\ReflectionMethod(SomeClass::class, 'someMethod');
echo (string)$parsedMethod;
Or you can use an additional classes ReflectionFile
and ReflectionFileNamespace
to analyse a raw PHP files:
$parsedFile = new \Go\ParserReflection\ReflectionFile('SomeClass.php');
$fileNameSpaces = $parsedFile->getFileNamespaces();
// We can iterate over namespaces in the file
foreach ($fileNameSpaces as $namespace) {
$classes = $namespace->getClasses();
// Iterate over the classes in the namespace
foreach ($classes as $class) {
echo "Found class: ", $class->getName(), PHP_EOL;
// Now let's show all methods in the class
foreach ($class->getMethods() as $method) {
echo "Found class method: ", $class->getName(), '::', $method->getName(), PHP_EOL;
}
// ...all properties in the class
foreach ($class->getProperties() as $property) {
echo "Found class property: ", $class->getName(), '->', $property->getName(), PHP_EOL;
}
}
}
To understand how library works let’s look at what happens during the call to the new \Go\ParserReflection\ReflectionClass(SomeClass::class)
\Go\ParserReflection\ReflectionClass
asks reflection engine to give an AST node for the given class nameComposerLocator
instance asks the Composer to find a filename for the given class and returns this result back to the reflection engineAll parser reflection classes extend PHP internal reflection classes, this means that you can use \Go\ParserReflection\ReflectionClass
instance in any place that asks for \ReflectionClass
instance. All reflection methods should be compatible with original ones, providing an except methods that requires object manipulation, such as invoke()
, invokeArgs()
, setAccessible()
, etc. These methods will trigger the process of class loading and switching to the internal reflection.