The AOP Practice of PHP

Source: Internet
Author: User
Tags php introduction

Introduction to AOP

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//Notify class testclass1{public    function testBeforAdd1 ()    {        echo get_class ($this) before testing. <br/> ';    }        } Pre-Test Notification Class Property class testclass2{    private $name;    Public $publicProperty 1 = ' test ';    Public function __construct ($name)    {        $this->name = $name;    }    Public Function GetName ()    {        return $this->name;    }    Public function test ()    {        $this->publicproperty1 = ' Test ';        return $this->publicproperty1;    }        }

testaop.php Source

<?php$testpoint11 = function  () {    echo "This is the pre-pointcut test function:";}; $testpoint = function  () {    echo "This is the pre-tangent test class method:";}; Aop_add_before (' testFunc1 () ', $testpoint one), Aop_add_before (' testclass1->testbeforadd1 () ', $testpoint);//--- ---Test class changeproperty{public    function Shoot ($who, $what)    {        if ($what = = ' Test ') {            $what = ' Pre-Test notification class attribute interception <br/> ';        }        echo "$who want to $what";    }} $testclass 1 = new ChangeProperty (), $testpoint 2 = function  (aopjoinpoint $AOP _tjp) use ($testclass 1) {    if ($ao P_tjp->getkindofadvice () = = = Aop_kind_before_read_property)    {        return;//return if the property cannot be read    }    ElseIf ($aop _tjp->getkindofadvice () = = = Aop_kind_before_write_property)    {        $testclass 1->shoot ($AOP _tjp->getobject ()->getname (), $AOP _tjp->getassignedvalue ());    }};/ /test class attribute Aop_add_before (' testclass2->publicproperty1 ', $testpoint 2);

test.php Source

<?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 (), $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:

The test returns notification $testpoint22 = function  () {    echo "Aop_add_after <br/>";}; Aop_add_after (' TestFunc2 () ', $testpoint 22);

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) {    return $param 1. $param 2;}

testaop.php Source:

Test peripheral notification Function Testaround (Aopjoinpoint $object) {    $args = $object->getarguments ();    if ($args [0]!== null) {        $args [0] = ' I want to test ';    }    if ($args [1]!== null) {        $args [1] = ' perimeter notification: ';    }    $object->setarguments ($args);    $object->process ();        $returnValue = $object->getreturnedvalue ();    $returnValue. = ' aop_add_around<br/> ';    $object->setreturnedvalue ($returnValue);    } Aop_add_around (' testFunc3 () ', ' testaround ');

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/>"; function foo () {    echo "I ' m foo<br/>";} $adviceShowFoo = function  () {    echo "after foo<br/>";}; Aop_add_after (' foo () ', $adviceShowFoo); foo (); Ini_set (' aop.enable ', ' 0 '); echo "AOP is now disabled<br/>"; foo (); echo "but can still register new aspects<br/>"; Aop_add_after (' f* () ', $adviceShowFoo); foo (); Ini_set (' Aop.enable ', ' 1 '); echo "Aop is now enabled<br/>"; 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)

Link

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.