Mastering Go! AOP: A Step-by-Step Guide to Aspect-Oriented Programming in PHP

This article introduces the Go! AOP framework for PHP, outlines its key features, explains core AOP concepts, and provides a detailed, code‑rich tutorial on installing the library, creating an aspect kernel, defining aspects, and configuring optional caching options.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering Go! AOP: A Step-by-Step Guide to Aspect-Oriented Programming in PHP

Overview

Go! AOP is a PHP framework that enables aspect‑oriented programming without modifying original source files, without requiring PECL extensions or a dependency‑injection container.

Key Features

Dynamic hook system for public, protected, static and final class methods.

Interception of methods defined in traits.

Interception of public/protected property access.

Hook for static class initialization (after class is loaded into memory).

Hook for object initialization (intercepts the new keyword).

Interception of built‑in PHP functions.

Advice types include @Before, @After, @Around allowing modification of return values.

Rich pointcut expression syntax for declaring join points directly in source.

Native debugging with Xdebug; woven code remains readable and breakpoints work.

Integrates with any existing PHP framework or library, with or without extra configuration.

Production‑ready optimizations: opcode‑cache support, lazy loading, join‑point caching, no runtime pointcut checks, no eval, no __call, no call_user_func_array, fast bootstrap (2–20 ms) and efficient advice invocation.

Aspect‑Oriented Programming Basics

In AOP, a join point is a specific execution point such as a method call. A pointcut is a collection of join points defined by an expression. Advice is the code that runs before, after, or around those points. An aspect groups related advice and pointcut definitions.

Installation

Step 1 – Install with Composer

composer require goaop/framework

The package is placed in vendor/goaop/framework.

Step 2 – Create an Application Aspect Kernel

Create a class that extends Go\Core\AspectKernel and implement configureAop() to register aspects.

<?php
// app/ApplicationAspectKernel.php

use Go\Core\AspectKernel;
use Go\Core\AspectContainer;

/**
 * Application Aspect Kernel
 */
class ApplicationAspectKernel extends AspectKernel
{
    /**
     * Configure the AspectContainer with aspects and pointcuts.
     *
     * @param AspectContainer $container
     */
    protected function configureAop(AspectContainer $container)
    {
        // register aspects here
    }
}

Step 3 – Initialise the Kernel in the Front Controller

Include the Composer autoloader and call init() on the kernel instance. The configuration array accepts the following keys: debugtrue for development, false for production. appDir – absolute path to the application root. cacheDir – directory where woven classes are cached. includePaths – list of directories that should be scanned for aspects (empty means all source files).

// front‑controller (e.g., Symfony2 web/app_dev.php)
include __DIR__ . '/vendor/autoload.php'; // Composer autoloader

$kernel = ApplicationAspectKernel::getInstance();
$kernel->init([
    'debug'        => true,
    'appDir'       => __DIR__ . '/../',
    'cacheDir'     => __DIR__ . '/cache/aop',
    'includePaths' => [__DIR__ . '/../src/'],
]);

Step 4 – Define an Aspect

An aspect is a regular PHP class that implements Go\Aop\Aspect. Advice methods are annotated with pointcut expressions. The example below logs method calls before execution of any public method in the Example class.

// src/Aspect/MonitorAspect.php
namespace Aspect;

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

class MonitorAspect implements Aspect
{
    /**
     * @Before("execution(public Example->*(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        echo 'Calling Before Interceptor for: ', $invocation,
             ' with arguments: ', json_encode($invocation->getArguments()), "
";
    }
}

Step 5 – Register the Aspect in the Kernel

Add the aspect instance to the container inside configureAop():

use Aspect\MonitorAspect;

protected function configureAop(AspectContainer $container)
{
    $container->registerAspect(new MonitorAspect());
}

Step 6 – Optional Cache Configuration

By default Go! AOP uses Doctrine\Common\Cache\FilesystemCache for annotation caching. Any cache that implements Doctrine\Common\Cache\Cache can be supplied via the annotationCache option in the kernel init array. For read‑only file systems an in‑memory cache such as ArrayCache is common:

'annotationCache' => new \Doctrine\Common\Cache\ArrayCache(),

Production Considerations

The framework avoids runtime pointcut checks, eval, __call and call_user_func_array, supports opcode caches, lazy loading and join‑point caching, resulting in a bootstrap time of 2–20 ms and efficient advice execution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendaopPHPFrameworkAspect Oriented ProgrammingGo!AOP
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.