PHP code example of next / aop

1. Go to this page and download the library: Download next/aop library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

next / aop example snippets


Aop::init(
    [__DIR__ . '/../app'],
    [
        \Next\Aop\Collector\PropertyAttributeCollector::class,
        \Next\Aop\Collector\AspectCollector::class,
    ],
    __DIR__ . '/../runtime/aop',
);



namespace App\aspects;

use Next\Aop\ProceedingJoinPoint;
use Next\Aop\Contract\AspectInterface;

#[\Attribute(\Attribute::TARGET_METHOD)]
class Round implements AspectInterface
{
    public function process(ProceedingJoinPoint $joinPoint): mixed
    {
        echo 'before';
        $result = $joinPoint->proceed(); // 直接调用被代理的方法
//        $result = $joinPoint->process(); // 继续执行其他切面逻辑
        echo 'after';
        return $result;
    }
}



namespace app\controller;

use App\Aop\Attribute\Inject;use App\aspects\Round;use support\Request;

class Index
{
    #[Inject]
    protected Request $request;

    #[Round]
    public function index()
    {
        echo '--controller--';
        return response('hello webman');
    }
}



namespace App\aspects;

use Next\Aop\Attribute\AspectConfig;
use Next\Aop\ProceedingJoinPoint;
use Next\Aop\Contract\AspectInterface;

#[\Attribute(\Attribute::TARGET_METHOD)]
#[AspectConfig('BaconQrCode\Writer', 'writeFile')]
class Round implements AspectInterface
{
    public function process(ProceedingJoinPoint $joinPoint): mixed
    {
        echo 'before';
        $result = $joinPoint->process();
        echo 'after';
        return $result;
    }
}


php 8.2
开启passthru函数
shell
php start.php start