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

Escaping OOP boundaries

Escaping OOP boundaries

Most of developers are constantly teaching and discussing modern patterns, frameworks and libraries. But what if you think that you know almost everything about traditional OOP in PHP? I can bet that at some level of mastery you could notice that traditional object-oriented patterns do not solve all problems but introduce new questions instead. This is because OOP-way is not suited well for complex tasks.

Are you looking for the new food for thoughts about how such complex issues could be solved in PHP? For example how to make your existing method asynchronous just with one single word? Or how to reduce boilerplate code for feature toggles?

Join me and I will show you how to apply the most powerful aspect-oriented framework to escape from your existing OOP boundaries and teach you new patterns that can help you to keep your code clean.

Alexander Lisachenko

November 09, 2019
Tweet

More Decks by Alexander Lisachenko

Other Decks in Programming

Transcript

  1. About me: ‣ Head of Web Development and Architecture at

    Alpari (RU) Forex Broker 2 lisachenko lisachenko
  2. About me: ‣ Head of Web Development and Architecture at

    Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years 2 lisachenko lisachenko
  3. About me: ‣ Head of Web Development and Architecture at

    Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years ‣ Clean code advocate, guru in the Enterprise Architecture 2 lisachenko lisachenko
  4. About me: ‣ Head of Web Development and Architecture at

    Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years ‣ Clean code advocate, guru in the Enterprise Architecture ‣ Author of the aspect-oriented framework Go! AOP 
 http://go.aopphp.com 2 lisachenko lisachenko
  5. Agenda: ‣ Object-Oriented Paradigm boundaries ‣ Escaping from PHP boundaries.

    ‣ Stream wrapper and filters. ‣ Cross-Cutting concerns. 4
  6. Agenda: ‣ Object-Oriented Paradigm boundaries ‣ Escaping from PHP boundaries.

    ‣ Stream wrapper and filters. ‣ Cross-Cutting concerns. ‣ Aspect-Oriented Programming with Go! AOP 4
  7. Agenda: ‣ Object-Oriented Paradigm boundaries ‣ Escaping from PHP boundaries.

    ‣ Stream wrapper and filters. ‣ Cross-Cutting concerns. ‣ Aspect-Oriented Programming with Go! AOP ‣ Hacking the PHP. Native structures and opcodes. 4
  8. 20 OpCache shared immutable data: +File functions (HashTable) +File classes

    (HashTable) +File main OpArray +Interned strings and immutable arrays
  9. 20 OpCache shared immutable data: +File functions (HashTable) +File classes

    (HashTable) +File main OpArray +Interned strings and immutable arrays +Meta-information (system_id, etc)
  10. 22 OpCache OOP restrictions: - Method could not be added/deleted

    or changed in runtime - Class, its parent, implemented interfaces and used traits are declared in compile-time, runtime API is not available
  11. 22 OpCache OOP restrictions: - Method could not be added/deleted

    or changed in runtime - Class, its parent, implemented interfaces and used traits are declared in compile-time, runtime API is not available - Low-level access to the memory is restricted to prevents errors and memory leaks
  12. 22 PS. Possible only with extensions like runkit, uopz, etc

    OpCache OOP restrictions: - Method could not be added/deleted or changed in runtime - Class, its parent, implemented interfaces and used traits are declared in compile-time, runtime API is not available - Low-level access to the memory is restricted to prevents errors and memory leaks
  13. «uopz is a black magic extension of the runkit-and-scary-stuff genre,

    intended to help with QA infrastructure. 27 Joe Watkins
  14. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’)
  15. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’) ‣ Register our own file handler via stream_wrapper_register(‘file’, self::class)
  16. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’) ‣ Register our own file handler via stream_wrapper_register(‘file’, self::class) ‣ Can hook into the PHP source file now!
  17. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’) ‣ Register our own file handler via stream_wrapper_register(‘file’, self::class) ‣ Can hook into the PHP source file now! ‣ Tokenise code with token_get_all($source)
  18. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’) ‣ Register our own file handler via stream_wrapper_register(‘file’, self::class) ‣ Can hook into the PHP source file now! ‣ Tokenise code with token_get_all($source) ‣ Transform this source (eg. remove final keyword)
  19. 30 Idea: ‣ PHP uses internal «file» wrapper for includes

    ‣ We can unregister the «file» handler via stream_wrapper_unregister(‘file’) ‣ Register our own file handler via stream_wrapper_register(‘file’, self::class) ‣ Can hook into the PHP source file now! ‣ Tokenise code with token_get_all($source) ‣ Transform this source (eg. remove final keyword) ‣ Give it back to the PHP to compile
  20. 31 dg/bypass-finals library composer show dg/bypass-finals name : dg/bypass-finals descrip.

    : Removes final keyword from source code on-the- fly and allows mocking of final methods and classes keywords : testing, phpunit, unit, mocking, finals versions : dev-master, v1.1.2, v1.1.1, v1.1.0, v1.0.1, v1.0 type : library
  21. 32 Usage: You need to enable it before the classes

    you want to remove the final are loaded. So call it as soon as possible!
  22. 32 Usage: You need to enable it before the classes

    you want to remove the final are loaded. So call it as soon as possible! PS. For additional details about PhpUnit integration, please read: https://www.tomasvotruba.cz/blog/2019/03/28/how-to-mock-final-classes-in-phpunit/
  23. 33 antecedent/patchwork library composer show antecedent/patchwork name : antecedent/patchwork descrip.

    : Method redefinition (monkey-patching) functionality for PHP. keywords : aop, testing, aspect, runkit, redefinition, monkeypatching, interception versions : dev-master, 2.1.11, 2.1.10… type : library
  24. Joe Armstrong “…You wanted a banana but what you got

    was a gorilla holding the banana and the entire jungle” 44
  25. Joe Armstrong “…You wanted a banana but what you got

    was a gorilla holding the banana and the entire jungle” 44
  26. Example: 45 Implement an audit system that checks an access

    rights for each public method in all classes in our system and then logs this information into the security journal.
  27. Authorization aspect: 57 Pointcut - describes list of interesting events

    Advice - event handler Joinpoint - defines an event object
  28. 61 goaop/framework composer show goaop/framework name : goaop/framework descrip. :

    Framework for aspect-oriented programming in PHP. keywords : php, aop, library, aspect versions : 3.0.x-dev, 2.3.2,… type : library https://github.com/goaop/framework
  29. 65 Lexical analysis and parsing of source code into the

    AST is performed (nikic/PHP-Parser)
  30. 67 The original class is renamed and replaced with a

    new class with additional behavior; stored in the cache
  31. 67 The original class is renamed and replaced with a

    new class with additional behavior; stored in the cache
  32. 67 The original class is renamed and replaced with a

    new class with additional behavior; stored in the cache Same class name!
  33. 67 The original class is renamed and replaced with a

    new class with additional behavior; stored in the cache Original class renamed Same class name!
  34. 67 The original class is renamed and replaced with a

    new class with additional behavior; stored in the cache Original class renamed Overridden method Same class name!
  35. 70 Idea: prevent execution of methods wrapping them into promises

    and run after the fastcgi_finish_request.
  36. 77 ‣ Available from PHP>=7.4 ‣ Allows calling C functions

    ‣ Allows using C data types from pure scripting language FFI - Foreign Function Interface
  37. 77 ‣ Available from PHP>=7.4 ‣ Allows calling C functions

    ‣ Allows using C data types from pure scripting language ‣ Opens a way to write PHP extensions and bindings to C libraries in pure PHP FFI - Foreign Function Interface
  38. 77 ‣ Available from PHP>=7.4 ‣ Allows calling C functions

    ‣ Allows using C data types from pure scripting language ‣ Opens a way to write PHP extensions and bindings to C libraries in pure PHP ‣ Headers can be preloaded during server startup FFI - Foreign Function Interface
  39. 80 lisachenko/z-engine composer show lisachenko/z-engine name : lisachenko/z-engine descrip. :

    Library that provides direct access to native PHP structures. versions : dev-master, 0.5.0,… type : library https://github.com/lisachenko/z-engine
  40. 81 ‣ Build on top of PHP’s Reflection API, eg

    ReflectionClass, ReflectionMethod lisachenko/z-engine
  41. 81 ‣ Build on top of PHP’s Reflection API, eg

    ReflectionClass, ReflectionMethod ‣ Provides low-level binding to PHP’s structures like zval, zend_class_entry, zend_function and much more… lisachenko/z-engine
  42. 81 ‣ Build on top of PHP’s Reflection API, eg

    ReflectionClass, ReflectionMethod ‣ Provides low-level binding to PHP’s structures like zval, zend_class_entry, zend_function and much more… ‣ Manipulates PHP itself in runtime lisachenko/z-engine
  43. 81 ‣ Build on top of PHP’s Reflection API, eg

    ReflectionClass, ReflectionMethod ‣ Provides low-level binding to PHP’s structures like zval, zend_class_entry, zend_function and much more… ‣ Manipulates PHP itself in runtime ‣ Expect memory leaks and segfaults! lisachenko/z-engine
  44. 89 ‣ API for userland PHP extensions ‣ OpCode manipulation

    and transformation ‣ Shared objects (will survive between requests) What to expect:
  45. 89 ‣ API for userland PHP extensions ‣ OpCode manipulation

    and transformation ‣ Shared objects (will survive between requests) ‣ Native hooks for PHP callbacks What to expect:
  46. 89 ‣ API for userland PHP extensions ‣ OpCode manipulation

    and transformation ‣ Shared objects (will survive between requests) ‣ Native hooks for PHP callbacks ‣ Much more :) What to expect: