#Clone the repository on your computer
git clone https://github.com/AOP-PHP/AOP
cd AOP
#prepare the package, you will need to have development tools for php
phpize
#compile the package
<?php
//测试前通知类
class testClass1
{
public function testBeforAdd1()
{
echo get_class($this) .'<br />';
}
}
//测试前通知类属性
class testClass2
{
private $name;
public $publicProperty1 = 'test';
public function __construct ($name)
{
$this->name = $name;
}
public function getName ()
{
return $this->name;
}
public function test ()
{
$this->publicProperty1 = 'test';
return $this->publicProperty1;
}
}
getKindOfAdvice
获取通知的类型。有以下几个默认值。一般用在方法的属性更改。
· AOP_KIND_BEFORE before a given call, may it be function, method or property
· AOP_KIND_BEFORE_METHOD before a method call (method of an object)
· AOP_KIND_BEFORE_FUNCTION before a function call (not a method call)
· AOP_KIND_BEFORE_PROPERTY before a property (read or write)
· AOP_KIND_BEFORE_READ_PROPERTY before a property access (read only)
· AOP_KIND_BEFORE_WRITE_PROPERTY before a property write (write only)
· AOP_KIND_AROUND around a given call, may it be function, method or property access (read / write)
· AOP_KIND_AROUND_METHOD around a method call (method of an object)
· AOP_KIND_AROUND_FUNCTION around a function call (not a method call)
· AOP_KIND_AROUND_PROPERTY around a property (read or write)
· AOP_KIND_AROUND_READ_PROPERTY around a property access (read only)
· AOP_KIND_AROUND_WRITE_PROPERTY around a property write (write only)
· AOP_KIND_AFTER after a given call, may it be function, method or property access (read / write)
· AOP_KIND_AFTER_METHOD after a method call (method of an object)
· AOP_KIND_AFTER_FUNCTION after a function call (not a method call)
· AOP_KIND_AFTER_PROPERTY after a property (read or write)
· AOP_KIND_AFTER_READ_PROPERTY after a property access (read only)
· AOP_KIND_AFTER_WRITE_PROPERTY after a property write (write only)
<?php
ini_set("aop.enable", "1");
echo "aop is enabled<br />";
function foo ()
{
echo "I'm foo<br />";
}
$adviceShowFoo = function ()
{
echo "After foo<br />";
};
aop_add_after('foo()', $adviceShowFoo);
foo();
ini_set('aop.enable', '0');
echo "aop is now disabled<br />";
foo();
echo "But you can still register new aspects<br />";
aop_add_after('f*()', $adviceShowFoo);
foo();
ini_set('aop.enable', '1');
echo "Aop is now enabled<br />";
foo();
运行结果:
aop is enabled
I'm foo
After foo
aop is now disabled
I'm foo
After foo
But you can still register new aspects
I'm foo
After foo
After foo
Aop is now enabled
I'm foo
After foo
After foo