您的位置 首页 php

PHP-AOP扩展使用简介

aop 全称:Aspect Oriented
Programming,即 面向切面编程 ,是一种采用外部注入的方式来取代代码侵入,能很好地降低代码模块间的耦合,通常用于的场景有:Authentication(权限)、Caching( 缓存 )、Context
passing(内容传递)、Error handling(错误处理)、Lazy
loading(懒加载)、Debugging(调试)、logging, tracing, profiling and
monitoring(记录跟踪,优化,校准)、Performance
optimization(性能优化)、Persistence(持久化)、Resource
pooling(资源池)、Synchronization(同步)、Transactions(事务)。

AOP在 JAVA 语言中的应用频率远比PHP要高的多,有名的Spring框架就实现了AOP。在PHP中,也有PHP-AOP扩展来支持面向切面编程。安装的AOP扩展如下:

#Clone the repository on your computer
git clone 
cd AOP
#prepare the package, you will need to have development tools for php
phpize
#compile the package
./configure
make
#before the installation, check that it works properly
make test
#install
make install 

安装完毕后,在你的php.ini里添加

extension=aop.so 

重启PHP-FPM。AOP的编程案例如下:

class MyServices
{
public function doAdminStuff1 ()
{
//some stuff only the admin should do
echo "Calling doAdminStuff1\n";
}
public function doAdminStuff2 ()
{
//some stuff only the admin should do
echo "Calling doAdminStuff2\n";
}
}
function adviceForDoAdmin ()
{
echo "Sorry, you should be an admin to do this\n";
}
aop_add_before('MyServices->doAdmin*()', 'adviceForDoAdmin');
$obj = new MyServices();
$obj->doAdminStuff1(); 

执行结果如下:

Sorry, you should be an admin to do this
Calling doAdminStuff1 

参考资料:

文章来源:智云一二三科技

文章标题:PHP-AOP扩展使用简介

文章地址:https://www.zhihuclub.com/153087.shtml

关于作者: 智云科技

热门文章

网站地图