There are 3 "entry points" to begin using Better Reflection:
The first entry point is a minimal service locator provided by the library:
<?php
$betterReflection = new \Roave\BetterReflection\BetterReflection();
$reflector = $betterReflection->reflector();
$class = $reflector->reflectClass('ClassName');
$function = $reflector->reflectFunction('functionName');
The second entry point is the static constructors on the Reflection*
classes:
ReflectionClass::createFromName(string $name)
ReflectionClass::createFromInstance(object $instance)
ReflectionMethod::createFromName(string $className, string $methodName)
ReflectionMethod::createFromInstance(object $instance, string $methodName)
ReflectionParameter::createFromClassNameAndMethod(string $className, string $methodName, string $parameterName)
ReflectionParameter::createFromClassInstanceAndMethod(object $instance, string $methodName, string $parameterName)
ReflectionParameter::createFromSpec([string $className, string $methodName], string $parameterName)
ReflectionParameter::createFromSpec([object $instance, string $methodName], string $parameterName)
ReflectionParameter::createFromSpec(string $functionName, string $parameterName)
ReflectionProperty::createFromName(string $className, string $propertyName)
ReflectionProperty::createFromInstance(object $instance, string $propertyName)
Using these is documented in a bit more detail within the Usage documentation. These methods use a default set of pre-configured Source Locators:
PhpInternalSourceLocator
EvaledCodeSourceLocator
AutoloadSourceLocator
The second entry point is the Reflector
objects, to which you must provide specific
instructions on how Better Reflection is to find code, in the form of the Source Locators.
Source Locators are instructions on finding source code for Better Reflection to examine to create the reflections. A detailed list of each source locator and how they work can be found in the Usage documentation - Source Locators section.
Using Nikita Popov's PHP-Parser library, the source code located by the Source Locators is loaded into an AST format, forming the core foundation on which the reflection is based. Using the AST, we are able to examine the structure of the code, finding out things like what methods and properties are on a class, and what parameters belong to a method or function for example.
Internally to the Reflection*
classes in Better Reflection, we cleverly hold the AST as the main property. All
reflection, analysis and modification is done directly to this AST. Therefore it is possible to unparse or export the
AST back into code.