PHP 实现aop
PHP 实现 AOP 的方法
AOP(面向切面编程)是一种编程范式,允许开发者将横切关注点(如日志、事务、权限等)从业务逻辑中分离出来。PHP 本身不原生支持 AOP,但可以通过以下方式实现:
使用 Go! AOP 框架
Go! AOP 是一个流行的 PHP AOP 框架,基于 PHP 的反射和动态代理实现。
安装 Go! AOP:
composer require goaop/framework
示例代码:
use Go\Core\AspectContainer;
use Go\Core\AspectKernel;
class LoggingAspect implements \Go\Aop\Aspect
{
public function beforeMethod(\Go\Aop\Intercept\MethodInvocation $invocation)
{
echo "Calling method: " . $invocation->getMethod()->getName();
}
}
class ApplicationAspectKernel extends AspectKernel
{
protected function configureAop(AspectContainer $container)
{
$container->registerAspect(new LoggingAspect());
}
}
ApplicationAspectKernel::getInstance()->init([
'debug' => true,
'appDir' => __DIR__,
'cacheDir' => __DIR__ . '/cache'
]);
使用 PHP-DI 和 AOP 插件
PHP-DI 是一个依赖注入容器,支持通过 AOP 插件实现切面编程。
安装 PHP-DI 和 AOP 插件:

composer require php-di/php-di
composer require php-di/aop-bridge
示例代码:
use DI\ContainerBuilder;
use DI\Aop\AspectKernel;
class LoggingAspect
{
public function before()
{
echo "Before method execution";
}
}
$builder = new ContainerBuilder();
$builder->useAutowiring(true);
$builder->useAnnotations(true);
$builder->enableAop();
$container = $builder->build();
AspectKernel::getInstance()->registerAspect(new LoggingAspect());
手动实现代理模式
如果不依赖框架,可以通过手动实现代理模式来模拟 AOP。
示例代码:

interface ServiceInterface
{
public function doSomething();
}
class RealService implements ServiceInterface
{
public function doSomething()
{
echo "Real work done";
}
}
class ProxyService implements ServiceInterface
{
private $realService;
public function __construct(RealService $realService)
{
$this->realService = $realService;
}
public function doSomething()
{
echo "Before method call";
$this->realService->doSomething();
echo "After method call";
}
}
$realService = new RealService();
$proxy = new ProxyService($realService);
$proxy->doSomething();
使用注解和反射
通过 PHP 的反射和自定义注解实现简单的 AOP。
示例代码:
class Loggable
{
public function __invoke($target, $method, $args)
{
echo "Before " . $method;
$result = call_user_func_array([$target, $method], $args);
echo "After " . $method;
return $result;
}
}
class SomeService
{
/
* @Loggable
*/
public function importantMethod()
{
echo "Important work";
}
}
$service = new SomeService();
$reflection = new ReflectionMethod($service, 'importantMethod');
$attributes = $reflection->getAttributes();
foreach ($attributes as $attribute) {
$aspect = $attribute->newInstance();
$aspect($service, 'importantMethod', []);
}
使用 Symfony 的 AOP 组件
Symfony 框架通过 EventDispatcher 组件提供类似 AOP 的功能。
示例代码:
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
class UserEvent extends Event
{
public const NAME = 'user.action';
private $user;
public function __construct($user)
{
$this->user = $user;
}
}
$dispatcher = new EventDispatcher();
$dispatcher->addListener(UserEvent::NAME, function (UserEvent $event) {
echo "User action logged";
});
$dispatcher->dispatch(new UserEvent('test'), UserEvent::NAME);
以上方法提供了不同复杂度的 PHP AOP 实现方案,可根据项目需求选择合适的方式。框架集成方案(如 Go! AOP 或 PHP-DI)适合大型项目,而手动实现适合简单场景或学习目的。






