Skip to content

Latest commit

 

History

History
69 lines (48 loc) · 3.09 KB

how-it-works.md

File metadata and controls

69 lines (48 loc) · 3.09 KB

How does Better Reflection work?

Reflection Process

1. Reflector

There are 3 "entry points" to begin using Better Reflection:

1.1 The BetterReflection bootstrapper

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');

1.2 The Reflection* static methods

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

1.3 Manually instantiating *Reflector objects

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.

2. 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.

3. PhpParser

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.

4. Reflection

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.