Have you ever used PHP’s built in reflection, only to find you can’t do quite what you wanted? What about finding types for parameters or properties? What about reflecting on classes that aren’t loaded, so that you can modify them directly? Better Reflection is an awesome new library that uses magical time-warp techniques* (*actual magic or time-warp not guaranteed) to improve on PHP’s built-in reflection by providing additional functionality. In this talk we’ll briefly explore the cool features of Better Reflection, and how you can use Better Reflection in your projects to maximise your reflection-fu.
30. // In ReflectionClass :
public static function createFromName($className)
{
return ClassReflector::buildDefaultReflector()->reflect($className);
}
// In ClassReflector :
public static function buildDefaultReflector()
{
return new self(new AggregateSourceLocator([
new PhpInternalSourceLocator(),
new EvaledCodeSourceLocator(),
new AutoloadSourceLocator(),
]));
}
36. // Save the class and require it
$classCode = (new CodePrinter())->prettyPrint([$classInfo->getAst()]);
$tmpFile = tempnam(sys_get_temp_dir(), 'br-monkey-patching');
file_put_contents($tmpFile, '<?php ' . $classCode);
require_once($tmpFile);
unlink($tmpFile);
37. // Now create an instance, and call the function...
$c = new MyClass();
var_dump($c->foo()); // will be 4!!!
41. ● DONE Currently: rewriting the internals
○ SourceLocators “might” return some code ?!
● DONE Returning AST/Code for method/fn
Ideas/plans - DONE
42. ● WIP More compatibility core reflection
● WIP Reflect core fns with default params
○ This is not currently possible with core reflection
● WIP Modification & exporting
● WIP Reflecting on closures
Ideas/plans - WIP
43. ● Adding a getColumn() function
● PHP 7 compatibility
● @inheritDoc traversal (to inherit type hints)
● Make it faster
Ideas/plans