A preliminary study of the AOP for php_php tutorial

Source: Internet
Author: User
Problem
An approach to AOP for PHP
Workaround


AOP is the continuation of OOP and is the abbreviation of Aspect oriented programming, meaning aspect-oriented programming. AOP is actually a continuation of the GOF design pattern, and the design pattern pursues the decoupling between the caller and the callee, and AOP can be said to be an implementation of this goal. In fact, this technology has appeared long ago. I was in the 06 when Google was the technology.
It is my understanding that the function will be cross-cut without destroying the original method or class. Then add your own method to deal with it. For example, we often have some methods to do before the permission to judge. After processing, log writes are performed, and so on. The general way of operation is in the head and bottom of the method of the write processing process. This undermines the principle of the single function of OOP. Because when there are 100 or even 1000 ways to do the same thing, there will inevitably be some unnecessary errors. This is the practical use of AOP ...
We write a function to output some information, before processing, do not want to have no permission to see the information of the people to look at. Some commonly used cache information may be written after processing. The usual notation is this class onetest{

Public Function GetInfo () {

Check Permissions

Acl::checkrole ();

Doing the process of obtaining information;

.....

Write Cache

Cache::writecache ();

}

}


Copy code What if there are 1000 ways to do the same thing? A change is changed everywhere. The hard-to-maintain situation arises. And the AOP approach to dealing with this problem requires just this class onetest{

Public Function GetInfo () {

Doing the process of obtaining information;

.....

}

}


Copy the code so that two destruction packages are taken out of the unified definition in other places .... That's basically what this means. Of course, the fraught is purely coincidental.
Detailed introduction everyone can Google. Some information has not been collated ..... This is the time to sneak out of the whole.
Here are a few links to PHP. Be interested to have a look
An approach to AOP for PHP
[url=http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377.aspx] link marker 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 marker http://hi.baidu.com/ Thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html[/url]
And then I made it easy for you.

/**

* tsaspect{

* AOP for PHP

* @package

* @version $id $

* @copyright 2009-2011 Sampeng

* @author Sampeng <[email=sampeng87@gmail.com]sampeng87@gmail.com[/email]>

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

*/

Class tsaspect{

/**

* Instance

* Method belongs to object reference

* @var Mixed

* @access Private

*/

Private $instance;

/**

* Method

* Method Name

* @var Mixed

* @access Private

*/

Private $method;



/**

* Aspect

* Tangent Save Array

* @var Array

* @access Private

*/

Private $aspect = Array ();



/**

* __construct

* Constructors find out how to implement all 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

* Method Collection before methods are executed

* @static

* @access Public

* @return void

*/

protected function beforefunction () {

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

return $result;

}



/**

* Afterfunction

* Method Collection after methods are executed

* @static

* @access Public

* @return void

*/

protected function afterfunction () {

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

}



/**

* Findfunction

* Find a collection of all aspect methods.

* @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 Insert 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);

}

Processing a processed collection

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 "This is a test AOP";

}



public static function before () {

echo "method before execution";

}

public static function after () {

echo "Method after execution
";

}

}



Class test{

Public Function Samtest ($arg) {

echo "This is a test method";

}

}



$test = new test ();

$aspect = new Tsaspect ($test, ' samtest ');

$aspect->callaspect ();


Copy code output:
Before the method executes
This is a test method
After the method executes


The netizen constructs the meaning:
Do not know,,, to develop ideas useful?
The netizen constructs the meaning:
Thought? This is actually a design pattern .... To decouple the part of a function that destroys functions. There are many ways to achieve this. Of course, PHP is quasi-AOP. Because he can't compile a language like Java to insert a cross-sectional process at compile time.
The netizen constructs the meaning:
New things are always a gorgeous disregard ...
The netizen constructs the meaning:
Last year, we know that the aspect-oriented programming,,, not use,,,, hehe,,,
The netizen constructs the meaning:
Just touch-oriented programming, Haskell, Erlang
The netizen constructs the meaning:
PHP is much simpler than Java to implement AOP. Because there's runkit.
The netizen constructs the meaning:
I don't even know what OOP is. Technology AH. Can't catch up.

http://www.bkjia.com/PHPjc/632517.html www.bkjia.com true http://www.bkjia.com/PHPjc/632517.html techarticle An approach to the problem of AOP for PHP solution AOP is the continuation of OOP and is the abbreviation of Aspect oriented programming, meaning aspect-oriented programming. AOP is actually a continuation of GOF design patterns, design patterns ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.