Implementation of one class of AOP in PHP-PHP source code

Source: Internet
Author: User
Implementation of a class of AOP in PHP

1. [Code]Core
 Teardown --> open --> setup --> packing. ** for example, if you call source-> doXX (), each process is: * unpacking: unpacking. this is the method that will be called to call any source function; * teardown: tear off the ornaments. for each Decoration in Wrapper, call its before () function; * open: actually calls the source-> doXX () function; * setup: Re-attaches the ornaments. for each Decoration in Wrapper, call its after () function; * packing: repackage. this is a method that can be called by any source function. **/class Wrapper {private $ source;/*** @ var bool */private $ undecorated; /*** @ var array [Decoration] */private $ Decorations = array (); public function _ construct ($ source) {$ this-> source = $ source;} public function _ call ($ name, $ parameters) {$ this-> unpacking ($ name, $ parameters); $ this-> tearDown ($ name, $ parameters); // openingif (method_exists ($ this-> source, $ name) {$ retval = call_user_func_array (array ($ this-> source, $ name), $ parameters) ;}$ this-> setup ($ retval, $ name, $ parameters); $ this-> packing ($ retval, $ name, $ parameters ); Return $ retval;} public function unpacking ($ name, $ parameters) {} public function packing ($ name, $ parameters) {} public function tearDown ($ name, $ parameters) {if ($ this-> undecorated) {return;} foreach ($ this-> decorations as $ d) {$ d-> before ($ name, $ parameters );}} public function setup ($ retval, $ name, $ parameters) {if ($ this-> undecorated) {return;} foreach ($ this-> decorations as $ d) {$ d-> after ($ retval, $ name, $ parameters) ;}} Public function decarate ($ decoration) {$ this-> decorations [] = $ decoration;} public static function wrap ($ source) {// wrap the source $ wrapperConfig = app ()-> wrappers [get_class ($ source)]; if ($ wrapperConfig) {$ wrapperClass = $ wrapperConfig ['class']; $ wrapper = new $ wrapperClass ($ source); foreach ($ wrapperConfig ['records'] as $ item) {$ decoration = new $ item; $ wrapper-> decarate ($ decoration) ;}} return $ wra Pper? $ Wrapper: $ source ;}}?>

2. [Code]Configuration
'wrappers'=>array('ContentService'=>array('class'=>'ContentWrapper','decorations'=>array('DasaiContentDecoration',)),'AOPWorker'=>array(//for test'class'=>'DiagnosisWrapper','decorations'=>array('DasaiDiagnosisDecoration'),),),

3. [Code]Test Code
Class AOPWorker {public function testAOP () {Debugger: print_r ("\ n worker: I have done a lot of operations \ n worker :...... \ n worker: finished \ n "); return 'OK' ;}} public function testAOP () {// test aop test entry $ aop = Wrapper :: wrap (new AOPWorker (); $ aop-> testAOP (33347);} class DiagnosisWrapper extends Wrapper {public function unpacking ($ name, $ parameters) {echo "\ nDiagnosisWrapper: hello, someone called $ name. I want to unpack it. \ n ";} public function packing ($ retval, $ name, $ parameters) {echo" \ nDiagnosisWrapper: Hello, call $ name and the result is $ retval. the package is repackaged. \ n ";}} class DasaiDiagnosisDecoration extends Decoration {public function before ($ name, $ parameters) {echo" \ r \ nDasaiDiagnosisDecoration: start to call $ name. Michael has already told James. \ n ";} public function after ($ retval, $ name, $ parameters) {echo" \ nDasaiDiagnosisDecoration: ends the call of $ name and tells Hawking and Sheldon. \ n ";}}

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.