php7 aop,php之aop实践
aop-php实践之通知
前通知aop_add_before
在代码中一些特殊点之前使用的通知,正常是调用一个方法或者函数。
我们先测试函数
testfunction.php代码:
phpfunctiontestFunc1(){echo 'aop_add_before
';
}
testaop.php代码:
php$testpoint1 = function() {echo "这是前切点测试函数:";
};
aop_add_before('testFunc1()', $testpoint1);
test.php代码:
phprequire 'testaop.php';require 'testclass.php';require 'testfunction.php';header("Content-Type:text/html;charset=utf-8");
testFunc1();
不出意外,执行test.php我们将会看到:
这是前切点测试函数:aop_add_before
我们再玩哈类
testclass.php代码:
phpclasstestClass1
{public functiontestBeforAdd1()
{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代码:
phprequire 'testaop.php';require 'testclass.php';require 'testfunction.php';header("Content-Type:text/html;charset=utf-8");
testFunc1();$testClass1 = newtestClass1();echo $testClass1->testBeforAdd1();
执行test.php
这是前切点测试函数:aop_add_before
这是前切点测试类方法:testClass1
再测试类属性
testclass.php源码
php//测试前通知类
classtestClass1
{publicfunction testBeforAdd1()
{
echo get_class($this) .'';
}
}//测试前通知类属性
classtestClass2
{private$name;public $publicProperty1 = 'test';publicfunction __construct ($name)
{
$this->name =$name;
}publicfunction getName ()
{return $this->name;
}publicfunction 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);//------测试类属性
classchangeProperty
{public function shoot ( $who, $what)
{if($what == 'test'){$what = '测试前通知类属性截取
';
}echo "$who 想要 $what ";
}
}$testclass1 = newchangeProperty();$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源码
phprequire 'testaop.php';require 'testclass.php';require 'testfunction.php';header("Content-Type:text/html;charset=utf-8");//前通知
testFunc1();$testClass1 = newtestClass1();echo $testClass1->testBeforAdd1();$runtest2 = new testClass2('skyboy');$runtest2->test();
执行test.php
这是前切点测试函数:aop_add_before
这是前切点测试类方法:testClass1
skyboy 想要 测试前通知类属性截取
返回后通知aop_add_after
在代码中一些特殊点之后使用的通知,一般是调用一个方法或者函数。
测试函数
testfunction.php源码:
functiontestFunc2(){echo '这是返回后通知测试:';
}
testaop.php源码:
//测试返回后通知
$testpoint22 = function()
{echo "aop_add_after
";
};
aop_add_after('testFunc2()', $testpoint22);
test.php源码:
//后通知
testFunc2();
执行test.php
这是返回后通知测试:aop_add_after
类和类属性和前通知类似,为了节省篇幅,这里偷懒了。
周边通知aop_add_around
测试函数
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
';$object->setReturnedValue($returnValue);
}
aop_add_around('testFunc3()', 'testaround');
test.php源码:
//周边通知
echo testFunc3(1,2);
执行test.php
我想测试周边通知:aop_add_around
类和类属性和前通知类似。
aop-php函数说明
除了三个重要函数aop_add_before,aop_add_after,aop_add_around之外,我们还要记住这几个重要的函数。
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)
getArguments
获取方法的参数。一般用在aop_add_before/aop_add_around。
setArguments
设置方法的参数。一般用在aop_add_before/aop_add_around。
getReturnedValue
获取方法的返回值。一般用在aop_add_after/aop_add_around。
setReturnedValue
设置方法的返回值。一般用在aop_add_after/aop_add_around。
process
让方法运行。一般用在aop_add_around。
具体详细说明,请参考官方文档。
php7 aop,php之aop实践相关推荐
- DropWizard的AOP扩展点最佳实践
DropWizard的AOP扩展点最佳实践 一.扩展点接口 二.使用示例 各类微服务框架都提供了AOP的能力,DW也不例外.AOP特别是在框架层面使用的特别多.使用的原理基本是动态代理模式. 其实我在 ...
- 基于使用AspectJ实现AOP,注解AOP开发(基于xml文件、基于注解)
AOP概念 AOP是Aspect Oriented Programming的缩写,即『面向切面编程』.它和我们平时接触到的OOP都是编程的不同思想,OOP,即『面向对象编程』,它提倡的是将功能模块化, ...
- 从AOP到Spring AOP
文章目录 一.前言 二.AOP基础知识 2.1 AOP 2.1.1 引子:AOP,AOP是什么? 2.1.2 使用对比的方式理解AOP--AOP与OOP 2.1.3 使用对比的方式理解AOP--静态A ...
- aopaspect区别_面试官:什么是AOP?Spring AOP和AspectJ的区别是什么?
AOP(Aspect Orient Programming),它是面向对象编程的一种补充,主要应用于处理一些具有横切性质的系统级服务,如日志收集.事务管理.安全检查.缓存.对象池管理等. AOP实现的 ...
- Spring AOP编程-传统AOP开发切点表达式写法介绍
关于execution的常用语法: 1. execution(public * *()) 所有的public的方法 2. execution(* cn.nwtxxb.aop.*(..)) 所有的aop ...
- Spring AOP编程-传统aop开发总结
1.编写目标对象(target) <!-- 目标target --><bean id="orderService" class="cn.nwtxxb.a ...
- AOP和Spring AOP介绍
AOP和Spring AOP介绍 文章目录 AOP和Spring AOP介绍 一.AOP简介 二. 传统开发存在的弊端 三. AOP实现原理 四.Spring AOP 五.AOP相关术语 一.AOP简 ...
- (转)使用CGLIB实现AOP功能与AOP概念解释
http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...
- Guice Aop 与 Hasor Aop 原理及其实现
为什么80%的码农都做不了架构师?>>> 这篇是承接<轻量级 Java 开发框架 设计>系列Blog文的后续文章,本文主要介绍 Hasor 中 AOP 方面的设计以 ...
- aop中joinpoint_Spring AOP示例教程–方面,建议,切入点,JoinPoint,注释,XML配置...
aop中joinpoint Spring Framework is developed on two core concepts – Dependency Injection and Aspect O ...
最新文章
- Python list排序
- Java高并发编程(五):Java中的锁Lock
- 强!chrome彻底关闭自动升级新方法实例演示,终于解决了chrome自动升级的烦恼
- HttpRunner环境部署-踩坑篇
- [毕业生的商业软件开发之路]C#语法基础结构
- 一个spark receiver 或多个spark receiver 接收 多个flume agent
- C语言中的指针与下标,C语言下标和指针的关系
- java jxl.jar_jxl.jar官方下载-jxl.jar(java操作excel) 免费版 - 河东下载站
- c语言char sex是什么意思,2012年计算机二级C语言入门教程:结构体
- 电脑在登陆界面如何打开计算机管理,无线路由器管理界面如何登录 无法进入管理界面怎么办...
- 使用cmd命令查看WiFi密码
- echars折线图y轴自定义数值会被转换为整数的问题
- 领航机器人广告段子_医院机器人物流科技宣传广告语
- 虚幻AI蓝图基础笔记(万字整理)
- 上顿号符号_标点符号(1):谈谈顿号的用法
- 增肌粉和蛋白粉的区别
- 参加2021数学建模美赛C题的感受
- 如何将pdf快速转换为图片格式
- 黑马程序员--黑马程序员CEO方立勋致全体员工和同学们的公开信
- php手机站自动跳转,php实现手机站自动跳转
热门文章
- 如何在SharePointDesigner订制页面里判断用户权限
- 【Shiro第七篇】SpringBoot + Shiro实现会话管理
- Java 序列化与反序列化详解
- 一个出色的UI设计师需要具备哪些能力?
- [手把手教你] 用Swoft 搭建微服务(TCP RPC)
- 《鸟哥的Linux私房菜》读书笔记
- UIApplication
- 管理九段,你的管理入围“几段”了?
- 一个老板向员工发的牢骚
- SQL SERVER: 合并相关操作(Union,Except,Intersect)