How to approach object-oriented programming with WordPress

How to approach object-oriented programming with WordPress

These are the slides from a talk given at WordCamp Miami 2019.

You're a WordPress developer who wants to use object-oriented programming in their next project. You're already familiar with concepts like inheritance. The issue is that you're not sure how to apply those concepts to design classes that feel useful.

This isn't something to feel ashamed about! In fact, it's a common problem when trying to use object-oriented programming with WordPress. It's hard to know how to design classes that work well with WordPress.

Well, you're in luck! This is what we'll go over during this talk.

We'll start by going over the prevalent class design in the WordPress ecosystem. We'll analyze what it's doing and why it leaves you feeling unsatisfied.

Then we'll look at how object-oriented programming expects you to design classes. We'll finish by looking at strategies that you can use to design classes. This should let you finally design classes that feel meaningful to use!

You can read the companion article at: https://carlalexander.ca/approaching-object-oriented-programming-wordpress

5a4758faa5ba6c1322bdfb0f6ebcf56c?s=128

Carl Alexander

March 15, 2019
Tweet

Transcript

  1. object-oriented programming with WordPress How to approach

  2. Carl Alexander

  3. @twigpress

  4. carlalexander.ca

  5. None
  6. Heard of object-oriented programming

  7. Tried to use it with WordPress

  8. Didn’t feel useful

  9. Common problem

  10. Hard to design classes that work well with WordPress

  11. Why?

  12. Going back to the beginning

  13. WordPress didn't start off using object-oriented programming

  14. PHP didn’t even support it well

  15. All procedural programming

  16. What is procedural programming?

  17. Programming using steps

  18. Each line of code is a step towards the outcome

    that you're looking for
  19. It’s how you break down problems

  20. 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; }
  21. 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; }
  22. 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; }
  23. 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; }
  24. 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; }
  25. 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; }
  26. How do you convert this?

  27. Move everything in a class

  28. class Authors { public function get_registered_after($date, $number = 5) {

    $results = array(); $authors = get_users('who=authors'); foreach ($authors->results as $author) { if ($author->user_registered > $date) { $results[] = $author; } if (count($results) >= $number) { break; } } return $results; } }
  29. class Authors { public function get_registered_after($date, $number = 5) {

    $results = array(); $authors = get_users('who=authors'); foreach ($authors->results as $author) { if ($author->user_registered > $date) { $results[] = $author; } if (count($results) >= $number) { break; } } return $results; } }
  30. class Authors { public function get_registered_after($date, $number = 5) {

    $results = array(); $authors = get_users('who=authors'); foreach ($authors->results as $author) { if ($author->user_registered > $date) { $results[] = $author; } if (count($results) >= $number) { break; } } return $results; } }
  31. This isn't object-oriented programming

  32. You put your procedural code inside a class method

  33. That's why using object-oriented programming with WordPress feels bad

  34. Everyone tells you to structure code that way

  35. Perpetuates this cycle of misunderstanding

  36. Questions?

  37. Understanding object-oriented programming

  38. Use classes working together to solve problems

  39. No steps like procedural programming

  40. Programming using Legos (!!!)

  41. Classes: Templates used to create objects

  42. Lego brick types

  43. Objects: Individual instances of a class

  44. Lego bricks

  45. Why isn't using object-oriented programming as easy as using Legos?

  46. Already have way of doing things

  47. Also not the complete picture

  48. Object-oriented programming with WordPress

  49. Not just using Legos to build things

  50. Designing whole Lego sets

  51. You have to visualize the Lego set

  52. You have to design the Lego brick types

  53. You have to assemble the Lego set

  54. A lot more than just building something with Legos

  55. Most developers don't have to do this

  56. Object-oriented frameworks do most of the heavy lifting for you

  57. They're the Lego Group

  58. They give you the Lego brick types

  59. They give you the instructions

  60. Original analogy works with frameworks

  61. Has important implications

  62. Similar footing with developers using a framework

  63. Neither of you knows how to think like the Lego

    Group
  64. You don't have a choice to learn how to think

    that way
  65. Will make you a much better developer

  66. Questions?

  67. Thinking like the Lego Group

  68. Hard thing to do

  69. Strategy to start designing classes better

  70. Break the problem down into smaller parts

  71. Think about all the Lego brick types you might need

  72. What does a WordPress plugin need?

  73. class AdminPage

  74. class Options

  75. class Plugin

  76. class Metabox

  77. Classes representing common WordPress elements

  78. Also have to think about what’s specific to your plugin

  79. E-commerce plugin?

  80. class Product

  81. class Sale

  82. Learning management system plugin?

  83. class Course

  84. class Students

  85. Break your plugin or theme into classes

  86. Some will be specific to WordPress

  87. Others to what you're building

  88. Add code to them next

  89. Properties to store data

  90. Methods to define behaviour

  91. Where things get complicated

  92. No right or wrong way to do it

  93. Don’t expect to get this all right on the first

    try
  94. Might need to break things down into more classes

  95. Might not know how to solve a specific problem

  96. Might have made some wrong assumptions

  97. All part of the process

  98. Different for everyone

  99. Identical outcome at the end

  100. Classes are no longer empty

  101. Start piecing these different classes together into a Lego set

  102. Review Break things down Add code

  103. Repeating this cycle is the key

  104. Only difference is how fast you do it

  105. Questions?

  106. Thank you!