php下aop的一个实现办法

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

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

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

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

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

  1. qh663 August 5, 2006 / 5:32 pm

    呵呵,看看源贴,我的方法不错吧。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s