SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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.
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.
27.
// 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(),
]));
}
28.
AutoloadSourceLocator
ReflectionClass::createFromName(new MyClass)
replace stream wrapper
disable error handling
call “class_exists”
restore stream wrapper
restore error handling
store attempted filename load
DO NOT LOAD FILE!
return stored filename
Read file and parse AST!
33.
// 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);
34.
// Now create an instance, and call the function...
$c = new MyClass();
var_dump($c->foo()); // will be 4!!!
37.
● DONE Currently: rewriting the internals
○ SourceLocators “might” return some code ?!
● DONE Returning AST/Code for method/fn
Ideas/plans - DONE
38.
● 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
39.
● Adding a getColumn() function
● PHP 7 compatibility
● @inheritDoc traversal (to inherit type hints)
● Make it faster
Ideas/plans