intermediate oop in php

85
Intermediate OOP in PHP PHP World - November 13, 2014 David Stockton @dstockto

Upload: david-stockton

Post on 24-Jun-2015

357 views

Category:

Technology


6 download

DESCRIPTION

Intermediate OOP in PHP is for developers who have moved past the basics of OOP and want to start exploring what's available and the power that OOP brings.

TRANSCRIPT

Page 1: Intermediate oop in php

Intermediate OOP in PHP

PHP World - November 13, 2014 David Stockton

@dstockto

Page 2: Intermediate oop in php

What we’ll cover

Magic Methods

Polymorphism

Collections

Filtering Collections

SPL Iterator

Page 3: Intermediate oop in php

What we’ll cover

SPL Count

SPL ArrayAccess

Listener Pattern

Loose Coupling

High Cohesion

Page 4: Intermediate oop in php

Magic Methods

Methods all start with __

Page 5: Intermediate oop in php

__construct & __destruct

__construct is called when you new up an object

Initialize object properties

Don’t do work!

__destruct is called when an object goes out of scope

Shut down connections, cleanup work

Page 6: Intermediate oop in php

__call

__call($name, array $arguments)

Method called when method is called that is not in scope

Method doesn’t exist

Method is private / protected and called from outside object

Page 7: Intermediate oop in php

__call

Page 8: Intermediate oop in php

__callStatic

Just like __call but for static calls out of scope

Foo::createFromThing()

Page 9: Intermediate oop in php

__get, __set, __isset, __unset

In many cases, if you need one, you need them all

Weird bugs if you don’t implement them all

Page 10: Intermediate oop in php

__get

__get($name)

Called when reading an object property that’s not in scope

!

$value = $foo->madeUpValue;

Page 11: Intermediate oop in php

__get

Page 12: Intermediate oop in php

__set

__set($name, $value)

!

!

Page 13: Intermediate oop in php

So, why do we need them all?

$foo->bar = ‘baz’;

$value = $foo->bar; // Get baz

if (isset($foo->bar)) { $value = $foo->bar; // Never get here if no __isset }

Page 14: Intermediate oop in php

__isset

__isset($name)

isset($object->variable)

!

!

Page 15: Intermediate oop in php

__unset

__unset($name)

unset($object->variable);

!

!

Page 16: Intermediate oop in php

What you can do with them

Implement a class that doesn’t allow dynamic values

Implement a class that only allows values with certain naming conventions

Automatic calling of accessors (getters and setters)

Page 17: Intermediate oop in php

The downsides

Harder to follow, no “ctrl-click” in IDEs

No auto-complete for __call or __callStatic methods in IDEs

Can use @method doc block to help

@method int add() add(numeric $a, numeric $b) Add numbers

Page 18: Intermediate oop in php

@method docblock

Page 19: Intermediate oop in php

__sleep & __wakeup

Called on serialize() and unserialize() calls

__sleep: Use to close connections, cleanup

__wakeup: Use to re-establish connections, setup

Why?

Some things in PHP are not serializable

Page 20: Intermediate oop in php

__toString()

Retrieve string representation of a class

Must return a string

Cannot throw an exception

Page 21: Intermediate oop in php

__toString

Page 22: Intermediate oop in php

__toString

Page 23: Intermediate oop in php

__clone

Create a new object based on an existing object

Called when clone is used:

$foo = clone $bar;

Page 24: Intermediate oop in php

__clone

By default, clone is shallow

!

!

!

Page 25: Intermediate oop in php

$foo = new Foo();

Foo$foo

Foo $bar$foo Bar

Page 26: Intermediate oop in php

$foo2 = clone $foo;

Foo $bar$foo Bar

Foo $bar$bar

Page 27: Intermediate oop in php

Now with __clone

Page 28: Intermediate oop in php

$foo = new Foo();

Foo$foo

Foo $bar$foo Bar

Page 29: Intermediate oop in php

$foo2 = clone $foo;

Foo $bar$foo Bar

Foo $bar$bar

__clone called on the clone

Page 30: Intermediate oop in php

$foo2 = clone $foo;

Foo $bar$foo Bar

Foo $bar$bar Bar

Page 31: Intermediate oop in php

__invoke

Allows an object to be used like a function or callable

Object can be built configured and then “executed”

Page 32: Intermediate oop in php

__invoke

Page 33: Intermediate oop in php

__invoke

Page 34: Intermediate oop in php

__autoload

Don’t use __autoload, use spl_autoload_register

Called when a class, interface, or trait is used that hasn’t been loaded

Allows just in time loading of classes, no more requires and includes at the top of files

Page 35: Intermediate oop in php

__set_state

Used with var_export

Allows setting of object variables from an array of input

Page 36: Intermediate oop in php

__debugInfo

New in PHP 5.6

Controls output of var_dump

Page 37: Intermediate oop in php

__debugInfo

Page 38: Intermediate oop in php

var_dump

Page 39: Intermediate oop in php

Add __debugInfo

Page 40: Intermediate oop in php

var_dump again

Page 41: Intermediate oop in php

PolymorphismAllow code to use different objects in the same

way

Page 42: Intermediate oop in php

Speakableinterface

Page 43: Intermediate oop in php

Polymorphism

Page 44: Intermediate oop in php

Polymorphism

Page 45: Intermediate oop in php

Collections and Polymorphism

Page 46: Intermediate oop in php

Silverware Drawer

Page 47: Intermediate oop in php

SilverwareContainerinterface

Page 48: Intermediate oop in php

Silverwareinterface

Page 49: Intermediate oop in php

Abstract Utensil Class

Page 50: Intermediate oop in php

A Forking Example

Page 51: Intermediate oop in php

Silver Spoons

Page 52: Intermediate oop in php

Knife

Page 53: Intermediate oop in php

Put silverware away

Page 54: Intermediate oop in php

How many utensils?

Add method getNumberOfItems

!

!

Implement SPL Countable

- OR -

Page 55: Intermediate oop in php

SPL Countable

Page 56: Intermediate oop in php

How to use?

Page 57: Intermediate oop in php

Retrieve Specific Utensils

Page 58: Intermediate oop in php

SPL to the Rescue

Implement SPL ArrayAccess

Make our object work like an array

Page 59: Intermediate oop in php

SPL ArrayAccess

Page 60: Intermediate oop in php

SPL ArrayAccess

Page 61: Intermediate oop in php

What does that allow?

Page 62: Intermediate oop in php

Use an iterator: !

• Implement iterator • Extend one of these: • AppendIterator • ArrayIterator • CachingIterator • CallbackFilterIterator • InfiniteIterator • …etc.

Loop Over Utensils in Drawer

Page 63: Intermediate oop in php

Easy wayExtend ArrayIterator

Page 64: Intermediate oop in php

Add some silverware

Page 65: Intermediate oop in php

Now what? Iterate it!

Page 66: Intermediate oop in php

A new iterator

Page 67: Intermediate oop in php

Only get clean utensils

Page 68: Intermediate oop in php

SPLSubject / SPLObserverMoving right along

Page 69: Intermediate oop in php

Implement SplSubject

Page 70: Intermediate oop in php

Email class continued

Page 71: Intermediate oop in php

Validate uses our observers

Page 72: Intermediate oop in php

Build some observersInvalid Emails don’t send

Page 73: Intermediate oop in php

Limit Sends by domains?

Page 74: Intermediate oop in php

Put it all together

Page 75: Intermediate oop in php

Loose Coupling

Page 76: Intermediate oop in php

Loose Coupling

Minimize number of hard-coded dependencies

Type hint interfaces, not classes

Dependency Injection / Inversion of Control

Page 77: Intermediate oop in php

Dependency Injection Simply

Page 78: Intermediate oop in php
Page 79: Intermediate oop in php

High CohesionDo one thing well

Page 80: Intermediate oop in php

High Cohesion

Do one thing well

Methods

Classes

Packages / Namespaces

Everything the class does is related to the goal

Page 81: Intermediate oop in php

–David Stockton

“If you have to use ‘and’ to describe what your method or class does,

it’s probably doing too much.”

Page 82: Intermediate oop in php

Prefer Composition over Inheritance

Page 83: Intermediate oop in php

Prefer Composition over Inheritance

Inheritance is not the only way to expand functionality of a class

Often it’s not even the best way

Page 84: Intermediate oop in php

Photo Credits

• Lego Magician - https://www.flickr.com/photos/evaysucamara/5438832695 • Silverware - https://www.flickr.com/photos/jenny-pics/4128947072 • Fork - https://www.flickr.com/photos/naturesdawn/2507321223 • Trailer hitch - https://www.flickr.com/photos/jeremybrooks/2476396242 • The Glue Side of the Force - https://www.flickr.com/photos/st3f4n/4085958000 • Last Will and Testament - https://www.flickr.com/photos/ken_mayer/5599532152 • Guest Composer - https://www.flickr.com/photos/tamuc/12843465194

Page 85: Intermediate oop in php

!

Please rate this talk: https://joind.in/11890

Questions?