Heim  >  Artikel  >  Backend-Entwicklung  >  初探AOP FOR PHP_PHP教程

初探AOP FOR PHP_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:52:06981Durchsuche



问题
初探AOP FOR PHP
解决方法


AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向方面编程。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。其实这个技术在很久前就出现了。我google的时候已经是06年就出现的技术。
用我的理解是,在不破坏原有方法或者类的时候将函数的进行横向切面。然后加进自己的方法处理。比如说,我们经常有一些方法执行之前要进行权限判断。处理之后要进行日志写入等等操作。一般的操作方式是在方法的头部和底部写处理过程。这就破坏了OOP的单一功能的原则。因为当有100甚至1000个方法要许进行同样处理的时候,难免会出现一些不必要的错误。这就是AOP的实际运用。。。
我们写一个函数输出一些信息的时候,在处理前,不希望没有权限看到这些信息的人来看。处理之后可能会写入一些常用缓存信息。通常的写法是这样 class OneTest{

public function getInfo(){

//检查一下权限

ACL::checkRole();

//做获取信息的动作过程;

.....

//写入缓存

Cache::writeCache();

}

}


复制代码如果有1000个这样需要做同样操作的方法怎么办呢。。一处修改处处修改。难维护的情况就出现了。而用AOP的办法去处理这个问题,就只需要这样 class OneTest{

public function getInfo(){

//做获取信息的动作过程;

.....

}

}


复制代码这样两个破坏封装的就拿出来在其他地方统一定义了。。。。。大体就是这么一个意思。当然理解错误,误人子弟纯属巧合。
详细介绍大家可以google。。一些资料还没整理。。。。。这还是忙里偷闲的时候整出来的。
下面是几个关于php的链接。有兴趣的可以看一下
AOP FOR PHP探讨
[url=http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377.aspx]链接标记http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377.aspx[/url]
PHP准AOP实现
[url=http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html]链接标记http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html[/url]
然后自己简单实现了一下

/**

* 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

* 方法所属对象引用

* @var mixed

* @access private

*/

private $instance;

/**

* method

* 方法名

* @var mixed

* @access private

*/

private $method;



/**

* aspect

* 切面保存数组

* @var array

* @access private

*/

private $aspect = array();



/**

* __construct

* 构造函数查找出所有Aspect的实现方法

* @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

* 方法之前执行的方法集合

* @static

* @access public

* @return void

*/

protected function beforeFunction(){

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

return $result;

}



/**

* afterFunction

* 方法之后执行的方法集合

* @static

* @access public

* @return void

*/

protected function afterFunction(){

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

}



/**

* findFunction

* 查找所有的Aspect的方法集合.

* @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

* 调用插入的方法

* @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.htmlTechArticle问题 初探AOP FOR PHP 解决方法 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向方面编程。AOP实际是GoF设计模式的延续,设计模式...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn