intermediate oop in php

90
Welcome to php[world] 2015 Wifi: SSID: Sheraton_Conference user: php2015 pass: sheraton Twitter: #phpworld Rate talks on joind.in https://joind.in/event/phpworld2015 Intermediate OOP in PHP

Upload: david-stockton

Post on 19-Jan-2017

1.165 views

Category:

Technology


2 download

TRANSCRIPT

Page 2: Intermediate OOP in PHP

INTERMEDIATE OOP IN PHP

PHP World - November 18, 2015David Stockton

@dstockto

Page 3: Intermediate OOP in PHP

WHAT WE’LL COVER• Magic Methods

• Polymorphism

• Collections

• Filtering Collections

• SPL Iterator

Page 4: Intermediate OOP in PHP

WHAT WE’LL COVER• SPL Count

• SPL ArrayAccess

• Listener Pattern

• Loose Coupling

• High Cohesion

Page 5: Intermediate OOP in PHP

MAGIC METHODS

• Methods all start with __

Page 6: Intermediate OOP in PHP

__construct

Page 7: Intermediate OOP in PHP

__construct

• __construct is called when you “new” up an object

• Initialize object properties

• Don’t do work!

Page 8: Intermediate OOP in PHP

__destruct

• __destruct is called when an object goes out of scope

• Shut down connections, cleanup work

Page 9: 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 10: Intermediate OOP in PHP

__call

Page 11: Intermediate OOP in PHP

__callStatic

• Just like __call but for static calls out of scope

• Foo::createFromThing()

Page 12: 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 13: Intermediate OOP in PHP

__get

• __get($name)

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

• $value = $foo->madeUpValue;

Page 14: Intermediate OOP in PHP

__get

Page 15: Intermediate OOP in PHP

__set

__set($name, $value)

Page 16: 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 17: Intermediate OOP in PHP

__isset__isset($name)

isset($object->variable)

Page 18: Intermediate OOP in PHP

__unset__unset($name)

unset($object->variable);

Page 19: 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 20: 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 21: Intermediate OOP in PHP

@METHOD DOCBLOCK

Page 22: 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 23: Intermediate OOP in PHP

__toString()

• Retrieve string representation of a class

• Must return a string

• Cannot throw an exception

Page 24: Intermediate OOP in PHP

__toString

Page 25: Intermediate OOP in PHP

__toString

Page 26: Intermediate OOP in PHP

__cloneCreate a new object based on an existing object

Called when clone is used:

$foo = clone $bar;

Page 27: Intermediate OOP in PHP

__clone

• By default, clone is shallow

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

Page 30: Intermediate OOP in PHP

CHANGING $BAR

Page 31: Intermediate OOP in PHP

NOW WITH __clone

Page 32: Intermediate OOP in PHP

$FOO = NEW FOO();

Foo$foo

Foo$bar$foo Bar

Page 33: Intermediate OOP in PHP

$FOO2 = CLONE $FOO;

Foo$bar$foo Bar

Foo$bar$bar

__clone called on the clone

Page 34: Intermediate OOP in PHP

$FOO2 = CLONE $FOO;

Foo$bar$foo Bar

Foo$bar$bar Bar

Page 35: Intermediate OOP in PHP

CHANGING $BAR

Page 36: 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 37: Intermediate OOP in PHP

__invoke

Page 38: Intermediate OOP in PHP

__invoke

Page 39: 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 40: Intermediate OOP in PHP

__set_state

• Used with var_export

• Allows setting of object variables from an array of input

Page 41: Intermediate OOP in PHP

__debugInfo

• New in PHP 5.6

• Controls output of var_dump

Page 42: Intermediate OOP in PHP

__debugInfo

Page 43: Intermediate OOP in PHP

var_dump

Page 44: Intermediate OOP in PHP

ADD __debugInfo

Page 45: Intermediate OOP in PHP

var_dump AGAIN

Page 46: Intermediate OOP in PHP

POLYMORPHISMAllow code to use different

objects in the same way

Page 47: Intermediate OOP in PHP

SPEAKABLEinterface

Page 48: Intermediate OOP in PHP

POLYMORPHISM

Page 49: Intermediate OOP in PHP

POLYMORPHISM

Page 50: Intermediate OOP in PHP

COLLECTIONS AND POLYMORPHISM

Page 51: Intermediate OOP in PHP

SILVERWARE DRAWER

Page 52: Intermediate OOP in PHP

SILVERWARECONTAINERinterface

Page 53: Intermediate OOP in PHP

SILVERWAREinterface

Page 54: Intermediate OOP in PHP

Abstract Utensil Class

“Type a quote here.”

Page 55: Intermediate OOP in PHP

A FORKING EXAMPLE

Page 56: Intermediate OOP in PHP

SILVER SPOONS

Page 57: Intermediate OOP in PHP

KNIFE

Page 58: Intermediate OOP in PHP

PUT SILVERWARE AWAY

Page 59: Intermediate OOP in PHP

HOW MANY UTENSILS?

• Add method getNumberOfItems

• Implement SPL Countable

Page 60: Intermediate OOP in PHP

SPL COUNTABLE

Page 61: Intermediate OOP in PHP

HOW TO USE?

Page 62: Intermediate OOP in PHP

RETRIEVE SPECIFIC UTENSILS

Page 63: Intermediate OOP in PHP

SPL TO THE RESCUE

• Implement SPL ArrayAccess

• Make our object work like an array

Page 64: Intermediate OOP in PHP

SPL ARRAYACCESS

Page 65: Intermediate OOP in PHP

SPL ARRAYACCESS

Page 66: Intermediate OOP in PHP

WHAT DOES THAT ALLOW?

Page 67: Intermediate OOP in PHP

USE AN ITERATOR:

• IMPLEMENT ITERATOR• EXTEND ONE OF THESE:

• AppendIterator• ArrayIterator• CachingIterator• CallbackFilterIterator• InfiniteIterator• …ETC.

Page 68: Intermediate OOP in PHP

EASY WAYExtend ArrayIterator

Page 69: Intermediate OOP in PHP

ADD SOME SILVERWARE

Page 70: Intermediate OOP in PHP

NOW WHAT? ITERATE IT!

Page 71: Intermediate OOP in PHP

A NEW ITERATOR

Page 72: Intermediate OOP in PHP

ONLY GET CLEAN UTENSILS

Page 73: Intermediate OOP in PHP

SPLSUBJECT / SPLOBSERVERMoving right along

Page 74: Intermediate OOP in PHP

IMPLEMENT SplSubject

Page 75: Intermediate OOP in PHP

EMAIL CLASS CONTINUED

Page 76: Intermediate OOP in PHP

VALIDATE USES OUR OBSERVERS

Page 77: Intermediate OOP in PHP

BUILD SOME OBSERVERSInvalid Emails don’t send

Page 78: Intermediate OOP in PHP

LIMIT SENDS BY DOMAINS?

Page 79: Intermediate OOP in PHP

PUT IT ALL TOGETHER

Page 80: Intermediate OOP in PHP

LOOSE COUPLING

Page 81: Intermediate OOP in PHP

LOOSE COUPLING• Minimize number of hard-coded dependencies

• Type hint interfaces, not classes

• Dependency Injection / Inversion of Control

Page 82: Intermediate OOP in PHP

DEPENDENCY INJECTION SIMPLY

Page 83: Intermediate OOP in PHP
Page 84: Intermediate OOP in PHP

HIGH COHESION

Do one thing well

Page 85: Intermediate OOP in PHP

HIGH COHESION• Do one thing well

• Methods

• Classes

• Packages / Namespaces

• Everything the class does is related to the goal

Page 86: 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 87: Intermediate OOP in PHP

PREFER COMPOSITION OVER INHERITANCE

Page 88: 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 89: Intermediate OOP in PHP

PHOTO CREDITS

• Trailer hitch - https://www.flickr.com/photos/jeremybrooks/2476396242• The Glue Side of the Force - https://www.flickr.com/photos/st3f4n/4085958000

All other photos are © David Stockton, 2015

Page 90: Intermediate OOP in PHP

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

Questions?