标签:style blog http color os 使用 io 文件 for
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加 功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。
AOP-PHP是一个PECL扩展,您可以在PHP中使用面向方面的编程,无需编译或进行其他任何中间步骤。
AOP扩展的设计是最简单的方法,你可以认为PHP中的aop实现。
AOP旨在让横切关注点的分离(缓存,日志,安全,交易,……)
有两种安装模式:
第一种方法:
sudo pecl install aop-beta   | 
第二种方法:
#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
    make
    #before the installation, check that it works properly
    make test
    #install
    make install
笔者在第二种方法安装中出现了错误(如果没有错误这里可以飘过):
Can‘t locate Autom4te/C4che.pm in @INC (@INC contains: /usr/local/share/autoconf...
解决办法是重新安装autoconf:
#wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz #tar -zxf autoconf-latest.tar.gz #rpm -qf /usr/bin/autoconf #查看autoconf的版本 #rpm -e --nodeps autoconf-2.59-12 #卸载原来版本 #./configure --prefix=/usr #make && make install
编译安装成功后,需要在php.ini里装载模块,一般在centos里php的模块装载在/etc/php.d里面,新建一个文件aop.ini ,内容为:
extension=aop.so
安装成功后查看phpinfo,会看到一下内容:

在实践之前我们需要先学习哈aop的一些专业术语。
在实践之前我们需要准备四个文件:测试函数文件testfunction.php、测试类文件testclass.php、测试aop文件testaop.php和运行文件test.php。
这样做可以真实模拟我们的项目,大部分的项目都是这样布局的。
在代码中一些特殊点之前使用的通知,正常是调用一个方法或者函数。
testfunction.php代码:
<?php
function testFunc1(){
    echo ‘aop_add_before <br/>‘;
}
testaop.php代码:
<?php
$testpoint1 = function () {
echo "这是前切点测试函数:";
};
aop_add_before(‘testFunc1()‘, $testpoint1);
test.php代码:
<?php
require ‘testaop.php‘;
require ‘testclass.php‘;
require ‘testfunction.php‘;
header("Content-Type:text/html;charset=utf-8"); 
testFunc1();
不出意外,执行test.php我们将会看到:
这是前切点测试函数:aop_add_before
testclass.php代码:
<?php
class testClass1
{
    public function testBeforAdd1()
    {
        echo get_class($this);
    }
}
testaop.php代码:
<?php
$testpoint1 = function () {
echo "这是前切点测试函数:";
};
$testpoint2 = function () {
echo "这是前切点测试类方法:";
};
aop_add_before(‘testFunc1()‘, $testpoint1);
aop_add_before(‘testClass1->testBeforAdd1()‘, $testpoint2);
test.php代码:
<?php
require ‘testaop.php‘;
require ‘testclass.php‘;
require ‘testfunction.php‘;
header("Content-Type:text/html;charset=utf-8"); 
testFunc1();
$testClass1 = new testClass1();
echo $testClass1->testBeforAdd1();
执行test.php
这是前切点测试函数:aop_add_before 这是前切点测试类方法:testClass1
testclass.php源码
<?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;
    }
        
}
testaop.php源码
<?php
$testpoint11 = function  ()
{
    echo "这是前切点测试函数:";
};
$testpoint12 = function  ()
{
    echo "这是前切点测试类方法:";
};
aop_add_before(‘testFunc1()‘, $testpoint11);
aop_add_before(‘testClass1->testBeforAdd1()‘, $testpoint12);
//------测试类属性
class changeProperty
{
    public function shoot ( $who, $what)
    {
        if($what == ‘test‘){
            $what = ‘测试前通知类属性截取 <br/>‘;
        }
        echo "$who 想要 $what ";
    }
}
$testclass1 = new changeProperty();
$testpoint2 = function  ( AopJoinPoint $aop_tjp ) use( $testclass1 )
{
    if ( $aop_tjp->getKindOfAdvice() === AOP_KIND_BEFORE_READ_PROPERTY )
    {
        return; // 如果属性不能读则返回
    }
    elseif ( $aop_tjp->getKindOfAdvice() === AOP_KIND_BEFORE_WRITE_PROPERTY )
    {
        $testclass1->shoot($aop_tjp->getObject()->getName(),$aop_tjp->getAssignedValue());
    }
};
//测试类属性
aop_add_before(‘testClass2->publicProperty1‘, $testpoint2);
test.php源码
<?php
require ‘testaop.php‘;
require ‘testclass.php‘;
require ‘testfunction.php‘;
header("Content-Type:text/html;charset=utf-8"); 
//前通知
testFunc1();
$testClass1 = new testClass1();
echo $testClass1->testBeforAdd1();
$runtest2 = new testClass2(‘skyboy‘);
$runtest2->test();
执行test.php
这是前切点测试函数:aop_add_before 这是前切点测试类方法:testClass1 skyboy 想要 测试前通知类属性截取
在代码中一些特殊点之后使用的通知,一般是调用一个方法或者函数。
testfunction.php源码:
function testFunc2(){
    echo ‘这是返回后通知测试:‘;
}
testaop.php源码:
//测试返回后通知
$testpoint22 = function  ()
{
    echo "aop_add_after <br/>";
};
aop_add_after(‘testFunc2()‘, $testpoint22);
test.php源码:
//后通知
testFunc2();
执行test.php
这是返回后通知测试:aop_add_after
类和类属性和前通知类似,为了节省篇幅,这里偷懒了。
testfunction.php源码:
function testFunc3($param1,$param2){
    return $param1. $param2;
}
testaop.php源码:
//测试周边通知
function testaround (AopJoinPoint $object)
{
    $args = $object->getArguments();
    if ($args[0] !== null) {
        $args[0] = ‘我想测试‘;
    }
    if ($args[1] !== null) {
        $args[1] = ‘周边通知:‘;
    }
    $object->setArguments($args);
    $object->process();
    
    $returnValue = $object->getReturnedValue();
    $returnValue .= ‘aop_add_around<br/>‘;
    $object->setReturnedValue($returnValue);
    
}
aop_add_around(‘testFunc3()‘, ‘testaround‘);
test.php源码:
//周边通知
echo testFunc3(1,2);
执行test.php
我想测试周边通知:aop_add_around
类和类属性和前通知类似。
除了三个重要函数aop_add_before,aop_add_after,aop_add_around之外,我们还要记住这几个重要的函数。
获取通知的类型。有以下几个默认值。一般用在方法的属性更改。
• 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)
获取方法的参数。一般用在aop_add_before/aop_add_around。
设置方法的参数。一般用在aop_add_before/aop_add_around。
获取方法的返回值。一般用在aop_add_after/aop_add_around。
设置方法的返回值。一般用在aop_add_after/aop_add_around。
让方法运行。一般用在aop_add_around。
具体详细说明,请参考官方文档。
新建一个文件aopopenclose.php
源码如下:
<?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 enabledI‘m fooAfter fooaop is now disabledI‘m fooAfter fooBut you can still register new aspectsI‘m fooAfter fooAfter fooAop is now enabledI‘m fooAfter fooAfter foo | 
aop-php在真实意义上实现了php的aop,用户无需用其他的方式即可轻松实现。aop的编程思想是一把利刃,可以让耦合性差的项目轻松实现解耦。
全部测试文件和编辑后文件打包。点此下载。(基于ceotos环境php5.3编译)
标签:style blog http color os 使用 io 文件 for
原文地址:http://www.cnblogs.com/shsgl/p/3934340.html