$30 off During Our Annual Pro Sale. View Details »

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

Carl Alexander

March 15, 2019
Tweet

More Decks by Carl Alexander

Other Decks in Technology

Transcript

  1. object-oriented programming
    with WordPress
    How to approach

    View Slide

  2. Carl Alexander

    View Slide

  3. @twigpress

    View Slide

  4. carlalexander.ca

    View Slide

  5. View Slide

  6. Heard of object-oriented programming

    View Slide

  7. Tried to use it with WordPress

    View Slide

  8. Didn’t feel useful

    View Slide

  9. Common problem

    View Slide

  10. Hard to design classes that
    work well with WordPress

    View Slide

  11. Why?

    View Slide

  12. Going back to the beginning

    View Slide

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

    View Slide

  14. PHP didn’t even support it well

    View Slide

  15. All procedural programming

    View Slide

  16. What is procedural programming?

    View Slide

  17. Programming using steps

    View Slide

  18. Each line of code is a step towards the
    outcome that you're looking for

    View Slide

  19. It’s how you break down problems

    View Slide

  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;
    }

    View Slide

  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;
    }

    View Slide

  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;
    }

    View Slide

  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;
    }

    View Slide

  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;
    }

    View Slide

  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;
    }

    View Slide

  26. How do you convert this?

    View Slide

  27. Move everything in a class

    View Slide

  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;
    }
    }

    View Slide

  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;
    }
    }

    View Slide

  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;
    }
    }

    View Slide

  31. This isn't object-oriented programming

    View Slide

  32. You put your procedural code
    inside a class method

    View Slide

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

    View Slide

  34. Everyone tells you to
    structure code that way

    View Slide

  35. Perpetuates this cycle of
    misunderstanding

    View Slide

  36. Questions?

    View Slide

  37. Understanding
    object-oriented programming

    View Slide

  38. Use classes working together
    to solve problems

    View Slide

  39. No steps like procedural programming

    View Slide

  40. Programming using Legos (!!!)

    View Slide

  41. Classes:
    Templates used to create objects

    View Slide

  42. Lego brick types

    View Slide

  43. Objects:
    Individual instances of a class

    View Slide

  44. Lego bricks

    View Slide

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

    View Slide

  46. Already have way of doing things

    View Slide

  47. Also not the complete picture

    View Slide

  48. Object-oriented programming
    with WordPress

    View Slide

  49. Not just using Legos to build things

    View Slide

  50. Designing whole Lego sets

    View Slide

  51. You have to
    visualize the Lego set

    View Slide

  52. You have to
    design the Lego brick types

    View Slide

  53. You have to
    assemble the Lego set

    View Slide

  54. A lot more than just
    building something with Legos

    View Slide

  55. Most developers don't have to do this

    View Slide

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

    View Slide

  57. They're the Lego Group

    View Slide

  58. They give you the Lego brick types

    View Slide

  59. They give you the instructions

    View Slide

  60. Original analogy works with frameworks

    View Slide

  61. Has important implications

    View Slide

  62. Similar footing with
    developers using a framework

    View Slide

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

    View Slide

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

    View Slide

  65. Will make you a much better developer

    View Slide

  66. Questions?

    View Slide

  67. Thinking like the Lego Group

    View Slide

  68. Hard thing to do

    View Slide

  69. Strategy to start designing classes better

    View Slide

  70. Break the problem down
    into smaller parts

    View Slide

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

    View Slide

  72. What does a WordPress plugin need?

    View Slide

  73. class AdminPage

    View Slide

  74. class Options

    View Slide

  75. class Plugin

    View Slide

  76. class Metabox

    View Slide

  77. Classes representing
    common WordPress elements

    View Slide

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

    View Slide

  79. E-commerce plugin?

    View Slide

  80. class Product

    View Slide

  81. class Sale

    View Slide

  82. Learning management system plugin?

    View Slide

  83. class Course

    View Slide

  84. class Students

    View Slide

  85. Break your plugin or theme
    into classes

    View Slide

  86. Some will be specific to WordPress

    View Slide

  87. Others to what you're building

    View Slide

  88. Add code to them next

    View Slide

  89. Properties to store data

    View Slide

  90. Methods to define behaviour

    View Slide

  91. Where things get complicated

    View Slide

  92. No right or wrong way to do it

    View Slide

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

    View Slide

  94. Might need to break things down
    into more classes

    View Slide

  95. Might not know how to solve
    a specific problem

    View Slide

  96. Might have made some
    wrong assumptions

    View Slide

  97. All part of the process

    View Slide

  98. Different for everyone

    View Slide

  99. Identical outcome at the end

    View Slide

  100. Classes are no longer empty

    View Slide

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

    View Slide

  102. Review
    Break things down
    Add code

    View Slide

  103. Repeating this cycle is the key

    View Slide

  104. Only difference is how fast you do it

    View Slide

  105. Questions?

    View Slide

  106. Thank you!

    View Slide