Home > Backend Development > PHP Tutorial > A first look at AOP FOR PHP_PHP tutorial

A first look at AOP FOR PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:52:06
Original
1036 people have browsed it



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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template