![Page 1: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/1.jpg)
Alexander [email protected]
Getting rid of duplicate code: weaving aspects in PHP with the help of Go! AOP library
![Page 2: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/2.jpg)
Speaker profile
Lisachenko Alexander
• Senior Web Architect at Alpari
• Symfony2 enthusiast: nearly a dozen internal services based on Symfony2, including primary site alpari.ru(CDN, Varnish+ESI, Twig, Assetic, ~60 submodules, ~20 bundles)
![Page 3: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/3.jpg)
• Machine programming;
• Structured programming;
• Procedure programming;
• Module programming;
• Object-oriented programming;
• < new high-level paradigm >
The evolution of programming
![Page 4: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/4.jpg)
Key elements: classes, objects.
Principles: abstraction, encapsulation, inheritance and polymorphism.
Good old OOP…
![Page 5: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/5.jpg)
The principle of single responsibility
![Page 6: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/6.jpg)
The principle of single responsibility
![Page 7: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/7.jpg)
The principle of single responsibility
![Page 8: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/8.jpg)
Authorization...
![Page 9: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/9.jpg)
Logging...
![Page 10: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/10.jpg)
Exception handling...
![Page 11: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/11.jpg)
All because of crosscutting concerns that permeates all of the code, like a skewer.
This code can not be placed in separate classes, and is everywhere:
• caching;
• logging;
• exception handling;
• authorization;
• transactionality.
Why is this so?
![Page 12: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/12.jpg)
The clinical diagnosis of typical application: <censored>-code
• unsuitable for reuse;
• difficult to understand the original purpose of the class, tangled logic, cyclomatic complexity;
• more likely to make a mistake and forget to write "boilerplate" code;
• copying of the code, the violation of DRY.
What do we have in the end?
![Page 13: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/13.jpg)
AOP to the rescue!
![Page 14: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/14.jpg)
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.
![Page 15: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/15.jpg)
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 the European Conference on OOP)
• 2001 – AspectJ AOP framework development
![Page 16: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/16.jpg)
Basic concepts of AOP
• Aspect - the module or class implementing crosscutting concerns. Aspect changes the behavior of the rest of the code, using advice in joinpoints identified by some pointcut.
• Advice - action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice.
![Page 17: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/17.jpg)
Basic concepts of AOP
• Join point — a point during the execution of a program, such as the execution of a method or the handling of an exception.
• Pointcut — set of join point. Pointcut determines if it is suitable join point to the advice given.
• Introduction — changing the structure of the class and / or change the inheritance hierarchy to add aspect functionality in foreign code.
![Page 18: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/18.jpg)
Basic concepts of AOP
![Page 19: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/19.jpg)
Basic advice types
• Before - advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point.
• After - advice to be executed after a join point completes normally.
• Around - advice that surrounds a join point such as a method invocation. Around advice responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
![Page 20: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/20.jpg)
Basic advice types
Before
Before
After
After Throwing
![Page 21: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/21.jpg)
Place for AOP in PHP
AOP complements existing technology into a single entity:• Dependency injection (IoC, DIC)• Abstraction of services (yaml, xml, php)• Aspect-Oriented Programming
![Page 22: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/22.jpg)
Place for AOP in PHP
• Dependency injection• Abstraction of services• Aspect-Oriented Programming
![Page 23: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/23.jpg)
Current implementation of AOP
Promising solutions:
• AOP-PHP
• JMSAopBundle
• TYPO3 Flow AOP component
Cemetery:
• PHPAspect
• Aspect-Oriented PHP
• AspectPHP
![Page 24: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/24.jpg)
Go! library
The basic idea is not new - replace a class with a similar class-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
![Page 25: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/25.jpg)
Go! library
• Does not use PHP-extensions, written entirely in PHP itself;
• Does not require the DI-container for spoofing services with proxy objects;
• Can intercept methods in final classes, final methods, and static methods;
• Can intercept access to public and protected properties;
• Clean code is generated, it is convenient to debug classes and aspects with XDebug
![Page 26: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/26.jpg)
Class source code
![Page 27: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/27.jpg)
Aspect class
![Page 28: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/28.jpg)
The result of running...
![Page 29: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/29.jpg)
Processed code of the class
![Page 30: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/30.jpg)
A good example is the best sermon
![Page 31: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/31.jpg)
A good example is the best sermon
![Page 32: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/32.jpg)
Caching with aspect example
![Page 33: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/33.jpg)
What is expected?
• Pointcut parser (look at FLOW3)
• Introduction — add traits and interfaces to classes
• Caching array of advices in shared-memory — no need to check anything at runtime (hello, serialization of Closure)
• Init joinpoints — replace all «new» expressions with custom joinpoints
• Maximum performance :)
![Page 34: Weaving aspects in PHP with the help of Go! AOP library](https://reader034.vdocuments.us/reader034/viewer/2022052601/5594b11b1a28ab1b648b466c/html5/thumbnails/34.jpg)
Thank you!
Questions?https://github.com/lisachenko/go-aop-php
Our company profile on hh.ru
Link to the library: