4.3. Aspect using S2AopFactory

By the create method of S2AopFactory class, the extended class of an aspect target class is generated and you are able to get the instance into which the aspect was woven.

public static function create(ReflectionClass $targetClass, array $aspects, array $args = array(), array $parameters = array()) 
  • The 1st argument : Aspect target class
  • The 2nd argument : Aspect woven in
  • The 3rd argument : Arguments of constructor method
  • The 4th argument : Additional parameter of S2Aop.PHP

As an example, an aspect is applied to the following service classes.

class Service {
    public function add($a, $b) {
        return $a + $b;

TraceInterceptor is aspected to add method of Service class as follws.

seasar\util\ClassLoader::import(dirname(__FILE__) . '/classes');

$interceptor = new seasar\aop\interceptor\TraceInterceptor;
$pointcut    = new seasar\aop\Pointcut('^add$');
$aspect      = new seasar\aop\Aspect($interceptor, $pointcut);
$service     = S2AopFactory::create(new ReflectionClass('Service'), array($aspect));

print get_class($service) . PHP_EOL;
$result = $service->add(2, 3);


Interceptor is a class which implements seasar\aop\MethodInterceptor interface. These next interceptors are attached to S2Aop.

  • seasar\aop\interceptor\TraceInterceptor
    TraceInterceptor is a interceptor for treating trace processing as "Crosscutting Concern".
  • seasar\aop\interceptor\MockInterceptor
    MockInterceptor is a interceptor for doing the test using Mock simply.
  • seasar\aop\interceptor\InterceptorChain
    InterceptorChain is a interceptor which makes two or more interceptors a group and thereby, reuse of interceptor is simplified.


Pointcut is a class showing where MethodInterceptor is applied.

 * Construct Pointcut
 * @param mixed $target

The regular expression which specifies Pointcut or ReflectionClass is passed to a constructor argument. If ReflectionClass is passed, and if the class implements abstract method, all the abstract method is be Pointcut. If the class does not implement abstract method at all, all the public method implemented in the class except the following conditions is be Pointcut.

  • The method which the parent class implements is excepted
  • The method from which a name starts with "set", "get" and "is" is excepted.


Aspect is a class which associates MethodInterceptor(Advice) and Pointcut.

 * Construct Aspect
 * @param seasar\aop\MethodInterceptor $methodInterceptor
 * @param seasar\aop\Pointcut $pointcut
seasar\aop\Aspect::__construct(MethodInterceptor $methodInterceptor, Pointcut $pointcut)

MethodInterceptor and Pointcut are specified by a constructor argument.

© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved.