The AspectO project

aspecto_box

Some years ago (2005), I was reading about Aspect Oriented Programming (AOP), but I am very slow to understand how “new” programming paradigms work, so I began a personal project to understand how to implement AOP.

The first task was to choose a programming language, Java was a option, especially because AspectJ is the most mature project about AOP, but I don’t like Java very much, so I choose PHP, because its an easy to learn and teach language and one of the most popular in the internet, among other good and more reasonable reasons I will talk about in another post, with the language chosen, now I need to take a look at the AOP projects in PHP.

Looking for AOP projects for PHP, I found some of them in a very immature state (at that moment). One of the most interesting parts of the AOP is the weaver, so I was looking for one that doesn’t need a binary extension to work, thinking in all the users that cannot install extensions at their web hoster’s servers. The projects that I found for AOP in PHP with no binary extension for the weaver were what I expected, a giant failure. So I began (in my free time) what I named the AspectO project.

In general, AspectO is a free (LGPL) set of classes written in PHP5 to bring support to AOP. This classes provides a very simple and easy to learn syntax, doesn’t requires binary extensions additional to a common PHP5 installation, obtains the content of classes using PCRE and the PHP5′s Object Oriented Reflection, and finally weaves your Aspects preprocessing source code.

It was September 9th, 2006 when AspectO was released as a 0.1a version, but a more stable version (0.2b) was released in February 27, 2007.

How it works.

AspectO uses three basic directories, one for your Aspects files, another for your classes and a final one for the weaved classes. The way it works is setting the paths of your directories as properties of the AspectOConf object, the AspectO object receives the AspecOConf object and calls the AspectOParser and the AspectoClassParser to compare your Aspects versus your Classes, checks if they are compatible and finally return them as fixed objects to the AspectOWeaver, it weaves both objects (classes and aspects) and returns weaved classes in the weaved classes directory.

The syntax of an Aspect in AspectO is very simple and consists of joinpoints, pointcuts and advices. AspectO provides three types of joinpoints: execution, call and construction; and three types of advices: after, before and around. Let’s see a example.

The Aspect

aspect AspectExample {
  pointcut PointcutOneExe : execution( * ClassOne methodOne () );
  pointcut PointcutTwoCall : call( ClassOne methodTwo () );
  before : PointcutOneExe {
    echo 'This is before.';
  }
  after : PointcutTwoCall {
    echo 'This is after.';
  }
  around : PointcutOneExe {
    if ( $foo ) {
      echo 'Here';
      proceed();
      echo 'around.';
    }
    echo "It has been 'arounded'.";
  }
}

The source class

class ClassOne {
  public static function methodOne () {
    echo 'I am at the method one.';
  }
  public function methodTwo () {
    echo 'I am at the method two.';
  }
}

Executing AspectO

// the first argument (boolean) indicates if AspectO has to weave or not
$aConf = new AspectOConfig( true, 'aspects/', 'classes/', 'weaved_classes/' );

$aspecto = new AspectO( $aConf );

The results using the weaved class

ClassOne::methodOne();
// returns: This is before. Here I am method one around. It has been 'arounded'.

$one = new ClassOne();
$one->MethodTwo();
// returns: I am at the method two. This is after.

Inter-types and wildcard

AspectO also provides inter-type declarations and the wildcard character asterisk *. The inter-type declarations allows you to declare new properties, methods and inheritance (implementation of interfaces and extends of parent classes). The wildcard character * can be applied over existent classes and methods, but not to new ones.

Inter-type declarations

// The following declaration will add a new property to the Actions
// (or any other coincidence using the wildcard)
// class named $myNewVar with public visibility
public A*ions $myNewVar = 'something';

// The following declaration will add a new method to the Actions
// class named newMethod with static access,
// private visibility and the arguments $newVar and $newVarTwo
private static Actions newMethod ( $newVar, $newVarTwo ) {
  echo 'someAction in new method';
}

// The following declaration extends the Account class into the
// Actions class
declare parent : Actions extends Account;

// The following declaration implements the InterfaseTest interfase
// into the Actions class
declare parent : Actions implements InterfaceTest;

AspectO’s future

AspectO will be reviewed in order to release a more stable and useful version. The project has provided a lot of ideas for the development of a new framework named Tlalokes and possibly it will be used to bring AOP to this framework.

Basilio Briceño

DevOps evangelist, SoftwareLibre activist, sometimes speaker & eclectic metalhead.

1 comment

  1. Lau   •  

    The greatest things are for the greatest men…

    … keep moving!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>