Slide 1

Slide 1 text

DISCOVER THE WORLD OF 
 OBJECT-ORIENTED PROGRAMMING

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

Let’s talk about…

Slide 7

Slide 7 text

Let’s talk about… “learning”

Slide 8

Slide 8 text

We forget how long it takes

Slide 9

Slide 9 text

We expect to learn things right away

Slide 10

Slide 10 text

It takes a while

Slide 11

Slide 11 text

Why can’t you learn it?

Slide 12

Slide 12 text

You’re not lacking resources either

Slide 13

Slide 13 text

You have tutorials

Slide 14

Slide 14 text

You have videos

Slide 15

Slide 15 text

You have courses

Slide 16

Slide 16 text

You have workshops

Slide 17

Slide 17 text

hopefully not this one!

Slide 18

Slide 18 text

Yet it still doesn’t click

Slide 19

Slide 19 text

Why is that?

Slide 20

Slide 20 text

You got caught up in an illusion

Slide 21

Slide 21 text

They make you think you’re learning

Slide 22

Slide 22 text

Except learning isn’t an objective

Slide 23

Slide 23 text

How can you know when you’re done?

Slide 24

Slide 24 text

You can’t

Slide 25

Slide 25 text

A good objective isn’t vague

Slide 26

Slide 26 text

It’s measurable

Slide 27

Slide 27 text

It’s concrete

Slide 28

Slide 28 text

It’s attainable

Slide 29

Slide 29 text

Learning is a process

Slide 30

Slide 30 text

There’s no tests

Slide 31

Slide 31 text

There’s no goal post

Slide 32

Slide 32 text

There’s no switch

Slide 33

Slide 33 text

It’s gradual and continuous

Slide 34

Slide 34 text

What can you do to learn?

Slide 35

Slide 35 text

Work on your own problem

Slide 36

Slide 36 text

It’s easier

Slide 37

Slide 37 text

It’s personal

Slide 38

Slide 38 text

It’s your problem

Slide 39

Slide 39 text

It’s not theirs

Slide 40

Slide 40 text

Exercise #1

Slide 41

Slide 41 text

Find a problem Looking for inspiration? ๏ CodeKata (http://codekata.com/) ๏ Exercism (http://exercism.io/) (~10 min.)

Slide 42

Slide 42 text

Debrief

Slide 43

Slide 43 text

There’s no wrong answer

Slide 44

Slide 44 text

Learn to find problems

Slide 45

Slide 45 text

How was the exercise?

Slide 46

Slide 46 text

Share your answers

Slide 47

Slide 47 text

So…

Slide 48

Slide 48 text

now you have a problem

Slide 49

Slide 49 text

How do you code it?

Slide 50

Slide 50 text

Can you explain what you do right now?

Slide 51

Slide 51 text

It isn’t easy to explain

Slide 52

Slide 52 text

You don’t think about it

Slide 53

Slide 53 text

You just code

Slide 54

Slide 54 text

That’s why OOP is hard

Slide 55

Slide 55 text

It’s another coding style

Slide 56

Slide 56 text

It’s not just about classes

Slide 57

Slide 57 text

Let’s look at your coding style

Slide 58

Slide 58 text

It’s called procedural programming

Slide 59

Slide 59 text

You code in a series of ordered steps

Slide 60

Slide 60 text

It’s how you solve a problem

Slide 61

Slide 61 text

Example

Slide 62

Slide 62 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 63

Slide 63 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 64

Slide 64 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 65

Slide 65 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 66

Slide 66 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 67

Slide 67 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 68

Slide 68 text

What about this?

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Still procedural

Slide 71

Slide 71 text

Procedural isn’t bad

Slide 72

Slide 72 text

It just doesn’t scale well

Slide 73

Slide 73 text

Not all problems break down into steps

Slide 74

Slide 74 text

How does OOP do it?

Slide 75

Slide 75 text

Less steps. More classes.

Slide 76

Slide 76 text

It’s a lot like building with Legos

Slide 77

Slide 77 text

Classes are bricks types

Slide 78

Slide 78 text

Each brick has properties

Slide 79

Slide 79 text

Unique to each brick

Slide 80

Slide 80 text

A yellow 1x8 brick

Slide 81

Slide 81 text

A green 1x8 brick

Slide 82

Slide 82 text

A green 2x2 brick

Slide 83

Slide 83 text

Each brick is an object

Slide 84

Slide 84 text

You assemble them into a solution

Slide 85

Slide 85 text

Exercise #2

Slide 86

Slide 86 text

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.)

Slide 87

Slide 87 text

Debrief

Slide 88

Slide 88 text

You’re used to steps

Slide 89

Slide 89 text

Now it’s with classes

Slide 90

Slide 90 text

Same goal: Break down the problem

Slide 91

Slide 91 text

How was the exercise?

Slide 92

Slide 92 text

Share your answers

Slide 93

Slide 93 text

Turning sketches into code

Slide 94

Slide 94 text

Time for OOP basics

Slide 95

Slide 95 text

Lots of concepts

Slide 96

Slide 96 text

We’ll focus on one

Slide 97

Slide 97 text

Encapsulation

Slide 98

Slide 98 text

Grouping data and behaviour

Slide 99

Slide 99 text

It’s done through classes

Slide 100

Slide 100 text

Imagine an oven

Slide 101

Slide 101 text

You want to see the right temperature

Slide 102

Slide 102 text

Who cares how it got it

Slide 103

Slide 103 text

Same idea

Slide 104

Slide 104 text

Who cares what goes on inside a class

Slide 105

Slide 105 text

You want a consistent behaviour

Slide 106

Slide 106 text

You need control to do it

Slide 107

Slide 107 text

That’s encapsulation (in a nutshell)

Slide 108

Slide 108 text

Encapsulation in PHP

Slide 109

Slide 109 text

“class” keyword

Slide 110

Slide 110 text

Groups properties and methods

Slide 111

Slide 111 text

class  MyPluginOptions   {   }

Slide 112

Slide 112 text

Properties

Slide 113

Slide 113 text

Store class data

Slide 114

Slide 114 text

class  MyPluginOptions   {          var  $options  =  array();   }

Slide 115

Slide 115 text

Methods

Slide 116

Slide 116 text

Define class behaviour

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

Visibility

Slide 121

Slide 121 text

Controls access to methods and properties

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

Exercise #3

Slide 125

Slide 125 text

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.)

Slide 126

Slide 126 text

Debrief

Slide 127

Slide 127 text

Learn to build prototypes

Slide 128

Slide 128 text

Work on the problem

Slide 129

Slide 129 text

It’s ok to stumble

Slide 130

Slide 130 text

How was the exercise?

Slide 131

Slide 131 text

Share your answers

Slide 132

Slide 132 text

Where to go from here

Slide 133

Slide 133 text

Use your notes to guide your focus

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

It’s about your intention and attitude

Slide 136

Slide 136 text

You have to show up and do the work

Slide 137

Slide 137 text

Let’s review the process

Slide 138

Slide 138 text

You found a problem

Slide 139

Slide 139 text

Problems are good objectives

Slide 140

Slide 140 text

You broke it down into classes

Slide 141

Slide 141 text

It’s a different way to solve a problem

Slide 142

Slide 142 text

Your worked on it and built your classes

Slide 143

Slide 143 text

You ran into issues and took notes

Slide 144

Slide 144 text

You review what you did well and where you stumbled

Slide 145

Slide 145 text

You repeat the process

Slide 146

Slide 146 text

Find a problem Break it down into classes Work on the problem Review the bad (and the good)

Slide 147

Slide 147 text

That’s how you’ll discover object-oriented programming

Slide 148

Slide 148 text

Thank you! @twigpress