The AOP Practice of PHP

Source: Internet
Author: User
Tags php introduction

Introduction to the AOP practice AOP in PHP

AOP is aspect oriented programming abbreviation, meaning: aspect-oriented programming (also known as aspect), through the pre-compilation method and run-time dynamic agent implementation of the program without modifying the source code in the case of dynamic Unified Add the function of a technology. 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.

aop-php Introduction

Aop-php is a pecl extension that allows you to use aspect-oriented programming in PHP without having to compile or do any other intermediate steps.

The design of the AOP extension is the simplest way, and you can think of the AOP implementation in PHP.

AOP is designed to allow for the separation of crosscutting concerns (caching, logging, security, trading, ...). )

Website: http://aop-php.github.io/

aop-php Installation and Installation

There are two modes of installation:

The first method:

sudo pecl install aop-beta  

The second method:

#Clone the repository on your computer    git Clone https://github.com/AOP-PHP/AOP    cd AOP    #prepare the package, You'll need to has development tools for PHP    phpize    #compile the package
./configure--WITH-AOP--with-php-config=/usr/bin/php-config
    Make    #before the installation, check this it works properly make    test    #install make    Install

Error handling

I have an error in the second method installation (if there are no errors here can float over):

Can ' t locate autom4te/c4che.pm in @INC (@INC contains:/usr/local/share/autoconf ...

The workaround is to reinstall Autoconf:

#wget HTTP://FTP.GNU.ORG/GNU/AUTOCONF/AUTOCONF-LATEST.TAR.GZ#TAR-ZXF autoconf-latest.tar.gz#rpm-qf/usr/bin/ autoconf #查看autoconf的版本 #rpm-e--nodeps autoconf-2.59-12 #卸载原来版本 #./configure--prefix=/usr#make && make install

After compiling the installation, it is necessary to load the module in php.ini, usually the PHP module in CentOS is loaded inside the/ETC/PHP.D, creating a new file Aop.ini with the following contents:

Extension=aop.so

After the installation is successful, look at Phpinfo and see the following:

aop-php Preschool Preparation

Professional terminology

Before we practice, we need to learn some of the terminology of HA AOP first.

Aspect (facets): a cross-sectional relationship is placed into a class in a group. Advice (Notification): Used to invoke a slice, define what to do and what time to do it in a certain situation. Notice is divided into: before notification, after return notification, after the notification and surrounding notification. Joinpoint (Access point): the location where the notification was created. Pointcut (Point Cutting): Defines a way to match notifications to certain access points. Refer to this blog: http://www.cnblogs.com/baochuan/archive/2012/08/22/2644529.html reference documentation After you have learned this knowledge, we also need to download the aop-php documentation.  Official documents downloaded, E Good can see the official documents, directly floating through the following text. Preparing files

Before we practice we need to prepare four files: Test the function file testfunction.php, test the class file testclass.php, test the AOP file testaop.php and run the file test.php.

Doing so can really simulate our project, and most of the projects are laid out like this.

Notice of aop-php Practice

Prior Notice Aop_add_before

A notification that is used before some special point in the code normally calls a method or function.

Let's test the function first.

testfunction.php Code:

<?phpfunction testFunc1 () {    echo ' Aop_add_before <br/> ';}

testaop.php Code:

<?php$testpoint1 = function () {echo "This is the pre-pointcut test function:";}; Aop_add_before (' testFunc1 () ', $testpoint 1);

test.php Code:

<?phprequire ' testaop.php '; require ' testclass.php '; require ' testfunction.php '; header ("content-type:text/html; Charset=utf-8 "); TestFunc1 ();

No accident, execute test.php we will see:

We'll play the class again.

testclass.php Code:

<?phpclass testclass1{public    function testBeforAdd1 ()    {        echo get_class ($this);}    }

testaop.php Code:

<?php$testpoint1 = function () {echo "This is the pre-pointcut test function:";}; $testpoint 2 = function () {echo "This is the pre-tangent test class method:";}; Aop_add_before (' testFunc1 () ', $testpoint 1), Aop_add_before (' testclass1->testbeforadd1 () ', $testpoint 2);

test.php Code:

<?phprequire ' testaop.php '; require ' testclass.php '; require ' testfunction.php '; header ("content-type:text/html; Charset=utf-8 "); TestFunc1 (); $testClass 1 = new TestClass1 (); Echo $testClass 1->testbeforadd1 ();

Executive test.php

Here is the pre-pointcut test function: Aop_add_before This is the pre-Tangency test class method: TestClass1
Re-test class properties

testclass.php Source

<?Php//Pre-Test notification classClasstestclass1{Publicfunction TestBeforAdd1 () {echo Get_class ($This).‘<br/>‘; } }//Notification class properties before testingClasstestclass2{Private$name;Public $publicProperty 1 =‘Test ' publicthis->name = $name;} public function getName () { return $this->name ; } public function test () {$ this->publicproperty1 = test return $this->publicproperty1;}}     

testaop.php Source

<?Php$testpoint 11 =function(){echo "This is the pre-pointcut test function:";};$testpoint 12 =function(){echo "This is the pre-tangent test class method:";}; Aop_add_before (' testFunc1 () ',$testpoint 11); Aop_add_before (' Testclass1->testbeforadd1 () ',$testpoint 12);//------Test Class PropertiesClasschangeproperty{PublicFunction Shoot ($who,$what) {If$what = = ' Test '){$what = ' Pre-test notification class attribute interception <br/> '; }echo "$who want to$what "; }}$testclass 1 =NewChangeProperty ();$testpoint 2 =function (Aopjoinpoint$AOP _TJP)Use$testclass 1){if ( $AOP _tjp->getkindofadvice () === Aop_kind_before_read_property) {return; // elseif ( $AOP _tjp->getkindofadvice () === Aop_kind_ Before_write_property) { $testclass 1->shoot ( $aop _ Tjp->getobject ()->getname (),  $AOP _tjp->// test class properties Aop_add_before (' testclass2-> PublicProperty1 ',  $testpoint 2);         

test.php Source

<?PhpRequire ' testaop.php 'require ' testclass.php ' ;; Header ("Content-type:text/html;charset=utf-8" ); //testfunc1 (); Span style= "color: #800080;" > $testClass 1 = new TestClass1 (); echo  $testClass 1->testbeforadd1 ();  $runtest 2 = new testClass2 (' Skyboy '  $runtest 2->test ();               

Executive test.php

Notify Aop_add_after after return

The notification that is used after some special point in the code is generally called a method or function.

Test function

testfunction.php Source:

function TestFunc2 () {    echo ' This is the return notification after the test: ';}  

testaop.php Source:

notify function  () {    echo "Aop_add_after <br/>"$testpoint)    after test returns;

test.php Source:

after notification testFunc2 ();

Executive test.php

This is the return notification after the test: Aop_add_after

  

Class and class properties are similar to previous notifications, and are lazy to save space.

Perimeter Notification Aop_add_around test function

testfunction.php Source:

function testFunc3 ($param 1,$param 2) {    $param 2;}    

testaop.php Source:

//Test perimeter Notificationsfunction Testaround (aopjoinpoint$object){$args =$objectGetarguments ();if ($args [0]!==Null) {$args [0] = ' I want to test '; }if ( $args [1]!== null< Span style= "color: #000000;" ) { $args [1] = ' perimeter notification: ' ;}  $object->setarguments ( $args );  $object->process ();  $returnValue =  $object getreturnedvalue ();  $returnValue. = ' aop_add_around<br/> '  $object->setreturnedvalue ( $returnValue            

test.php Source:

Peripheral Notice echo testFunc3 (UP); 

Executive test.php

I want to test perimeter notification: Aop_add_around

Class and class properties are similar to previous notifications.

aop-php function Description

In addition to the three important function aop_add_before,aop_add_after,aop_add_around, we also need to remember these important functions.

Getkindofadvice

Gets the type of notification. There are several default values. Generally used in the method of property changes.

Aop_kind_before before a given call, may it is function, method or property
Aop_kind_before_method before a method call (method of an object)
Aop_kind_before_function before a FUNCTION call (not a method call)
Aop_kind_before_property before a property (read or write)
Aop_kind_before_read_property before a property access (READ only)
Aop_kind_before_write_property before a property write (write only)
Aop_kind_around AROUND A given call, may it is function, method or property access (Read/write)
Aop_kind_around_method AROUND A method call (method of an object)
aop_kind_around_function AROUND a FUNCTION call (not a method call)
Aop_kind_around_property AROUND a property (read or write)
Aop_kind_around_read_property AROUND A property access (READ only)
Aop_kind_around_write_property AROUND A property write (write only)
Aop_kind_after after a given call, may it is function, method or property access (Read/write)
Aop_kind_after_method After a method call (method of an object)
Aop_kind_after_function after a FUNCTION call (not a method call)
Aop_kind_after_property after a property (read or write)
Aop_kind_after_read_property after a property access (READ only)
Aop_kind_after_write_property after a property write (write only)

Getarguments

Gets the parameters of the method. Generally used in Aop_add_before/aop_add_around.

Setarguments

Sets the parameters of the method. Generally used in Aop_add_before/aop_add_around.

Getreturnedvalue

Gets the return value of the method. Generally used in Aop_add_after/aop_add_around.

Setreturnedvalue

Sets the return value of the method. Generally used in Aop_add_after/aop_add_around.

Process

Let the method run. Generally used in Aop_add_around.

Please refer to the official documentation for detailed instructions.

Aop-php Open and close

Create a new file aopopenclose.php

The source code is as follows:

<?PhpIni_set ("Aop.enable", "1");echo "AOP is enabled<br/>";functionFoo () {echo "I ' m foo<br/>";}$adviceShowFoo =function {echo " After foo<br/> ";}; Aop_add_after (' foo () ',  $adviceShowFoo ini_set (' aop.enable ', ' 0 ' ;foo ();  echo "but can still register new aspects<br/>" ;aop_add_after (' f* () ',  $adviceShowFoo ini_set (' aop.enable ', ' 1 ' ;foo ();        

Operation Result:

aop is enabledI‘m fooAfter fooaop is now disabledI‘m fooAfter fooBut you can still register new aspectsI‘m fooAfter fooAfter fooAop is now enabledI‘m fooAfter fooAfter foo

  

aop-php Summary

Aop-php implements PHP's AOP in its true sense, and users don't have to do it in any other way. The idea of AOP programming is a razor that allows for easy decoupling of projects with poor coupling.

All test files and post-editing files are packaged. Click here to download. (php5.3 compilation based on Ceotos environment)

The AOP Practice of PHP

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.