A couple of years ago, in 2009 to be precise, I came into contact with aspect-oriented programming (AOP) for the first time. That was a Java EE project in which JBoss Seam was used. Amongst others, the implementation used JBoss Seam Interceptors while EJB 3.0 introduced a standard interceptor model for session bean components.

The possibilities which were opened up by using such annotation based features were amazing. They allowed the encapsulation of cross-cutting concerns without having to change the affected code. By using AOP, in particular non-functional requirements can be realized, such as logging oder security checks. At the same time, the program logic remains lean.

Since I used to deal with PHP a lot, I was looking for an opportunity to use AOP within PHP code as well. Ideally a potential solution would meet the following requirements:

  1. provides annotation-based AOP features (without using placeholders for pointcuts)
  2. is lightweight and has as few dependencies as possible
  3. is easy to extend (with own interceptors)
  4. must not have additional requirements to the runtime (no pre-processors, no specific PHP extensions)
  5. only uses the standard functionality of PHP where possible
  6. considers the use of AOP as optional for each class, so the framework has to be integrated via its own autoloader
  7. keeps performance loss to a minimum

First of all, the annotation support had to be implemented. Since no suitable libraries for handling annotations in PHP (like in Java) existed back then, I started to write my own small framework which could read annotations from docblocks. I made some progress at that but I still hoped that one day PHP would support annotations out of the box. For a long time nothing happened within the PHP universe. When an RFC for annotation support in PHP was rejected in 2010, it seemed that PHP would remain free from annotations (and thus from interceptors and AOP) forever.

Meanwhile, there are some really cool frameworks which support AOP, first of all Doctrine2 and TYPO3 Flow. The trick is still easy: The annotations are placed within docblocks and can be read und evaluated via reflection. In particular, TYPO3 Flow comes closest to my idea of AOP (based on my experience with Java).

But these existing solutions had their own drawbacks.

TYPO3 Flow is a large and complex framework. In my opinion, it is less suitable to write small applications. If you want to use AOP features of TYPO3 Flow, you have to take the whole framework as a platform for your PHP application.

Furthermore, TYPO3 Flow works with pointcut designators which define the classes and methods to be considered. Although this corresponds to the AOP paradigm, it still differs from the interceptor model known from Java.

Hence I wanted to build my own solution step by step. This framework would fulfill the requirements and would be like AOP from the Java world. So this is article is just the first in a series of articles where we will describe the framework and the underlying concepts.

The framework is named Extended Application Repository, or Exar for short.

The first step will tackle the necessary condition, without which Exar could not work at all – a reflection API for PHP classes which supports reading and evaluation of annotations. This has not much to do with AOP itself, but it provides the foundation for it.

The next article will describe the implementation in greater detail. In the meantime feel free to fork Exar from Github https://github.com/techdev-solutions/exar-framework or checkout the website http://exarphp.com/

Continue with Part 2: Reading Annotations

Image courtesy of Viktor Hanáček

Stay in the loop

Join 1,000+ subscribers and get a new article every week