search
HomeBackend DevelopmentPHP TutorialA first look at AOP FOR PHP_PHP tutorial
A first look at AOP FOR PHP_PHP tutorialJul 13, 2016 am 10:52 AM
aopforoopphpPreliminary explorationmethodyesofsolvequestion



Question
A first look at AOP FOR PHP
Solution


AOP is the continuation of OOP and is the abbreviation of Aspect Oriented Programming, which means aspect-oriented programming. AOP is actually a continuation of the GoF design pattern. The design pattern tirelessly pursues the decoupling between the caller and the callee. AOP can be said to be a realization of this goal. In fact, this technology appeared a long time ago. When I googled it, it was already a technology that appeared in 2006.
From my understanding, it is to cut the function horizontally without destroying the original method or class. Then add your own methods to deal with it. For example, we often have to perform permission judgment before executing some methods. After processing, log writing and other operations must be performed. The general operation method is to write the processing procedure at the head and bottom of the method. This destroys the single-function principle of OOP. Because when there are 100 or even 1000 methods to perform the same processing, it is inevitable that some unnecessary errors will occur. This is AOP in action. . .
When we write a function to output some information, we don't want people who don't have permission to see the information to see it before processing. Some common cache information may be written after processing. The usual way to write it is like this class OneTest{

public function getInfo(){

//Check the permissions

ACL::checkRole();

//Do the action process of obtaining information;

.....

//Write to cache

Cache::writeCache();

}

}


Copy the code What if there are 1000 methods that need to do the same operation. . Modify one place and modify everywhere. Difficult maintenance situations arise. To use AOP to deal with this problem, you only need this class OneTest{

public function getInfo(){

//Do the action process of obtaining information;

.....

}

}


Copy the code so that the two that break the encapsulation are taken out and defined in other places. . . . . That's roughly what it means. Of course, the misunderstanding was a mistake and it was purely a coincidence.
You can google for detailed introduction. . Some information has not been sorted out yet. . . . . This was done in my spare time while busy.
Here are a few links about php. If you are interested, you can take a look
AOP FOR PHP Discussion
[url=http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377.aspx]Link tag http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377 .aspx[/url]
PHP quasi-AOP implementation
[url=http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html]Link tag http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html[/url]
Then I simply implemented it myself

/**

* TSAspect{

* AOP for php

* @package

* @version $id$

* @copyright 2009-2011 SamPeng

* @author SamPeng

* @license PHP Version 5.2 {@link [url=http://www.sampeng.cn]www.sampeng.cn[/url]}

*/

class TSAspect{

/**

* instance

* Reference to the object to which the method belongs

* @var mixed

* @access private

*/

private $instance;

/**

* method

*Method name

* @var mixed

* @access private

*/

private $method;



/**

* aspect

* Aspect save array

* @var array

* @access private

*/

private $aspect = array();



/**

* __construct

* The constructor finds out the implementation methods of all Aspects

* @param mixed $instance

* @param mixed $method

* @param mixed $arg

* @access public

* @return void

*/

public function __construct( $instance,$method,$arg = null ){

$this->aspect = self::findFunction();

$this->instance = $instance;

$this->method = $method;

}



public function callAspect(){

$before_arg = $this->beforeFunction();

$callBack = array( $this->instance,$this->method);

$return = call_user_func_array( $callBack,$arg );

$this->afterFunction();

}





/**

* beforeFunction

* Collection of methods executed before the method

* @static

* @access public

* @return void

*/

protected function beforeFunction(){

$result = $this->getFunction("before");

return $result;

}



/**

* afterFunction

* Collection of methods executed after the method

* @static

* @access public

* @return void

*/

protected function afterFunction(){

$result = $this->getFunction( "after" );

}



/**

* findFunction

* Find all Aspect method collections.

* @static

* @access private

* @return void

*/

private static function findFunction(){

$aspect = array();

foreach ( get_declared_classes() as $class ){

$reflectionClass = new ReflectionClass( $class );

if ( $reflectionClass->implementsInterface( 'InterfaceAspect' ) )

$aspect[] = $reflectionClass;

}

return $aspect;



}



/**

* getFunction

* Call the inserted method

* @param mixed $aspect

* @static

* @access private

* @return void

*/

private function getFunction($aspect){

$result = array();

$array = $this->aspect;

foreach ( $array as $plugin ){

if ( $plugin->hasMethod($aspect ) ){

$reflectionMethod = $plugin->getMethod( $aspect );

if ( $reflectionMethod->isStatic() ){

$items = $reflectionMethod->invoke( null );

}else{

$pluginInstance = $plugin->newInstance();

$items = $reflectionMethod->invoke( $pluginInstance );

}

//处理经过处理的集合

if ( is_array( $items ) ){

$result = array_merge( $result,$items );

}

}

}

return $result;

}

}





interface InterfaceAspect{

public static function getName();

}



class testAspect implements InterfaceAspect{

public static function getName(){

return "这是一个测试AOP";

}



public static function before(){

echo "方法执行之前";

}

public static function after(){

echo "方法执行后
";

}

}



class test{

public function samTest($arg){

echo "这是一个测试方法";

}

}



$test = new test();

$aspect = new TSAspect($test,'samTest');

$aspect->callAspect();


复制代码输出:
方法执行之前
这是一个测试方法
方法执行之后


网友建意:
不懂,,,对开发思想有用不?
网友建意:
思想?这其实也是一种设计模式。。。。将破坏函数单一功能的部分耦合出来。。具体实现有很多办法。。当然PHP的是准AOP。。因为他不能像java那样的编译语言在编译时的时候插入横切面处理过程。
网友建意:
新的事物总是被人华丽的无视。。。
网友建意:
去年就知道面向切面编程了,,,用不上,,,,呵呵,,,
网友建意:
刚刚接触面向函数式编程,Haskell 、Erlang
网友建意:
PHP实现AOP要比Java简单的多..因为有runkit
网友建意:
自己连OOP还没大懂呢。技术啊。追不上。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632517.htmlTechArticleProblem AOP FOR PHP Solution AOP is the continuation of OOP and is the abbreviation of Aspect Oriented Programming, which means aspect-oriented programming. AOP is actually a continuation of the GoF design pattern. The design pattern...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
go语言不支持aop吗go语言不支持aop吗Dec 27, 2022 pm 05:04 PM

go语言支持aop。AOP是指面向切面编程,是通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术;AOP是面向对象中的一种方式,主要应用场景:日志记录,性能统计,安全控制,事务处理,异常处理等等。

springboot通过spel结合aop实现动态传参的方法springboot通过spel结合aop实现动态传参的方法May 16, 2023 am 11:49 AM

SpEl表达式简介正式撸代码之前,先了解下SpEl(SpringExpressionLanguage)表达式,这是Spring框架中的一个利器.Spring通过SpEl能在运行时构建复杂表达式、存取对象属性、对象方法调用等等.举个简单的例子方便理解,如下//定义了一个表达式StringexpressionStr="1+1";ExpressionParserparser=newSpelExpressionParser();Expressionexpression=parser.

如何在PHP中使用AOP框架如何在PHP中使用AOP框架May 19, 2023 pm 01:21 PM

AOP(面向切面编程)是一种编程思想,用于解耦业务逻辑和横切关注点(如日志、权限等)。在PHP中,使用AOP框架可以简化编码,提高代码可维护性和可扩展性。本文将介绍在PHP中使用AOP框架的基本原理和实现方法。一、AOP的概念和原理面向切面编程,指的是将程序的业务逻辑和横切关注点分离开来,通过AOP框架来实现统一管理。横切关注点指的是在程序中需要重复出现并且

Java语言中的IoC和AOP的应用Java语言中的IoC和AOP的应用Jun 10, 2023 pm 12:10 PM

在Java语言中,IoC(InversionofControl)和AOP(AspectOrientedProgramming)是两种非常重要的编程思想和技术。它们的应用可以大大提高代码的可维护性、可扩展性和可重用性,从而帮助开发人员更加高效地开发和维护软件系统。IoC是一种面向对象设计模式,也被称为“依赖注入(DependencyInjection

入门Java爬虫:认识其基本概念和应用方法入门Java爬虫:认识其基本概念和应用方法Jan 10, 2024 pm 07:42 PM

Java爬虫初探:了解它的基本概念与用途,需要具体代码示例随着互联网的快速发展,获取并处理大量的数据成为企业和个人不可或缺的一项任务。而爬虫(WebScraping)作为一种自动化的数据获取方法,不仅能够快速地收集互联网上的数据,还能够对大量的数据进行分析和处理。在许多数据挖掘和信息检索项目中,爬虫已经成为一种非常重要的工具。本文将介绍Java爬虫的基本概

Springboot+AOP怎么实现时间参数格式转换Springboot+AOP怎么实现时间参数格式转换May 11, 2023 pm 03:43 PM

前言场景前端传过来的时间参数,我们后端自定义时间格式转化使用,想转成什么就转成什么。不同业务场景,跟前端对接,一种控件基本时间参数是固定格式的,为了避免前端去转换时间参数的格式,跟前端约定好,让他们固定传递一种格式,后端自己看需求转换格式使用即可。效果①从yyyy-MM-ddHH:mm:ss转换成yyyy-MM-dd使用:②从yyyyMMddHHmmss转换成yyyy-MM-ddHH:mm:ss使用:③不再举例,其实就是自己想怎么转就怎么转。实战pom.xml(aop依赖、lombok依赖):o

JavaScript怎么用for求n的阶乘JavaScript怎么用for求n的阶乘Dec 08, 2021 pm 06:04 PM

用for求n阶乘的方法:1、使用“for (var i=1;i<=n;i++){}”语句控制循环遍历范围为“1~n”;2、循环体中,使用“cj*=i”将1到n的数相乘,乘积赋值给变量cj;3、循环结束后,变量cj的值就n的阶乘,输出即可。

SpringBoot中怎么利用AOP和拦截器实现自定义注解SpringBoot中怎么利用AOP和拦截器实现自定义注解May 29, 2023 pm 07:58 PM

Spring实现自定义注解通过拦截器+AOP实现自定义注解的实现,在这里拦截器充当在指定注解处要执行的方法,aop负责将拦截器的方法和要注解生效的地方做一个织入(通过动态注解生成代理类实现)。1.引入相关依赖spring-boot-starter:spring的一些核心基础依赖spring-boot-starter-aop:spring实现Aop的一些相关依赖org.springframework.bootspring-boot-starterorg.springframework.bootsp

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function