Aspect-oriented programming & PHP

Other
Aspect-oriented programming & PHP

Aspect-oriented programming (AOP) is a programming paradigm that allow to increase modularity by provide better options regarding the separation of cross-cutting concerns.

PHP currently hasn't native support for AOP, but from PHP v5.4 are at least are few solutions provided by community that can support AOP usage in PHP.

 

A good choice to apply AOP in PHP (>= v5.4) is use Go! AOP-PHP library. Before give some examples of code i would like to bring into discussion some keywords from AOP paradigm.

 

Cross-cutting concerns

Even though most classes in an OOP model will perform a single, specific function also called a concern, they often share common or secondary requirements with other classes also called cross-cutting concern.

 

Advice

This is the additional code(common code) that you want to apply to your existing model(usually TOP model).

 

Pointcut

This define the point of execution in the application at which cross-cutting concern needs to be applied.

 

Aspect

The most important term represents the combination of the pointcut and the advice.

 

As I said earlier in order to make a quick example, i used WAMP on Windows with PHP 5.4 and Go! AOP-PHP library. I installed Go! AOP-PHP using composer because this library has 3 additional dependencies, and composer will configure this for you, instead of doing this manually. After installing you can run the default demos with library came, located in demos subdirectory.

 

The best examples in my opinion, are the one with the "fluent interface" and the one with the "human". So in order to have fluent interface(also known as method chaining) in OOP, particulary in PHP we need to return $this which keep reference to the current object on each method. This sound like duplicate code in some matter and AOP it help us to not write that code on each method but still to be able to use method chaining. This can be achive by implement an Aspect called "FluentInterfaceAspect" wich contain an method called "aroundMethodExecution" which has the following implementation:

       
    /**
     * Fluent interface advice
     *
     * @Around("within(Aspect\FluentInterface+) && execution(public **->set*(*))")
     *
     * @param MethodInvocation $invocation
     * @return mixed|null|object
     */
    protected function aroundMethodExecution(MethodInvocation $invocation)
    {
        $result = $invocation->proceed();
        return $result!==null ? $result : $invocation->getThis();
    }

So this method check if the target method return null value which is default value if in our method we not have any return statement, and instead of let the target method to return null will change return value with $this (reference to the current object), and method chaining will be possible to use.

 

The other example that clearly illustrates the benefits of AOP is example with "human" class. Let's imagine that we have a class called Human with the following methods: eat, cleanTeeth, washUp, work and sleep.

Now we will call some methods from that class after this testing scenario:

    $man = new Example\Human();
    echo "Want to eat something, let's have a breakfast!", PHP_EOL;
    $man->eat();
    echo "I should work to earn some money", PHP_EOL;
    $man->work();
    echo "It was a nice day, go to bed", PHP_EOL;
    $man->sleep();  

The output of this scenario will be:

Want to eat something, let's have a breakfast!
Washing up...
Eating...
Cleaning teeth...
I should work to earn some money
Working...
It was a nice day, go to bed
Cleaning teeth...
Go to sleep...

Even though we called in the test scenario only 3 methods, because of the Aspect created for this class, the AOP library automaticly called for us before eat method the washUp and after eat method the cleanTeeth method. Also before sleep method the cleanTeeth method was called again, in order to make a more complex cycle for a day of human life.

 

In the end we can say that AOP allow to alter the regular execution flow of the application but allow you that without change any single line from application structure, all you need is to define an "Aspect" somewhere in a separate file. Few general examples where usually is used are: implement method chaining, login or log systems.

In other words AOP allow you to prevent much better code duplication and allow a better separation between clases/objects, by allowing the separation of cross-cutting concerns.

 

The detailed info about Go! AOP-PHP library can be found here and more info about AOP paradigm can be found here.

Add comment
 
 
 

Comments