Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Stories from the other side

814 views

Published on

Exploring the foundations of OOP and FP in PHP.
This presentation was held at MageTitans 2016 in Manchster.
It is the first time I've given this talk, I expect it to evolve over time.

Published in: Software

Stories from the other side

  1. 1. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  2. 2. Stories from the Other Side MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  3. 3. Exploring OOP and Functional Programming (to become a better programmer) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  4. 4. Why am I interested in this? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  5. 5. Smart guys go FP (lambdalicious, phunkie) The Little Schemer Scala is the rage! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  6. 6. What is this, OOP + FP? Where did these paradigms start? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  7. 7. The term "Object Oriented Programming" was coined by Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  8. 8. Original image by Marcin Wichary - Thanks! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  9. 9. Amongst many other achievements, Alan Kay created Smalltalk MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  10. 10. "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things" It can be done in Smalltalk and in LISP." ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  11. 11. "It can be done in Smalltalk and in LISP." wait... WHAT? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  12. 12. OOP in LISP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  13. 13. ((lambda (f) (define pick (lambda (n lat) (cond ((zero? (sub1 n)) (car lat)) (else (pick (sub1 n) (cdr lat)))))) It's lists and recursion all the way down MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  14. 14. How does OOP work with that? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  15. 15. What is OOP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  16. 16. Is it about inheritance? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  17. 17. "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things" ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  18. 18. Kay doesn't mention inheritance. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  19. 19. Is OOP about classes? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  20. 20. Kay doesn't mention classes either. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  21. 21. If OOP is not about classes or inheritance, what is it about? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  22. 22. Lets start with Messaging MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  23. 23. I guess that means method calls. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  24. 24. In class based OOP, the methods of a class define its Interface. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  25. 25. Interface == Related Methods MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  26. 26. Quite similar to related Functions within a Namespace MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  27. 27. So OOP is about grouping methods together? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  28. 28. "... local retention and protection ..." ~ Alan Kay MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  29. 29. Oh yeah, objects have states. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  30. 30. What is a state? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  31. 31. State in class based OOP: class Foo { private static $class_state = 42; private $instance_state = -0.2; } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  32. 32. State in RL is more like public function __construct(...) { $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames(); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  33. 33. What properties do these examples of state have? public function __construct(...) { $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames(); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  34. 34. A literal $this->type = 'simple'; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  35. 35. A shared object $this->customerSession = $customerSession; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  36. 36. Literal zero (but looks like that might change) $this->counter = 0; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  37. 37. Global state $this->id = $_REQUEST['id']; MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  38. 38. Stuff from the DB $this->names = $db->getNames(); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  39. 39. Can we partition those examples? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  40. 40. 1. State that is always the same 2. State that might change (within the lifespan of the object) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  41. 41. State that might change: → Counter → Session → Filesystem / DB / Internet → Globals MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  42. 42. Non-changing state → The Type ID → Application State → Configuration MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  43. 43. What can we do with this information? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  44. 44. Pure Functions! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  45. 45. Pure functions? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  46. 46. → Don't cause any side effects → Given the same arguments always return the same result MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  47. 47. What are side effects? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  48. 48. → Changing state non-local to the function scope → DB or Filesystem or Network access → Throwing Exceptions → Forking MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  49. 49. No side effects was one part. What was the other? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  50. 50. Same Arguments ==> Same Result MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  51. 51. A function that always returns the same result given the same arguments is called Referentially Transparent MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  52. 52. Referentially Transparent Functions must not use state that might change! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  53. 53. Why should we care? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  54. 54. Benefits of Pure Functions! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  55. 55. → Easier to think about (reasonability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  56. 56. → Easier to break apart (decomposability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  57. 57. → Easier to combine (composability & reuseability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  58. 58. → Easier to show correctness (testability) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  59. 59. → Easier to parallelize (threadability ;)) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  60. 60. Mkay. But what about the changing things? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  61. 61. How can we deal with changing state? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  62. 62. We treat objects as snapshots of the state of the world in one moment. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  63. 63. The state of the world in that moment will never change. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  64. 64. If change occurs, it means now is a different moment in time. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  65. 65. We can represent change in two ways: → by modifying the object → by creating a new instance MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  66. 66. Immutability MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  67. 67. For example, what happens when an admin logs in? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  68. 68. $loggedInAdmin = $loggedOutAdmin->login(); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  69. 69. public function login() { return new self($isLoggedIn = true); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  70. 70. But what if a method needs the "current instance"? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  71. 71. Either that object has to be recreated with the current instance, or the current instance has to be passed as a method argument. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  72. 72. OOP style $authorizationManager->hasAccess($dashboard, $loggedInAdmin); $authorizationManager->hasAccess($dashboard, $loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  73. 73. In FP $has_access($dashboard, $loggedInAdmin); $has_access($dashboard, $loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  74. 74. There are more parameters in RL! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  75. 75. Okay, lets reduce the argument count by adding properties to the object. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  76. 76. $dashboardAuthorizationManager = new class($authorizationManager, $dashboard) { private $authorizationManager; private $page; public function __construct($authorizationManager, $page) { $this->authorizationManager = $authorizationManager; $this->page = $page; } public function hasAccess($admin) { return $this->authorizationManager ->hasAccess($this->page, $admin); } } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  77. 77. Only one argument left: $pageAuthorizationManager->hasAccess($loggedInAdmin); $pageAuthorizationManager->hasAccess($loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  78. 78. How might that look like in FP? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  79. 79. $has_access_to_dashboard = function ($admin) use ($dashboard, $has_access) { return $has_access($dashboard, $admin); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  80. 80. $has_access_to_dashboard($loggedInAdmin); $has_access_to_dashboard($loggedOutAdmin); MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  81. 81. Can we remove all arguments? MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  82. 82. $loggedInAdminDashboardAuthorization = new class($dashboardAuthorizationManager, $loggedInAdmin) { private $pageAuthorizationManager; private $admin; public function __construct($pageAuthorizationManager, $admin) { $this->pageAuthorizationManager = $pageAuthorizationManager; $this->admin = $admin; } public function hasAccess() { return $this->pageAuthorizationManager ->hasAccess($this->admin); } } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  83. 83. In FP: $has_logged_in_admin_access_to_dashboard = function () use ($has_access_to_dashboard, $admin) { return $has_access_to_dashboard($admin); } MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  84. 84. We can bundle state and functions without classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  85. 85. We can bundle state and process without classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  86. 86. Look Ma, OOP in PHP without hands classes! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  87. 87. We have the choice: → pass state with method arguments → set state as object properties MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  88. 88. The more volatile the object state, the more often we need to create new instances... MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  89. 89. ...if we want to have Pure Functions. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  90. 90. Lesson from FP: Distinguish between mutable and immutable object properties. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  91. 91. "Mutable stateful objects are the new spaghetti code" ~ Rich Hickey MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  92. 92. Summary MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  93. 93. Concepts from the FP languages can be useful in OO PHP. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  94. 94. We can benefit from designing our OO code so it exhibits the properties of pure functions. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  95. 95. We barely scratched the surface. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  96. 96. Don't be afraid of FP's mathematical pattern names. It's still just code. MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  97. 97. Building these slides I learned something about OOP. I hope you found it interesting, too! MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp
  98. 98. (if (has-communication? you vinai) (communicate you vinai) (enjoy you remaining-agenda)) MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

×