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

School of PHP Magic

School of PHP Magic

"The closer you are, the less you see"

How often do you think you know a lot about PHP magic? And what if I tell you that you just did not look carefully? After all, the more you think that you know everything, the easier it is to deceive you. Look carefully, because the closer you are, the less you see.

The school of PHP magic opens its doors. Let's run beyond the established boundaries of OOP rules and make the impossible possible. And who knows, maybe you will love this magic: magic methods, non-standard ways of accessing properties, changing contexts, aspect-oriented programming, streaming filters, and more.

Alexander Lisachenko

May 17, 2019
Tweet

More Decks by Alexander Lisachenko

Other Decks in Programming

Transcript

  1. ‣ Head of Web Development 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
  2. The Trick 6 …an action that is intended to deceive,

    either as a way of cheating someone, or as a joke or form of entertainment
  3. 9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(),

    __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(),
  4. 9 Magic equipment __construct(), __destruct(), __clone(), __call(), __callStatic(), __get(), __set(),

    __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __debugInfo()
  5. 11

  6. 12

  7. 13

  8. 21

  9. 24 Trick #2.2: Magic expressions 1) $factory will be used

    as a class name for «new» 2) Parse error will be thrown
  10. 24 Trick #2.2: Magic expressions 1) $factory will be used

    as a class name for «new» 2) Parse error will be thrown 3) Result of method $factory->build() will be used
  11. 24 Trick #2.2: Magic expressions 1) $factory will be used

    as a class name for «new» 2) Parse error will be thrown 3) Result of method $factory->build() will be used 4) Value of property $factory->build will be used
  12. 29

  13. 29 What is magic? Focused deception. But deception meant to

    entertain. Try to remember next one card…
  14. 29

  15. 30

  16. 30

  17. 33

  18. 33 Who knows how to do this? Who knows two

    solutions? Who knows three solutions?
  19. 33 Who knows how to do this? Who knows two

    solutions? Who knows three solutions? Who knows more than three?
  20. Trick #3: doctrine/instantiator package 38 composer show doctrine/instantiator --all name

    : doctrine/instantiator descrip. : A small, lightweight utility to instantiate objects in PHP without invoking their constructors keywords : constructor, instantiate type : library license : MIT License (MIT)
  21. Trick #4: leedavis/altr-ego package 45 composer show leedavis81/altr-ego --all name

    : leedavis81/altr-ego descrip. : Access an objects protected / private properties and methods keywords : php, break scope versions : dev-master, v1.0.2, v1.0.1, v1.0.0 type : library license : MIT License (MIT)
  22. 52

  23. 52 What if we use previous tricks? Intercept properties. Securely

    store values of properties. Provide read-only API.
  24. 52 What if we use previous tricks? Intercept properties. Securely

    store values of properties. Provide read-only API.
  25. 52 What if we use previous tricks? Intercept properties. Securely

    store values of properties. Provide read-only API.
  26. 52 What if we use previous tricks? Intercept properties. Securely

    store values of properties. Provide read-only API.
  27. 53

  28. 53 Secure place for values: Global variables Public properties Protected

    properties Private properties Private static properties
  29. 53 Secure place for values: Global variables Public properties Protected

    properties Private properties Private static properties Static variables in functions
  30. 53 Secure place for values: Global variables Public properties Protected

    properties Private properties Private static properties Static variables in functions
  31. Trick #5: lisachenko/immutable-object 61 composer show lisachenko/immutable-object --all name :

    lisachenko/immutable-object descrip. : Immutable object library keywords : versions : * dev-master type : library license : MIT License (MIT)
  32. 77

  33. 77

  34. Aspect glossary Pointcut - describes list of interesting events Advice

    - event handler 78 Joinpoint - defines an event object
  35. 83 Lexical analysis and parsing of source code into the

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

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

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

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

    new class with additional behavior; stored in the cache Original class renamed Same class name!
  40. 85 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!
  41. 86 What to expect in future: Opcache preloading for AOP

    core FFI integration to modify binary opcodes
  42. 86 What to expect in future: Opcache preloading for AOP

    core FFI integration to modify binary opcodes Modifying PHP engine internal callbacks
  43. 86 What to expect in future: Opcache preloading for AOP

    core FFI integration to modify binary opcodes Modifying PHP engine internal callbacks Some more magic?
  44. Trick #8: goaop/framework 87 composer show goaop/framework --all name :

    goaop/framework descrip. : Framework for aspect-oriented programming in PHP. keywords : php, aop, library, aspect versions : dev-master, 3.0.x-dev, 2.x-dev, 2.3.1, … type : library license : MIT License
  45. 92 Trick: prevent execution of methods wrapping them into promises

    and run after the fastcgi_finish_request.