Slide 1

Slide 1 text

object-oriented programming with WordPress How to approach

Slide 2

Slide 2 text

Carl Alexander

Slide 3

Slide 3 text

@twigpress

Slide 4

Slide 4 text

carlalexander.ca

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Heard of object-oriented programming

Slide 7

Slide 7 text

Tried to use it with WordPress

Slide 8

Slide 8 text

Didn’t feel useful

Slide 9

Slide 9 text

Common problem

Slide 10

Slide 10 text

Hard to design classes that work well with WordPress

Slide 11

Slide 11 text

Why?

Slide 12

Slide 12 text

Going back to the beginning

Slide 13

Slide 13 text

WordPress didn't start off using object-oriented programming

Slide 14

Slide 14 text

PHP didn’t even support it well

Slide 15

Slide 15 text

All procedural programming

Slide 16

Slide 16 text

What is procedural programming?

Slide 17

Slide 17 text

Programming using steps

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

It’s how you break down problems

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

How do you convert this?

Slide 27

Slide 27 text

Move everything in a class

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

This isn't object-oriented programming

Slide 32

Slide 32 text

You put your procedural code inside a class method

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Everyone tells you to structure code that way

Slide 35

Slide 35 text

Perpetuates this cycle of misunderstanding

Slide 36

Slide 36 text

Questions?

Slide 37

Slide 37 text

Understanding object-oriented programming

Slide 38

Slide 38 text

Use classes working together to solve problems

Slide 39

Slide 39 text

No steps like procedural programming

Slide 40

Slide 40 text

Programming using Legos (!!!)

Slide 41

Slide 41 text

Classes: Templates used to create objects

Slide 42

Slide 42 text

Lego brick types

Slide 43

Slide 43 text

Objects: Individual instances of a class

Slide 44

Slide 44 text

Lego bricks

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Already have way of doing things

Slide 47

Slide 47 text

Also not the complete picture

Slide 48

Slide 48 text

Object-oriented programming with WordPress

Slide 49

Slide 49 text

Not just using Legos to build things

Slide 50

Slide 50 text

Designing whole Lego sets

Slide 51

Slide 51 text

You have to visualize the Lego set

Slide 52

Slide 52 text

You have to design the Lego brick types

Slide 53

Slide 53 text

You have to assemble the Lego set

Slide 54

Slide 54 text

A lot more than just building something with Legos

Slide 55

Slide 55 text

Most developers don't have to do this

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

They're the Lego Group

Slide 58

Slide 58 text

They give you the Lego brick types

Slide 59

Slide 59 text

They give you the instructions

Slide 60

Slide 60 text

Original analogy works with frameworks

Slide 61

Slide 61 text

Has important implications

Slide 62

Slide 62 text

Similar footing with developers using a framework

Slide 63

Slide 63 text

Neither of you knows how to think like the Lego Group

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Will make you a much better developer

Slide 66

Slide 66 text

Questions?

Slide 67

Slide 67 text

Thinking like the Lego Group

Slide 68

Slide 68 text

Hard thing to do

Slide 69

Slide 69 text

Strategy to start designing classes better

Slide 70

Slide 70 text

Break the problem down into smaller parts

Slide 71

Slide 71 text

Think about all the Lego brick types you might need

Slide 72

Slide 72 text

What does a WordPress plugin need?

Slide 73

Slide 73 text

class AdminPage

Slide 74

Slide 74 text

class Options

Slide 75

Slide 75 text

class Plugin

Slide 76

Slide 76 text

class Metabox

Slide 77

Slide 77 text

Classes representing common WordPress elements

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

E-commerce plugin?

Slide 80

Slide 80 text

class Product

Slide 81

Slide 81 text

class Sale

Slide 82

Slide 82 text

Learning management system plugin?

Slide 83

Slide 83 text

class Course

Slide 84

Slide 84 text

class Students

Slide 85

Slide 85 text

Break your plugin or theme into classes

Slide 86

Slide 86 text

Some will be specific to WordPress

Slide 87

Slide 87 text

Others to what you're building

Slide 88

Slide 88 text

Add code to them next

Slide 89

Slide 89 text

Properties to store data

Slide 90

Slide 90 text

Methods to define behaviour

Slide 91

Slide 91 text

Where things get complicated

Slide 92

Slide 92 text

No right or wrong way to do it

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

Might need to break things down into more classes

Slide 95

Slide 95 text

Might not know how to solve a specific problem

Slide 96

Slide 96 text

Might have made some wrong assumptions

Slide 97

Slide 97 text

All part of the process

Slide 98

Slide 98 text

Different for everyone

Slide 99

Slide 99 text

Identical outcome at the end

Slide 100

Slide 100 text

Classes are no longer empty

Slide 101

Slide 101 text

Start piecing these different classes together into a Lego set

Slide 102

Slide 102 text

Review Break things down Add code

Slide 103

Slide 103 text

Repeating this cycle is the key

Slide 104

Slide 104 text

Only difference is how fast you do it

Slide 105

Slide 105 text

Questions?

Slide 106

Slide 106 text

Thank you!