Exemple #1
0
    public function fooBefore($params)
    {
        echo '------------------------------------------' . PHP_EOL;
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
}
class AspectThree
{
    public function fooAfter($params)
    {
        echo '------------------------------------------' . PHP_EOL;
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
}
$model = new Model();
//Bind multi aspect objects
microAOP\Proxy::__bind__($model, 'AspectOne', 'AspectTwo', 'AspectThree');
$model->foo();
/*  output:

------------------------------------------
AspectOne::fooBefore has been executed
------------------------------------------
AspectTwo::fooBefore has been executed
Model::foo has been executed
------------------------------------------
AspectOne::fooAfter has been executed
------------------------------------------
AspectThree::fooAfter has been executed

*/
Exemple #2
0
    {
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
}
class Aspect
{
    public function argsAlways($params)
    {
        echo '------------------------------------------' . PHP_EOL;
        echo __METHOD__ . ' has been executed' . PHP_EOL;
        print_r($params['args']);
    }
}
$model = new Model();
//Just bind it
microAOP\Proxy::__bind__($model, new Aspect());
$model->args('arg1', 'argTwo');
echo "===========================================" . PHP_EOL;
$model->args('one', 'two', 'argThree');
/*  output:

Model::args has been executed
------------------------------------------
Aspect::argsAlways has been executed
Array
(
    [one] => arg1
    [two] => argTwo
    [three] => three
)
===========================================
Exemple #3
0
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
}
class Aspect
{
    public function saveBefore($params)
    {
        echo '------------------------------------------' . PHP_EOL;
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
    public function saveAfter($params)
    {
        echo '------------------------------------------' . PHP_EOL;
        echo __METHOD__ . ' has been executed' . PHP_EOL;
    }
}
$model = new Model();
//Just bind it by classname
microAOP\Proxy::__bind__($model, 'Aspect');
//You can bing it by Instance too, like this
//microAOP\Proxy::__bind__($model, new Aspect());
$model->save();
/*  output:

------------------------------------------
Aspect::saveBefore has been executed
Model::save has been executed
------------------------------------------
Aspect::saveAfter has been executed

*/
{
    echo __FUNCTION__ . ' has been executed' . PHP_EOL;
    echo '------------------------------------------' . PHP_EOL;
}
function fooAfter()
{
    echo '------------------------------------------' . PHP_EOL;
    echo __FUNCTION__ . ' has been executed' . PHP_EOL;
}
$model = new Model();
//Bind function
microAOP\Proxy::__bind_func__($model, 'foo', 'before', 'fooBefore');
microAOP\Proxy::__bind_func__($model, 'foo', 'after', 'fooAfter');
$model->foo();
//取消绑定Model::foo中before位置的函数
microAOP\Proxy::__unbind_func__($model, 'foo', 'before');
echo '==========================================' . PHP_EOL;
$model->foo();
/*  output:

fooBefore has been executed
------------------------------------------
Model::foo has been executed
------------------------------------------
fooAfter has been executed
==========================================
Model::foo has been executed
------------------------------------------
fooAfter has been executed

*/
Exemple #5
0
    }
}
class AspectTwo
{
    public function saveBefore($params)
    {
        echo __METHOD__ . ' has been executed' . PHP_EOL;
        echo '------------------------------------------' . PHP_EOL;
    }
}
$model = new Model();
//绑定切面类
microAOP\Proxy::__bind__($model, 'AspectOne', 'AspectTwo');
$model->save();
//取消绑定切面类:AspectOne
microAOP\Proxy::__unbind__($model, 'AspectOne');
echo '==========================================' . PHP_EOL;
$model->save();
/*  output:

AspectOne::saveBefore has been executed
------------------------------------------
AspectTwo::saveBefore has been executed
------------------------------------------
Model::save has been executed
==========================================
AspectTwo::saveBefore has been executed
------------------------------------------
Model::save has been executed

*/
Exemple #6
0
}
$model = new Model();
//Bind function
microAOP\Proxy::__bind_func__($model, 'foo', 'before', 'foo');
//Bind closure
microAOP\Proxy::__bind_func__($model, 'foo', 'after', $closure);
//Another way to bind closure
microAOP\Proxy::__bind_func__($model, 'three', 'after', function () {
    echo __FUNCTION__ . ' has been executed' . PHP_EOL;
});
//Bind Static method of class
microAOP\Proxy::__bind_func__($model, 'fooOne', 'before', 'Aspect::foo');
//Bind method of object
microAOP\Proxy::__bind_func__($model, 'two', 'before', array(new Aspect(), 'two'));
//Support for regex
microAOP\Proxy::__bind_func__($model, '/^foo.*/i', 'always', 'forRegex');
$model->foo();
echo '==========================================' . PHP_EOL;
$model->fooOne();
echo '==========================================' . PHP_EOL;
$model->two();
echo '==========================================' . PHP_EOL;
$model->three();
/*  output:

------------------------------------------
foo has been executed
Model::foo has been executed
------------------------------------------
{closure} has been executed
------------------------------------------