public function callBar() { $this->bar(); } public function callBaz() { $this->baz(); } public function callFizzBuzz() { $this->fizzbuzz(); } } $class = new MyClass(); // Call methods directly $class->foo(); // This be foo, foo! $class->bar(); // This is the bar function (Tried to call bar) $class->baz(); // The amazing baz method. (Tried to call baz) $class->fizzbuzz(7); // Nothing (Tried to call fizzbuzz) echo "Calling the call functions below:\n"; $class->callFoo(); // This be foo, foo! $class->callBar(); // This is the bar function $class->callBaz(); // The amazing baz method. $class->callFizzBuzz();
<?php require_once __DIR__ . '/../../vendor/autoload.php'; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; use PhpParser\PrettyPrinter\Standard as CodePrinter; // Create the reflection first (without loading) $classInfo = (new ClassReflector(new SingleFileSourceLocator(__DIR__ . '/MyClass.php')))->reflect('MyClass'); // Override the body...! $classInfo->getMethod('foo')->setBodyFromClosure(function () { return 4; }); // Load the class...!!!! $classCode = (new CodePrinter())->prettyPrint([$classInfo->getAst()]); eval($classCode); $c = new MyClass(); echo $c->foo() . "\n"; // should be 4...!?!??
error_reporting(E_ALL); trait A { public function foo() { echo 'a'; } } trait B { public function foo() { echo 'b'; } } trait C { public function foo() { echo 'c'; } } class MyClass { use C, A, B { B::foo insteadof A, C; } } $t = new MyClass(); $t->foo();