Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Aspect Oriented Programming in PHP

Aspect Oriented Programming in PHP

AOP is a programming paradigm that aims to separate supporting functionality from the business logic. In this talk I'll explain in more detail what AOP is, how we can apply it in PHP, and what kind of solutions are currently available.

Link to Go! AOP: http://go.aopphp.com/
Link to PHP-AOP: http://pecl.php.net/package/AOP

Frank van den Brink

June 14, 2013
Tweet

More Decks by Frank van den Brink

Other Decks in Programming

Transcript

  1. class SecretService { public function getSecret($id) { // Check access

    if (!$this->isAllowedToAccess($id)) { throw new AccessDeniedException('You cannot do this!'); } // Get the secret $secret = $this->storage->getById($id); // Log that someone’s fetching the secret $this->logger->info('Secret accessed!'); return $secret; } }
  2. Programming intertwined code is hard and complex since all concerns

    have to be dealt with at the same time and at the same level. Source: Separation of Concerns - Hürsch, Lopes - 1995
  3. Programming intertwined code is hard and complex since all concerns

    have to be dealt with at the same time and at the same level. The extended programming language provides no adequate abstraction of concerns at the implementation level. Source: Separation of Concerns - Hürsch, Lopes - 1995
  4. Intertwined code is hard to understand because of the above

    lack of abstraction. Source: Separation of Concerns - Hürsch, Lopes - 1995
  5. Commingled code is hard to maintain and modify because the

    concerns are strongly coupled. Source: Separation of Concerns - Hürsch, Lopes - 1995
  6. The intertwined code gives rise to inheritance anomalies due to

    the strong coupling of different concerns. Source: Separation of Concerns - Hürsch, Lopes - 1995
  7. The intertwined code gives rise to inheritance anomalies due to

    the strong coupling of different concerns. It becomes impossible to redefine a method implementation or the commingled special concern in a subclass without redefining both. Source: Separation of Concerns - Hürsch, Lopes - 1995
  8. class SecretService { public function getSecret($id) { // Check access

    if (!$this->isAllowedToAccess($id)) { throw new AccessDeniedException('You cannot do this!'); } // Get the secret $secret = $this->storage->getById($id); // Log that someone’s fetching the secret $this->logger->info('Secret accessed!'); return $secret; } }
  9. class Awesome { } 89 50 4e 47 0d 0a

    1a fe c3 1b 6c 88 COMPILER
  10. class SecretService { public function getSecret($id) { return $this->storage->getById($id); }

    } class AuthorizationAspect { public function checkAccessBeforeGetSecret($id) { if (!$this->isAllowedToAccess($id)) { throw new AccessDeniedException('You cannot do this!'); } } }
  11. class SecretService { public function getSecret($id) { return $this->storage->getById($id); }

    } class AuthorizationAspect { public function checkAccessBeforeGetSecret($id) { if (!$this->isAllowedToAccess($id)) { throw new AccessDeniedException('You cannot do this!'); } } } class LoggingAspect { public function logAfterGetSecret($id) { $this->logger->info('Secret accessed!'); } }
  12. class SecretService { public function getSecret($id) { // Check access

    if (!$this->isAllowedToAccess($id)) { throw new AccessDeniedException('You cannot do this!'); } // Get the secret $secret = $this->storage->getById($id); // Log that someone’s fetching the secret $this->logger->info('Secret accessed!'); return $secret; } }
  13. class LoggingAspect implements Aspect { /** * @After("execution(public SecretService->getSecret(*))") */

    public function logAfterGetSecret(MethodInvocation $invocation) { $args = $invocation->getArguments(); $this->logger->info('Secret ' . $args[0] . ' accessed!'); } }
  14. class CachingAspect implements Aspect { /** * @Around("execution(public SecretService->getSecret(*))") */

    public function cacheGetSecret(MethodInvocation $invocation) { $args = $invocation->getArguments(); if ($this->cache->has($args[0])) { return $this->cache->get($args[0]); } $secret = $invocation->proceed(); $this->cache->set($args[0], $secret); return $secret; } }