Welcome

This demo shows your an examples of AOP usage.

Please, choose one of available examples from navigation menu.
You can also try to run this code with XDebug.

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...
  
<?php
/*
 * Go! AOP framework
 *
 * @copyright Copyright 2012, Lisachenko Alexander <lisachenko.it@gmail.com>
 *
 * This source file is subject to the license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Demo\Aspect;

use 
Go\Aop\Aspect;
use 
Go\Aop\Intercept\MethodInvocation;
use 
Go\Lang\Annotation\After;
use 
Go\Lang\Annotation\Before;
use 
Go\Lang\Annotation\Pointcut;

/**
 * Healthy live aspect
 */
class HealthyLiveAspect implements Aspect
{
    
/**
     * Pointcut for eat method
     *
     * @Pointcut("execution(public Demo\Example\HumanDemo->eat(*))")
     */
    
protected function humanEat() {}

    
/**
     * Washing hands before eating
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("$this->humanEat")
     */
    
protected function washUpBeforeEat(MethodInvocation $invocation)
    {
        
/** @var $person \Demo\Example\HumanDemo */
        
$person $invocation->getThis();
        
$person->washUp();
    }

    
/**
     * Method that advices to clean the teeth after eating
     *
     * @param MethodInvocation $invocation Invocation
     * @After("$this->humanEat")
     */
    
protected function cleanTeethAfterEat(MethodInvocation $invocation)
    {
        
/** @var $person \Demo\Example\HumanDemo */
        
$person $invocation->getThis();
        
$person->cleanTeeth();
    }

    
/**
     * Method that advice to clean the teeth before going to sleep
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("execution(public Demo\Example\HumanDemo->sleep(*))")
     */
    
protected function cleanTeethBeforeSleep(MethodInvocation $invocation)
    {
        
/** @var $person \Demo\Example\HumanDemo */
        
$person $invocation->getThis();
        
$person->cleanTeeth();
    }
}
<?php
/*
 * Go! AOP framework
 *
 * @copyright Copyright 2013, Lisachenko Alexander <lisachenko.it@gmail.com>
 *
 * This source file is subject to the license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Demo\Example;

/**
 * Human class example
 */
class HumanDemo
{

    
/**
     * Eat something
     */
    
public function eat()
    {
        echo 
"Eating..."PHP_EOL;
    }

    
/**
     * Clean the teeth
     */
    
public function cleanTeeth()
    {
        echo 
"Cleaning teeth..."PHP_EOL;
    }

    
/**
     * Washing up
     */
    
public function washUp()
    {
        echo 
"Washing up..."PHP_EOL;
    }

    
/**
     * Working
     */
    
public function work()
    {
        echo 
"Working..."PHP_EOL;
    }

    
/**
     * Go to sleep
     */
    
public function sleep()
    {
        echo 
"Go to sleep..."PHP_EOL;
    }
}