由于php不能动态修改类和对象的方法,实现aop就变得很困难。网上看到过用__get, __set, __call来实现的办法。这个我以前也想过。但这种办法也有很大的缺点。如果能够用 runkit 扩展,就好很多了。下面是个用 runkit 来实现方法调用拦截的例子。

class Target {
    function add($a, $b){
        echo $a + $b.”n”;

runkit_method_rename(‘Target’, ‘add’, ‘#add’);
    echo “before calln”;
    $ret = $this->{“#add”}($a,$b);
    echo “after calln”;
    return $ret;

$t = new Target;
$t->add(1, 3);

1 thought on “php下aop的一个实现办法

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.