SlideShare a Scribd company logo
Solving Cross-Cutting
Concerns in PHP
Alexander Lisachenko
About me:
2
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
About me:
2
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
‣ Have worked with computers
since 7 years old
About me:
2
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
‣ Have worked with computers
since 7 years old
‣ Clean code advocate, guru in
enterprise architecture
About me:
2
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
‣ Have worked with computers
since 7 years old
‣ Clean code advocate, guru in
enterprise architecture
‣ Author of the aspect-oriented
framework Go! AOP 

http://go.aopphp.com
About me:
2
lisachenko
lisachenko
Agenda
3
Agenda
‣ Advantages of Object-Oriented Paradigm
3
Agenda
‣ Advantages of Object-Oriented Paradigm
‣ Limitations of OOP and how they affect our
application
3
Agenda
‣ Advantages of Object-Oriented Paradigm
‣ Limitations of OOP and how they affect our
application
‣ Existing object-oriented ways of solving
cross-cutting concerns
3
Agenda
‣ Advantages of Object-Oriented Paradigm
‣ Limitations of OOP and how they affect our
application
‣ Existing object-oriented ways of solving
cross-cutting concerns
‣ Pros and cons: the aspect-oriented
approach of solving cross-cutting concerns
3
Agenda
‣ Advantages of Object-Oriented Paradigm
‣ Limitations of OOP and how they affect our
application
‣ Existing object-oriented ways of solving
cross-cutting concerns
‣ Pros and cons: the aspect-oriented
approach of solving cross-cutting concerns
‣ Examples of using aspects in real life
applications
3
Текст
Object-Oriented Paradigm
Solution to the complexity
5
Modularity
We connect classes, components, bundles, and
frameworks.
6
Reusability
We use existing code to create new code on the
top.
7
Encapsulation
We hide implementation details from the outside
world.
8
Is it a silver bullet?
Object-Oriented Paradigm
8
Is it a silver bullet?
Object-Oriented Paradigm
8
Is it a silver bullet?
Object-Oriented Paradigm
Edsger W. Dijkstra
“Object-oriented programming is an
exceptionally bad idea which could
only have originated in California.”
9
Joe Armstrong
“…You wanted a banana but what you got
[with OOP] was a gorilla holding the banana
and the entire jungle”
10
What are the problems?
11
What are the problems?
‣ Limitation of object-oriented
representation of real life processes
11
What are the problems?
‣ Limitation of object-oriented
representation of real life processes
‣ Essential complexity results in strong
coupling between components
11
What are the problems?
‣ Limitation of object-oriented
representation of real life processes
‣ Essential complexity results in strong
coupling between components
‣ Essential complexity results in scattered
implementation of our concerns
11
Текст
Cross-cutting concerns
What they are and why we should think about
them
13
Cross-cutting concerns are
aspects of a program that affect
other concerns.
Cross-cutting concerns - why
they affect our code?
14
Task: Implement an audit system that checks
access permission for each public method over
all classes in our system and then logs this
information into the security journal.
Clean model
15
Authorization control…
16
Authorization control…
16
Logging and audit…
17
Logging and audit…
17
Logging and audit…
17
Error handling…
18
Error handling…
18
Code tangling
19
Code scattering
20
Текст
OOP ways
What do we have in OOP to fight cross-cutting
concerns?
Decorator
Design pattern that allows
behavior to be added to an
individual object, either
statically or dynamically,
without affecting the behavior
of other objects from the
same class.
22
23
23
23
23
Decorator
+ Respects LSP - We can pass an instance of a
decorator everywhere the original object is expected.
+ Keeps SRP for the original class because it doesn’t
contain secondary concerns now
- Too many decorator classes for each combination of
concern+concrete interface
- Secondary concerns (authorization, caching, etc.) are
scattered across all decorators
- Additional overhead for calling original methods
24
Mediator
Defines an object that
incapsulates all logic of
interactions between objects,
giving them the ability to
work without explicit
references to each other
25
26
26
26
27
27
27
Mediator
+ All secondary concerns can be moved to separate
classes without duplication (SRP for secondary
concerns)
+ Keeps SRP for the original class, because it doesn’t
contain secondary concerns now
+ The most flexible way of extending a system
- Requires specific changes in the original class to
perform notification of mediator
- Hard to debug code with complex logic of interaction
because there are no explicit links between objects
28
Текст
Take control of everything
What if we combine all the best features of the
decorator and mediator into the one pattern?
Aspect-Oriented Paradigm
A programming
paradigm that aims to
increase modularity by
allowing the separation
of cross-cutting
concerns
30
Go! AOP Framework
31
Go! AOP Framework
31
‣ Inspired by famous AspectJ and Spring
frameworks
Go! AOP Framework
31
‣ Inspired by famous AspectJ and Spring
frameworks
‣ Over 800 stargazers on Github - thank you!
Go! AOP Framework
31
‣ Inspired by famous AspectJ and Spring
frameworks
‣ Over 800 stargazers on Github - thank you!
‣ Over 200k installations on Packagist
Go! AOP Framework
31
‣ Inspired by famous AspectJ and Spring
frameworks
‣ Over 800 stargazers on Github - thank you!
‣ Over 200k installations on Packagist
‣ PHP5.6, PHP7.0 and Opcache compatible
Go! AOP Framework
31
‣ Inspired by famous AspectJ and Spring
frameworks
‣ Over 800 stargazers on Github - thank you!
‣ Over 200k installations on Packagist
‣ PHP5.6, PHP7.0 and Opcache compatible
‣ There are bridges for SF2, ZF2, Laravel…
Let’s take our clean code…
32
Let’s take our clean code…
32
Let’s take our clean code…
Method execution is an event
32
…and subscribe to our event
33
…and subscribe to our event
33
…and subscribe to our event
Pointcut - describes list of
interesting events
33
…and subscribe to our event
Pointcut - describes list of
interesting events
33
…and subscribe to our event
Pointcut - describes list of
interesting events
Joinpoint - defines an event
object
33
…and subscribe to our event
Pointcut - describes list of
interesting events
Joinpoint - defines an event
object
33
…and subscribe to our event
Pointcut - describes list of
interesting events
Joinpoint - defines an event
object
Advice - event handler
33
How it works
34
All aspects (or advisors) are registered in
the aspect kernel
How it works
35
The special php://filter stream filter is
registered via stream_filter_register()
How it works
36
The composer class loader is replaced
with a weaving proxy
How it works
37
Lexical analysis and parsing of AST is
performed (nikic/PHP-Parser)
How it works
38
Static reflection is created from the AST
(goaop/parser-reflection)
How it works
39
The original class is renamed and replaced with a
new class with additional behavior; stored in the
cache
How it works
39
The original class is renamed and replaced with a
new class with additional behavior; stored in the
cache
How it works
39
The original class is renamed and replaced with a
new class with additional behavior; stored in the
cache
Simple inheritance
How it works
39
The original class is renamed and replaced with a
new class with additional behavior; stored in the
cache
Simple inheritance
How it works
39
The original class is renamed and replaced with a
new class with additional behavior; stored in the
cache
Simple inheritance
Overridden method
Текст
Go! AOP joinpoints
What can be intercepted?
Method interceptors:
41
Method interceptors:
41
Can be a final class or a trait!
Property interceptors:
42
Logic interceptors:
43
AOP
+ Keeps SRP for the original class because it doesn’t
contain secondary concerns now
+ All secondary concerns are stored in separate classes
without duplication (SRP for secondary concerns)
+ Very flexible way of extending our system (like mediator
does)
+ Does not require changes in original classes
- No tools to debug AOP code; no help from IDE
- Overhead for AOP initialization and execution
44
Go! AOP: Debugging with
XDebug
45
Go! AOP: Debugging with
XDebug
45
Go! AOP: Plugin for the
PhpStorm
46
Pointcut syntax highlighting,
completion and analysis
47
Pointcut syntax highlighting,
completion and analysis
47
Navigate to advice/advised
elements
48
Navigate to advice/advised
elements
48
Текст
Circuit breaker
Prevents one failure from reoccurring
continuously
Logic of CircuitBreaker
50
Logic of CircuitBreaker
50
‣ For each public method execution we count
the number of failures in APC/Memcache.
Logic of CircuitBreaker
50
‣ For each public method execution we count
the number of failures in APC/Memcache.
‣ If the number of failures is too high, then we
break the circuit to prevent subsequent failures.
Logic of CircuitBreaker
50
‣ For each public method execution we count
the number of failures in APC/Memcache.
‣ If the number of failures is too high, then we
break the circuit to prevent subsequent failures.
‣ If the circuit is broken, then the original method
body should not be executed for the
configured amount of time.
Logic of CircuitBreaker
50
‣ For each public method execution we count
the number of failures in APC/Memcache.
‣ If the number of failures is too high, then we
break the circuit to prevent subsequent failures.
‣ If the circuit is broken, then the original method
body should not be executed for the
configured amount of time.
‣ https://github.com/ejsmont-artur/php-circuit-
breaker can be used.
Step 1. Define an annotation
51
Step 2. Define an aspect
52
Step 2. Define an aspect
53
Step 2. Define an aspect
53
All methods with «Fuse» annotation
Step 2. Define an aspect
53
All methods with «Fuse» annotation
Step 2. Define an aspect
53
All methods with «Fuse» annotation
Step 2. Define an aspect
53
All methods with «Fuse» annotation
Step 3. Use it in your code
54
Step 3. Use it in your code
54
More examples for AOP
55
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
‣ Feature toggle pattern
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
‣ Feature toggle pattern
‣ Authorization control
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
‣ Feature toggle pattern
‣ Authorization control
‣ Traditional caching, logging, transaction
control, security audit
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
‣ Feature toggle pattern
‣ Authorization control
‣ Traditional caching, logging, transaction
control, security audit
‣ Design by contract programming (Php-Deal
library)
More examples for AOP
55
‣ Profiling methods with pinba extension and
Intaro Pinboard
‣ Feature toggle pattern
‣ Authorization control
‣ Traditional caching, logging, transaction
control, security audit
‣ Design by contract programming (Php-Deal
library)
‣ Aspect-oriented testing (AspectMock)
Conclusion
56
Conclusion
56
‣ OOP is a nice tool for solving accidental
complexity but not for essential complexity.
Conclusion
56
‣ OOP is a nice tool for solving accidental
complexity but not for essential complexity.
‣ Pay attention to code scattering and code
tangling.
Conclusion
56
‣ OOP is a nice tool for solving accidental
complexity but not for essential complexity.
‣ Pay attention to code scattering and code
tangling.
‣ Try to extract secondary concerns into
separate classes via OOP decomposition for
small applications.
Conclusion
56
‣ OOP is a nice tool for solving accidental
complexity but not for essential complexity.
‣ Pay attention to code scattering and code
tangling.
‣ Try to extract secondary concerns into
separate classes via OOP decomposition for
small applications.
‣ Try to use AOP for complex enterprise
applications.
Thank you for the attention!
https://github.com/goaop
https://github.com/lisachenko
https://twitter.com/lisachenko

More Related Content

What's hot (20)

PDF
Making ES6 available to all with ChakraCore and Typescript
Christian Heilmann
 
PDF
Monitoring 改造計畫:流程觀點
William Yeh
 
PDF
So You Just Inherited a $Legacy Application...
Joe Ferguson
 
PDF
Maven - Taming the Beast
Roberto Cortez
 
PDF
Client-side Development 2016
Huge
 
PDF
Why every startup built with Ruby on Rails has an upper hand over their compe...
DreamToIPO
 
PDF
Jr devsurvivalguide
James York
 
PDF
Java EE 7 meets Java 8
Roberto Cortez
 
PDF
VCS for Teamwork - GIT Workshop
Anis Ahmad
 
PDF
Rubyslava debugging with_pry
olahmichal
 
PDF
Becoming a Git Master - Nicola Paolucci
Atlassian
 
PDF
DevOpsDaysRiga 2018: Neil Crawford - Trunk based development, continuous depl...
DevOpsDays Riga
 
PDF
php-1701-a
Adam Culp
 
PPTX
Becoming fully buzzword compliant
Trisha Gee
 
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
PPTX
Using software modules welcome to hell!
Baruch Sadogursky
 
PDF
Can you TDD Rails?
Andrzej Krzywda
 
PDF
Atlassian - Software For Every Team
Sven Peters
 
PDF
Old and new perils of open source - Great Wide Open keynote
Christian Heilmann
 
PPTX
Practical DevOps & Continuous Delivery – A Webinar to learn in depth on DevO...
Hugo Messer
 
Making ES6 available to all with ChakraCore and Typescript
Christian Heilmann
 
Monitoring 改造計畫:流程觀點
William Yeh
 
So You Just Inherited a $Legacy Application...
Joe Ferguson
 
Maven - Taming the Beast
Roberto Cortez
 
Client-side Development 2016
Huge
 
Why every startup built with Ruby on Rails has an upper hand over their compe...
DreamToIPO
 
Jr devsurvivalguide
James York
 
Java EE 7 meets Java 8
Roberto Cortez
 
VCS for Teamwork - GIT Workshop
Anis Ahmad
 
Rubyslava debugging with_pry
olahmichal
 
Becoming a Git Master - Nicola Paolucci
Atlassian
 
DevOpsDaysRiga 2018: Neil Crawford - Trunk based development, continuous depl...
DevOpsDays Riga
 
php-1701-a
Adam Culp
 
Becoming fully buzzword compliant
Trisha Gee
 
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
Using software modules welcome to hell!
Baruch Sadogursky
 
Can you TDD Rails?
Andrzej Krzywda
 
Atlassian - Software For Every Team
Sven Peters
 
Old and new perils of open source - Great Wide Open keynote
Christian Heilmann
 
Practical DevOps & Continuous Delivery – A Webinar to learn in depth on DevO...
Hugo Messer
 

Viewers also liked (15)

ODP
Weaving aspects in PHP with the help of Go! AOP library
Alexander Lisachenko
 
PPTX
Building Big Architectures
Ramit Surana
 
PDF
Lean publishing "Principles of Package Design"
Matthias Noback
 
PDF
PHP Experience 2016 - ROA – Resource Oriented Architecture
iMasters
 
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
PPTX
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
PDF
Simple Web Services with PHP
John Paul Ada
 
PPTX
WebCamp 2016: PHP. Николай Паламарчук: PHP и микросервисы
WebCamp
 
PPTX
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
Alexandre Brandão Lustosa
 
PPTX
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
Xuefeng Zhang
 
PDF
Microservices: Where do they fit within a rapidly evolving integration archit...
Kim Clark
 
DOC
CONCEPTUAL FRAMEWORK
lendiibanez22
 
PDF
Integrating React.js Into a PHP Application
Andrew Rota
 
PDF
Process Oriented Architecture
Alan McSweeney
 
PPT
Chapter 6-THEORETICAL & CONCEPTUAL FRAMEWORK
Ludy Mae Nalzaro,BSM,BSN,MN
 
Weaving aspects in PHP with the help of Go! AOP library
Alexander Lisachenko
 
Building Big Architectures
Ramit Surana
 
Lean publishing "Principles of Package Design"
Matthias Noback
 
PHP Experience 2016 - ROA – Resource Oriented Architecture
iMasters
 
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Simple Web Services with PHP
John Paul Ada
 
WebCamp 2016: PHP. Николай Паламарчук: PHP и микросервисы
WebCamp
 
PHP with Service BUS (RabbitMQ/Redis/MongoDB) - IMasters PHP Experience 2016
Alexandre Brandão Lustosa
 
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
Xuefeng Zhang
 
Microservices: Where do they fit within a rapidly evolving integration archit...
Kim Clark
 
CONCEPTUAL FRAMEWORK
lendiibanez22
 
Integrating React.js Into a PHP Application
Andrew Rota
 
Process Oriented Architecture
Alan McSweeney
 
Chapter 6-THEORETICAL & CONCEPTUAL FRAMEWORK
Ludy Mae Nalzaro,BSM,BSN,MN
 
Ad

Similar to Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016 (20)

PDF
Solving cross cutting concerns in PHP - PHPSerbia-2017
Alexander Lisachenko
 
PPTX
Spring AOP in Nutshell
Onkar Deshpande
 
PPTX
Aspect-oriented programming
MohamadHayeri1
 
PPTX
Performance analysis of synchronisation problem
harshit200793
 
PPTX
Aspect Oriented Programming
Shreya Chatterjee
 
PDF
WoMakersCode 2016 - Shit Happens
Jackson F. de A. Mafra
 
PPT
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
Thuy_Dang
 
PPTX
Aspect Oriented Programming
Rodger Oates
 
PPTX
Introduction to Aspect Oriented Programming
Yan Cui
 
PPTX
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Yan Cui
 
PPTX
Spring AOP
Radhakrishna Mutthoju
 
PPT
Aop2007
Tuhin_Das
 
PPT
Aspect Oriented Programming
Anumod Kumar
 
PDF
Aspect-Oriented Programming and Depedency Injection
Robert Lemke
 
PPT
Aspect Oriented Software Development
Jignesh Patel
 
ODP
Aspect-Oriented Programming for PHP
William Candillon
 
PPTX
Intro To AOP
Donald Belcham
 
PDF
Crosscutting Specification Interference Detection at Aspect Oriented UML-Base...
IJERA Editor
 
PDF
Spring aop
Hamid Ghorbani
 
PPTX
Aspect Oriented Programming
Rajesh Ganesan
 
Solving cross cutting concerns in PHP - PHPSerbia-2017
Alexander Lisachenko
 
Spring AOP in Nutshell
Onkar Deshpande
 
Aspect-oriented programming
MohamadHayeri1
 
Performance analysis of synchronisation problem
harshit200793
 
Aspect Oriented Programming
Shreya Chatterjee
 
WoMakersCode 2016 - Shit Happens
Jackson F. de A. Mafra
 
AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team
Thuy_Dang
 
Aspect Oriented Programming
Rodger Oates
 
Introduction to Aspect Oriented Programming
Yan Cui
 
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Yan Cui
 
Aop2007
Tuhin_Das
 
Aspect Oriented Programming
Anumod Kumar
 
Aspect-Oriented Programming and Depedency Injection
Robert Lemke
 
Aspect Oriented Software Development
Jignesh Patel
 
Aspect-Oriented Programming for PHP
William Candillon
 
Intro To AOP
Donald Belcham
 
Crosscutting Specification Interference Detection at Aspect Oriented UML-Base...
IJERA Editor
 
Spring aop
Hamid Ghorbani
 
Aspect Oriented Programming
Rajesh Ganesan
 
Ad

Recently uploaded (20)

PPTX
Sample pitch deck: know what to keep in your pitch deck (for competitions only)
Ujjwaal G
 
PPTX
Correlation Research Grade 12 Presentation
angelieofolit
 
PDF
QYResearch Agricultural Machinery Market Overview, Top 30 Players Ranking, ke...
YangJunping
 
PPTX
Call-Of-Duty PowerPoint Template for Presentation
olivastephaniefrance
 
PPTX
2025-07-20 Abraham 08 (shared slides).pptx
Dale Wells
 
PPTX
India Energy and Utility Sector Landscape
Anand Akshay
 
PPTX
Applied Stats for Real-Life Decisions.pptx
khalyaniramjan49
 
PPTX
Diversity in tech, the missing link to great software.
Ramona Domen
 
PPTX
A Mother's Love - Helen Steiner Rice.pptx
AlbertoTierra
 
PPTX
Renters' Rights and PBSA. How the bill will impact on the sector
Nick Emms
 
PPTX
English_Book_2 part 2 let reviewers news
2022mimiacadserver
 
PDF
481771880-Fundamentals-of-Public-Speaking.pdf
crisjseit1211
 
PPTX
Presentation for a short film .pptx.pptx
madisoncosta17
 
PPTX
The Brain Behind Your Device: A Deep Dive into Operating Systems
vanshshah1920
 
PDF
Buy Old GitHub Accounts -Trusted Sellers
GitHub Account
 
PPTX
English_Book_1 part 1 LET Reviewers NEw-
2022mimiacadserver
 
PPTX
The Waiting Time Sermon Delivered at SDA CHURCH.pptx
HumphreyAgala
 
PPTX
Patient with Upper & Lower GI bleeding .pptx
bdmlwb701
 
PPTX
WATCHMAN, WATCH! - SERMON at SDA CHURCH.pptx
HumphreyAgala
 
PPTX
Creative perspective presentation copy.pptx
dreamsteel
 
Sample pitch deck: know what to keep in your pitch deck (for competitions only)
Ujjwaal G
 
Correlation Research Grade 12 Presentation
angelieofolit
 
QYResearch Agricultural Machinery Market Overview, Top 30 Players Ranking, ke...
YangJunping
 
Call-Of-Duty PowerPoint Template for Presentation
olivastephaniefrance
 
2025-07-20 Abraham 08 (shared slides).pptx
Dale Wells
 
India Energy and Utility Sector Landscape
Anand Akshay
 
Applied Stats for Real-Life Decisions.pptx
khalyaniramjan49
 
Diversity in tech, the missing link to great software.
Ramona Domen
 
A Mother's Love - Helen Steiner Rice.pptx
AlbertoTierra
 
Renters' Rights and PBSA. How the bill will impact on the sector
Nick Emms
 
English_Book_2 part 2 let reviewers news
2022mimiacadserver
 
481771880-Fundamentals-of-Public-Speaking.pdf
crisjseit1211
 
Presentation for a short film .pptx.pptx
madisoncosta17
 
The Brain Behind Your Device: A Deep Dive into Operating Systems
vanshshah1920
 
Buy Old GitHub Accounts -Trusted Sellers
GitHub Account
 
English_Book_1 part 1 LET Reviewers NEw-
2022mimiacadserver
 
The Waiting Time Sermon Delivered at SDA CHURCH.pptx
HumphreyAgala
 
Patient with Upper & Lower GI bleeding .pptx
bdmlwb701
 
WATCHMAN, WATCH! - SERMON at SDA CHURCH.pptx
HumphreyAgala
 
Creative perspective presentation copy.pptx
dreamsteel
 

Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016

  • 1. Solving Cross-Cutting Concerns in PHP Alexander Lisachenko
  • 3. ‣ Head of Software Architecture at Alpari (RU) Forex Broker About me: 2 lisachenko lisachenko
  • 4. ‣ Head of Software Architecture at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old About me: 2 lisachenko lisachenko
  • 5. ‣ Head of Software Architecture at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old ‣ Clean code advocate, guru in enterprise architecture About me: 2 lisachenko lisachenko
  • 6. ‣ Head of Software Architecture at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old ‣ Clean code advocate, guru in enterprise architecture ‣ Author of the aspect-oriented framework Go! AOP 
 http://go.aopphp.com About me: 2 lisachenko lisachenko
  • 8. Agenda ‣ Advantages of Object-Oriented Paradigm 3
  • 9. Agenda ‣ Advantages of Object-Oriented Paradigm ‣ Limitations of OOP and how they affect our application 3
  • 10. Agenda ‣ Advantages of Object-Oriented Paradigm ‣ Limitations of OOP and how they affect our application ‣ Existing object-oriented ways of solving cross-cutting concerns 3
  • 11. Agenda ‣ Advantages of Object-Oriented Paradigm ‣ Limitations of OOP and how they affect our application ‣ Existing object-oriented ways of solving cross-cutting concerns ‣ Pros and cons: the aspect-oriented approach of solving cross-cutting concerns 3
  • 12. Agenda ‣ Advantages of Object-Oriented Paradigm ‣ Limitations of OOP and how they affect our application ‣ Existing object-oriented ways of solving cross-cutting concerns ‣ Pros and cons: the aspect-oriented approach of solving cross-cutting concerns ‣ Examples of using aspects in real life applications 3
  • 14. 5 Modularity We connect classes, components, bundles, and frameworks.
  • 15. 6 Reusability We use existing code to create new code on the top.
  • 16. 7 Encapsulation We hide implementation details from the outside world.
  • 17. 8 Is it a silver bullet? Object-Oriented Paradigm
  • 18. 8 Is it a silver bullet? Object-Oriented Paradigm
  • 19. 8 Is it a silver bullet? Object-Oriented Paradigm
  • 20. Edsger W. Dijkstra “Object-oriented programming is an exceptionally bad idea which could only have originated in California.” 9
  • 21. Joe Armstrong “…You wanted a banana but what you got [with OOP] was a gorilla holding the banana and the entire jungle” 10
  • 22. What are the problems? 11
  • 23. What are the problems? ‣ Limitation of object-oriented representation of real life processes 11
  • 24. What are the problems? ‣ Limitation of object-oriented representation of real life processes ‣ Essential complexity results in strong coupling between components 11
  • 25. What are the problems? ‣ Limitation of object-oriented representation of real life processes ‣ Essential complexity results in strong coupling between components ‣ Essential complexity results in scattered implementation of our concerns 11
  • 26. Текст Cross-cutting concerns What they are and why we should think about them
  • 27. 13 Cross-cutting concerns are aspects of a program that affect other concerns.
  • 28. Cross-cutting concerns - why they affect our code? 14 Task: Implement an audit system that checks access permission for each public method over all classes in our system and then logs this information into the security journal.
  • 39. Текст OOP ways What do we have in OOP to fight cross-cutting concerns?
  • 40. Decorator Design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. 22
  • 41. 23
  • 42. 23
  • 43. 23
  • 44. 23
  • 45. Decorator + Respects LSP - We can pass an instance of a decorator everywhere the original object is expected. + Keeps SRP for the original class because it doesn’t contain secondary concerns now - Too many decorator classes for each combination of concern+concrete interface - Secondary concerns (authorization, caching, etc.) are scattered across all decorators - Additional overhead for calling original methods 24
  • 46. Mediator Defines an object that incapsulates all logic of interactions between objects, giving them the ability to work without explicit references to each other 25
  • 47. 26
  • 48. 26
  • 49. 26
  • 50. 27
  • 51. 27
  • 52. 27
  • 53. Mediator + All secondary concerns can be moved to separate classes without duplication (SRP for secondary concerns) + Keeps SRP for the original class, because it doesn’t contain secondary concerns now + The most flexible way of extending a system - Requires specific changes in the original class to perform notification of mediator - Hard to debug code with complex logic of interaction because there are no explicit links between objects 28
  • 54. Текст Take control of everything What if we combine all the best features of the decorator and mediator into the one pattern?
  • 55. Aspect-Oriented Paradigm A programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns 30
  • 57. Go! AOP Framework 31 ‣ Inspired by famous AspectJ and Spring frameworks
  • 58. Go! AOP Framework 31 ‣ Inspired by famous AspectJ and Spring frameworks ‣ Over 800 stargazers on Github - thank you!
  • 59. Go! AOP Framework 31 ‣ Inspired by famous AspectJ and Spring frameworks ‣ Over 800 stargazers on Github - thank you! ‣ Over 200k installations on Packagist
  • 60. Go! AOP Framework 31 ‣ Inspired by famous AspectJ and Spring frameworks ‣ Over 800 stargazers on Github - thank you! ‣ Over 200k installations on Packagist ‣ PHP5.6, PHP7.0 and Opcache compatible
  • 61. Go! AOP Framework 31 ‣ Inspired by famous AspectJ and Spring frameworks ‣ Over 800 stargazers on Github - thank you! ‣ Over 200k installations on Packagist ‣ PHP5.6, PHP7.0 and Opcache compatible ‣ There are bridges for SF2, ZF2, Laravel…
  • 62. Let’s take our clean code… 32
  • 63. Let’s take our clean code… 32
  • 64. Let’s take our clean code… Method execution is an event 32
  • 65. …and subscribe to our event 33
  • 66. …and subscribe to our event 33
  • 67. …and subscribe to our event Pointcut - describes list of interesting events 33
  • 68. …and subscribe to our event Pointcut - describes list of interesting events 33
  • 69. …and subscribe to our event Pointcut - describes list of interesting events Joinpoint - defines an event object 33
  • 70. …and subscribe to our event Pointcut - describes list of interesting events Joinpoint - defines an event object 33
  • 71. …and subscribe to our event Pointcut - describes list of interesting events Joinpoint - defines an event object Advice - event handler 33
  • 72. How it works 34 All aspects (or advisors) are registered in the aspect kernel
  • 73. How it works 35 The special php://filter stream filter is registered via stream_filter_register()
  • 74. How it works 36 The composer class loader is replaced with a weaving proxy
  • 75. How it works 37 Lexical analysis and parsing of AST is performed (nikic/PHP-Parser)
  • 76. How it works 38 Static reflection is created from the AST (goaop/parser-reflection)
  • 77. How it works 39 The original class is renamed and replaced with a new class with additional behavior; stored in the cache
  • 78. How it works 39 The original class is renamed and replaced with a new class with additional behavior; stored in the cache
  • 79. How it works 39 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Simple inheritance
  • 80. How it works 39 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Simple inheritance
  • 81. How it works 39 The original class is renamed and replaced with a new class with additional behavior; stored in the cache Simple inheritance Overridden method
  • 82. Текст Go! AOP joinpoints What can be intercepted?
  • 84. Method interceptors: 41 Can be a final class or a trait!
  • 87. AOP + Keeps SRP for the original class because it doesn’t contain secondary concerns now + All secondary concerns are stored in separate classes without duplication (SRP for secondary concerns) + Very flexible way of extending our system (like mediator does) + Does not require changes in original classes - No tools to debug AOP code; no help from IDE - Overhead for AOP initialization and execution 44
  • 88. Go! AOP: Debugging with XDebug 45
  • 89. Go! AOP: Debugging with XDebug 45
  • 90. Go! AOP: Plugin for the PhpStorm 46
  • 95. Текст Circuit breaker Prevents one failure from reoccurring continuously
  • 97. Logic of CircuitBreaker 50 ‣ For each public method execution we count the number of failures in APC/Memcache.
  • 98. Logic of CircuitBreaker 50 ‣ For each public method execution we count the number of failures in APC/Memcache. ‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures.
  • 99. Logic of CircuitBreaker 50 ‣ For each public method execution we count the number of failures in APC/Memcache. ‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures. ‣ If the circuit is broken, then the original method body should not be executed for the configured amount of time.
  • 100. Logic of CircuitBreaker 50 ‣ For each public method execution we count the number of failures in APC/Memcache. ‣ If the number of failures is too high, then we break the circuit to prevent subsequent failures. ‣ If the circuit is broken, then the original method body should not be executed for the configured amount of time. ‣ https://github.com/ejsmont-artur/php-circuit- breaker can be used.
  • 101. Step 1. Define an annotation 51
  • 102. Step 2. Define an aspect 52
  • 103. Step 2. Define an aspect 53
  • 104. Step 2. Define an aspect 53 All methods with «Fuse» annotation
  • 105. Step 2. Define an aspect 53 All methods with «Fuse» annotation
  • 106. Step 2. Define an aspect 53 All methods with «Fuse» annotation
  • 107. Step 2. Define an aspect 53 All methods with «Fuse» annotation
  • 108. Step 3. Use it in your code 54
  • 109. Step 3. Use it in your code 54
  • 110. More examples for AOP 55
  • 111. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard
  • 112. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard ‣ Feature toggle pattern
  • 113. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard ‣ Feature toggle pattern ‣ Authorization control
  • 114. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard ‣ Feature toggle pattern ‣ Authorization control ‣ Traditional caching, logging, transaction control, security audit
  • 115. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard ‣ Feature toggle pattern ‣ Authorization control ‣ Traditional caching, logging, transaction control, security audit ‣ Design by contract programming (Php-Deal library)
  • 116. More examples for AOP 55 ‣ Profiling methods with pinba extension and Intaro Pinboard ‣ Feature toggle pattern ‣ Authorization control ‣ Traditional caching, logging, transaction control, security audit ‣ Design by contract programming (Php-Deal library) ‣ Aspect-oriented testing (AspectMock)
  • 118. Conclusion 56 ‣ OOP is a nice tool for solving accidental complexity but not for essential complexity.
  • 119. Conclusion 56 ‣ OOP is a nice tool for solving accidental complexity but not for essential complexity. ‣ Pay attention to code scattering and code tangling.
  • 120. Conclusion 56 ‣ OOP is a nice tool for solving accidental complexity but not for essential complexity. ‣ Pay attention to code scattering and code tangling. ‣ Try to extract secondary concerns into separate classes via OOP decomposition for small applications.
  • 121. Conclusion 56 ‣ OOP is a nice tool for solving accidental complexity but not for essential complexity. ‣ Pay attention to code scattering and code tangling. ‣ Try to extract secondary concerns into separate classes via OOP decomposition for small applications. ‣ Try to use AOP for complex enterprise applications.
  • 122. Thank you for the attention! https://github.com/goaop https://github.com/lisachenko https://twitter.com/lisachenko