Preliminary Research on AOP FOR PHP

Source: Internet
Author: User



Problem
Preliminary Research on AOP FOR PHP
Solution


AOP is a continuation of OOP and short for Aspect Oriented Programming, which means Aspect-Oriented Programming. AOP is actually a continuation of the GoF design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal. In fact, this technology appeared a long time ago. I used google technology in.
In my understanding, the horizontal cut of the function is implemented without damaging the original method or class. Then add your own method for processing. For example, we often have some methods for permission judgment before execution. Write logs after processing. The general operation is to write the processing process at the header and bottom of the method. This undermines the single function principle of OOP. Because when 100 or even 1000 methods require the same processing, unnecessary errors may inevitably occur. This is the practical application of AOP...
When writing a function to output some information, we do not want anyone who has no permission to view the information before processing it. Some common cache information may be written after processing. This is usually the way class OneTest {

Public function getInfo (){

// Check the permission

ACL: checkRole ();

// The process of obtaining information;

.....

// Write Cache

Cache: writeCache ();

}

}


Copy the code. What if there are 1000 such methods that require the same operation .. Make changes everywhere for one change. Difficult to maintain. To solve this problem using the AOP method, you only need the class OneTest {

Public function getInfo (){

// The process of obtaining information;

.....

}

}


Copy the code so that the two broken packages are defined in other places ..... This is basically the meaning. Of course, misunderstanding is a coincidence.
For more information, see google .. Some documents have not been sorted out ..... This was made during the busy time.
Below are several links to php. If you are interested, take a look.
Discussion on AOP FOR PHP
[Url = Response
PHP quasi-AOP implementation
[Url = Response
Then I implemented it myself.

/**

* 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 Object Reference

* @ Var mixed

* @ Access private

*/

Private $ instance;

/**

* Method

* Method name

* @ Var mixed

* @ Access private

*/

Private $ method;



/**

* Aspect

* Save an array of cut planes

* @ Var array

* @ Access private

*/

Private $ aspect = array ();



/**

* _ Construct

* Constructor finds the implementation methods of 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

* Set of methods executed before the Method

* @ Static

* @ Access public

* @ Return void

*/

Protected function beforeFunction (){

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

Return $ result;

}



/**

* AfterFunction

* Set of methods executed after the Method

* @ Static

* @ Access public

* @ Return void

*/

Protected function afterFunction (){

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

}



/**

* FindFunction

* Search for a set 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 );

}

// Process the processed set

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 of AOP ";

}



Public static function before (){

Echo "before method execution ";

}

Public static function after (){

After echo "method execution
";

}

}



Class test {

Public function samTest ($ arg ){

Echo "this is a test method ";

}

}



$ Test = new test ();

$ Aspect = new TSAspect ($ test, 'samtest ');

$ Aspect-> callAspect ();


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


Netizen's suggestion:
Don't understand, isn't it useful for development ideas?
Netizen's suggestion:
Thoughts? This is actually a design model .... Coupling the parts that destroy a single function .. There are many specific implementation methods .. Of course PHP is quasi-AOP .. This is because he cannot insert the crosstab chart processing process when compiling a compilation language like java.
Netizen's suggestion:
New things are always ignored...
Netizen's suggestion:
Last year, I learned about Aspect-Oriented Programming ,,,
Netizen's suggestion:
I just got started with functional programming, Haskell, Erlang
Netizen's suggestion:
PHP implementation of AOP is much simpler than Java, because runkit is available.
Netizen's suggestion:
I don't know much about OOP. Technology. Cannot catch up.

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.