phpAspect Logo

Chapter 4. Writing Aspects

phpAspect extends the syntax of the php compiler in order to introduce a new software entity called aspects. All aspects must be written in a file with the .aspect.php extension.

In a aspect.php file, all the code outside the aspect entity will be ignored by phpAspect.

Example 4.1: Your first aspect with phpAspect

<?php
aspect MyCrosscuttingConcern{
 
    //Attributes
    //Methods
 
    //Poincuts
    //Advices
    //Inter-type declarations
}
//The code outside the aspect will be ignored
?>


Aspects are first classes entities during the execution. During the execution of your program, aspects are represented by classes having attributes and methods.

This mean that you can write user defined methods, attributes and contants into aspects.

Example 4.2: Aspects are first classes entities during the execution

<?php
aspect ObserverPattern{
 
    private $observers = array();
 
    public function addObserver(Observer $o){
        $this->observers[] = $o;
    }
}
?>


Note

All aspects are instanciate with a singleton pattern when the program execution starts.