Skip to content

Instantly share code, notes, and snippets.

@asgrim
Created November 3, 2015 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asgrim/720245b693eb1cdacf13 to your computer and use it in GitHub Desktop.
Save asgrim/720245b693eb1cdacf13 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use BetterReflection\Reflector\ClassReflector;
use BetterReflection\SourceLocator\AggregateSourceLocator;
use BetterReflection\SourceLocator\SingleFileSourceLocator;
use PhpParser\PrettyPrinter\Standard as CodePrinter;
// Create the reflection first (without loading)
$classInfo = (new ClassReflector(new AggregateSourceLocator([
new SingleFileSourceLocator('MyClass.php'),
])))->reflect('MyClass');
// Override the body...!
$classInfo->getMethod('foo')->setBody(function () {
return 4;
});
// Load the class...!!!!
$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);
$c = new MyClass();
var_dump($c->foo()); // should be 4...!?!??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment