Why is this so?All because of crosscutting concerns that permeates allof the code, like a skewer.This code can not be placed in separate classes, and iseverywhere:• caching;• logging;• exception handling;• authorization;• transactionality.
What do we have in the end?The clinical diagnosis of typical application: <censored>-code• unsuitable for reuse;• difficult to understand the original purpose of theclass, tangled logic, cyclomatic complexity;• more likely to make a mistake and forget to write"boilerplate" code;• copying of the code, the violation of DRY.
AOP to the rescue! Aspect-Oriented Programming (AOP)• AOP - programming technique in the class paradigm, based on the concept of aspect - a block of code that encapsulates the crosscutting logic in the class.
AOP History•1974 – the principle of division of responsibility•1990е – AOP research • Composition Filters • Subject-Oriented Programming • Adaptive Programming• 1997 - Aspect-Oriented Programming (report on theEuropean Conference on OOP)• 2001 – AspectJ AOP framework development
Basic concepts of AOP• Aspect - the module or class implementingcrosscutting concerns. Aspect changes the behavior ofthe rest of the code, using advice in joinpoints identifiedby some pointcut.• Advice - action taken by an aspect at a particular joinpoint. Different types of advice include "around,""before" and "after" advice.
Basic concepts of AOP• Join point — a point during the execution of aprogram, such as the execution of a method or thehandling of an exception.• Pointcut — set of join point. Pointcut determines if itis suitable join point to the advice given.• Introduction — changing the structure of the classand / or change the inheritance hierarchy to add aspectfunctionality in foreign code.
Basic advice types• Before - advice that executes before a join point, butwhich does not have the ability to prevent executionflow proceeding to the join point.• After - advice to be executed after a join pointcompletes normally.• Around - advice that surrounds a join point such as amethod invocation. Around advice responsible forchoosing whether to proceed to the join point or toshortcut the advised method execution by returning itsown return value or throwing an exception.
Basic advice types Before Before After Throwing After
Place for AOP in PHP AOP complements existing technology into a singleentity:• Dependency injection (IoC, DIC)• Abstraction of services (yaml, xml, php)• Aspect-Oriented Programming
Place for AOP in PHP• Dependency injection• Abstraction of services• Aspect-Oriented Programming
Current implementation of AOP Promising solutions:• AOP-PHP• JMSAopBundle• TYPO3 Flow AOP component Cemetery:• PHPAspect• Aspect-Oriented PHP• AspectPHP
Go! library The basic idea is not new - replace a class with a similarclass-decorator implementation. Key points: • Static analysis of classes before loading them into memory (php-token-reflection, ядро ApiGen) • Change the class hierarchy "on the fly" • Modification of source code for the class at the time of class load, caching
Go! library• Does not use PHP-extensions, written entirely in PHPitself;• Does not require the DI-container for spoofingservices with proxy objects;• Can intercept methods in final classes, final methods,and static methods;• Can intercept access to public and protectedproperties;• Clean code is generated, it is convenient to debugclasses and aspects with XDebug
What is expected?• Pointcut parser (look at FLOW3)• Introduction — add traits and interfaces to classes• Caching array of advices in shared-memory — noneed to check anything at runtime (hello, serialization ofClosure)• Init joinpoints — replace all «new» expressions withcustom joinpoints• Maximum performance :)
Thank you! Questions? https://github.com/lisachenko/go-aop-phpOur company profile Link to the library:on hh.ru