AOP 實現肯定有的,建議用 composer 然後在packagist.org能搜尋很多,建議用 PSR-14 規範的實現。
PSR-14 實現的元件清單:
https://packagist.org/providers/psr/event-dispatcher-implementation
我用過的,推薦的:
phly/phly-event-dispatcher 使用事例:
use Psr\EventDispatcher\EventDispatcherInterface;
class UpdatedEvent {
public __construct(array $user) {}
public getData() {}
}
class UserController {
public __construct(EventDispatcherInterface $events) {
$this->events = $events;
public edit() {
//...
$this->events->dispatch(new UpdatedEvent($user));
// IoC 構建 UserController 程式碼
function ($container) {
$events = $container->get(EventDispatcherInterface::class);
$events->listen(ErrorEvent::class, function (UpdatedEvent $event) {
// todo ....
});
return new UserController ($events);
其它非 psr14 實現的:
AOP 實現肯定有的,建議用 composer 然後在packagist.org能搜尋很多,建議用 PSR-14 規範的實現。
PSR-14 實現的元件清單:
https://packagist.org/providers/psr/event-dispatcher-implementation
我用過的,推薦的:
phly/phly-event-dispatcher (輕量)symfony/event-dispatcher (熱度高)phly/phly-event-dispatcher 使用事例:
use Psr\EventDispatcher\EventDispatcherInterface;
class UpdatedEvent {
public __construct(array $user) {}
public getData() {}
}
class UserController {
public __construct(EventDispatcherInterface $events) {
$this->events = $events;
}
public edit() {
//...
$this->events->dispatch(new UpdatedEvent($user));
}
}
// IoC 構建 UserController 程式碼
function ($container) {
$events = $container->get(EventDispatcherInterface::class);
$events->listen(ErrorEvent::class, function (UpdatedEvent $event) {
// todo ....
});
return new UserController ($events);
}
用 PSR 14 介面,再配合 IoC ,能提高程式碼獨立性,易維護。其它非 psr14 實現的:
evenement/evenement (輕量,簡單)