liqiongfan / xaop

Xaop PHP高性能的AOP扩展
https://www.supjos.cn
Other
48 stars 10 forks source link

addAfterReturnAop 方法注入,返回值如何获取? #10

Closed longhtml closed 5 years ago

longhtml commented 5 years ago

<?php // This is the file demonstrate the InjectionAOP mode which need set the php.ini with the config to 3: // xaop.aop_mode = 3 class Swing { public function get($name) { echo 'Swing::get()' . PHP_EOL; return [1,2,3,4,5]; // to test the after_return aop feature } } // This aop will caught all return value except the null // So if you use return null, it will be omitted by the kernel, for the kernel default return null Xaop::addAfterReturnAop(Swing::class, "__get", function(){ echo '_after_return_caught' . PHP_EOL; // 如何获取返回值? }); echo '

';
$swing = new Swing();
$swing->di;
echo '
';

/* output: Swing::__get() _after_return_caught /

liqiongfan commented 5 years ago

addAfterReturnAop方法仅作为切入方法参与原始方法之后的代码运行,匿名方法内进行return语句返回,并不影响原始方法。 如果需要操作原始方法的返回值,建议使用环绕aop。