Discover the world of object-oriented programming

Discover the world of object-oriented programming

These are the slides from a workshop given at WordCamp Montréal 2015.

Object-oriented programming is a tricky subject in the WordPress community. Of course, you've heard about it. Everyone says it's awesome!

Meanwhile, you're ready for that next step. You've built a few plugins and maybe a theme or two. You're not new to WordPress anymore.

Yet each time you look into it, it makes no sense! You end up telling yourself object-oriented programming isn’t useful. That it's not worth the trouble to learn.

The goal of this workshop is to help you with that. It’ll walk you through a basic framework to get you started. You’ll leave with a better understanding of what object-oriented programming is. You'll be able to progress at last.

An email course based on this workshop is available at: https://carlalexander.ca/discover-object-oriented-programming/

5a4758faa5ba6c1322bdfb0f6ebcf56c?s=128

Carl Alexander

June 29, 2015
Tweet

Transcript

  1. DISCOVER THE WORLD OF 
 OBJECT-ORIENTED PROGRAMMING

  2. Carl Alexander

  3. @twigpress

  4. carlalexander.ca

  5. None
  6. Let’s talk about…

  7. Let’s talk about… “learning”

  8. We forget how long it takes

  9. We expect to learn things right away

  10. It takes a while

  11. Why can’t you learn it?

  12. You’re not lacking resources either

  13. You have tutorials

  14. You have videos

  15. You have courses

  16. You have workshops

  17. hopefully not this one!

  18. Yet it still doesn’t click

  19. Why is that?

  20. You got caught up in an illusion

  21. They make you think you’re learning

  22. Except learning isn’t an objective

  23. How can you know when you’re done?

  24. You can’t

  25. A good objective isn’t vague

  26. It’s measurable

  27. It’s concrete

  28. It’s attainable

  29. Learning is a process

  30. There’s no tests

  31. There’s no goal post

  32. There’s no switch

  33. It’s gradual and continuous

  34. What can you do to learn?

  35. Work on your own problem

  36. It’s easier

  37. It’s personal

  38. It’s your problem

  39. It’s not theirs

  40. Exercise #1

  41. Find a problem Looking for inspiration? ๏ CodeKata (http://codekata.com/) ๏

    Exercism (http://exercism.io/) (~10 min.)
  42. Debrief

  43. There’s no wrong answer

  44. Learn to find problems

  45. How was the exercise?

  46. Share your answers

  47. So…

  48. now you have a problem

  49. How do you code it?

  50. Can you explain what you do right now?

  51. It isn’t easy to explain

  52. You don’t think about it

  53. You just code

  54. That’s why OOP is hard

  55. It’s another coding style

  56. It’s not just about classes

  57. Let’s look at your coding style

  58. It’s called procedural programming

  59. You code in a series of ordered steps

  60. It’s how you solve a problem

  61. Example

  62. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  63. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  64. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  65. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  66. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  67. function  get_authors_registered_after($date,  $number  =  5)   {      

       $results  =  array();          $authors  =  get_users('who=authors');              foreach  ($authors  as  $author)  {                  if  ($author-­‐>user_registered  >  $date)  {                          $results[]  =  $author;                  }                      if  (count($results)  >=  $number)  {                          break;                  }          }              return  $results;   }
  68. What about this?

  69. class  Authors     {          public

     function  get_registered_after($date,  $number  =  5)          {                  $results  =  array();                  $authors  =  get_users('who=authors');                      foreach  ($query-­‐>results  as  $author)  {                          if  ($author-­‐>user_registered  >  $date)  {                                  $results[]  =  $author;                          }                              if  (count($results)  >=  $number)  {                                  break;                          }                  }                      return  $results;          }   }
  70. Still procedural

  71. Procedural isn’t bad

  72. It just doesn’t scale well

  73. Not all problems break down into steps

  74. How does OOP do it?

  75. Less steps. More classes.

  76. It’s a lot like building with Legos

  77. Classes are bricks types

  78. Each brick has properties

  79. Unique to each brick

  80. A yellow 1x8 brick

  81. A green 1x8 brick

  82. A green 2x2 brick

  83. Each brick is an object

  84. You assemble them into a solution

  85. Exercise #2

  86. Sketch your problem Using the problem from exercise #1: ๏

    Sketch as many classes as possible ๏ Can be as simple as just naming the class ๏ Break things down as much as possible ๏ Done quickly? Add properties! (~15 min.)
  87. Debrief

  88. You’re used to steps

  89. Now it’s with classes

  90. Same goal: Break down the problem

  91. How was the exercise?

  92. Share your answers

  93. Turning sketches into code

  94. Time for OOP basics

  95. Lots of concepts

  96. We’ll focus on one

  97. Encapsulation

  98. Grouping data and behaviour

  99. It’s done through classes

  100. Imagine an oven

  101. You want to see the right temperature

  102. Who cares how it got it

  103. Same idea

  104. Who cares what goes on inside a class

  105. You want a consistent behaviour

  106. You need control to do it

  107. That’s encapsulation (in a nutshell)

  108. Encapsulation in PHP

  109. “class” keyword

  110. Groups properties and methods

  111. class  MyPluginOptions   {   }

  112. Properties

  113. Store class data

  114. class  MyPluginOptions   {          var  $options

     =  array();   }
  115. Methods

  116. Define class behaviour

  117. class  MyPluginOptions   {          var  $options

     =  array();          function  get($name,  $default  =  null)  {                  if  (!$this-­‐>has($name))  {                          return  $default;                  }                  return  $this-­‐>options[$name];          }          function  has($name)  {                  return  isset($this-­‐>options[$name]);          }          function  set($name,  $value)  {                  $this-­‐>options[$name]  =  $value;          }   }
  118. class  MyPluginOptions   {          var  $options

     =  array();          function  get($name,  $default  =  null)  {                  if  (!$this-­‐>has($name))  {                          return  $default;                  }                  return  $this-­‐>options[$name];          }          function  has($name)  {                  return  isset($this-­‐>options[$name]);          }          function  set($name,  $value)  {                  $this-­‐>options[$name]  =  $value;          }   }
  119. class  MyPluginOptions   {          var  $options

     =  array();          function  get($name,  $default  =  null)  {                  if  (!$this-­‐>has($name))  {                          return  $default;                  }                  return  $this-­‐>options[$name];          }          function  has($name)  {                  return  isset($this-­‐>options[$name]);          }          function  set($name,  $value)  {                  $this-­‐>options[$name]  =  $value;          }   }
  120. Visibility

  121. Controls access to methods and properties

  122. class  MyPluginOptions   {          private  $options

     =  array();          public  function  get($name,  $default  =  null)  {                  if  (!$this-­‐>has($name))  {                          return  $default;                  }                  return  $this-­‐>options[$name];          }          public  function  has($name)  {                  return  isset($this-­‐>options[$name]);          }          public  function  set($name,  $value)  {                  $this-­‐>options[$name]  =  $value;          }   }
  123. class  MyPluginOptions   {          private  $options

     =  array();          public  function  get($name,  $default  =  null)  {                  if  (!$this-­‐>has($name))  {                          return  $default;                  }                  return  $this-­‐>options[$name];          }          public  function  has($name)  {                  return  isset($this-­‐>options[$name]);          }          public  function  set($name,  $value)  {                  $this-­‐>options[$name]  =  $value;          }   }
  124. Exercise #3

  125. Code your classes Using your sketches from exercise #2: ๏

    Create classes with properties and methods ๏ Control their access (public or private) ๏ Having an issue? Take note of it and move on ๏ Too easy? Try connecting your classes together (~20 min.)
  126. Debrief

  127. Learn to build prototypes

  128. Work on the problem

  129. It’s ok to stumble

  130. How was the exercise?

  131. Share your answers

  132. Where to go from here

  133. Use your notes to guide your focus

  134. You can't control how fast you can learn a skill

  135. It’s about your intention and attitude

  136. You have to show up and do the work

  137. Let’s review the process

  138. You found a problem

  139. Problems are good objectives

  140. You broke it down into classes

  141. It’s a different way to solve a problem

  142. Your worked on it and built your classes

  143. You ran into issues and took notes

  144. You review what you did well and where you stumbled

  145. You repeat the process

  146. Find a problem Break it down into classes Work on

    the problem Review the bad (and the good)
  147. That’s how you’ll discover object-oriented programming

  148. Thank you! @twigpress