Transcript
Page 1: Stories from the other side

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 2: Stories from the other side

Stories fromthe Other Side

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 3: Stories from the other side

Exploring OOP andFunctional Programming(to become a better programmer)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 4: Stories from the other side

Why am I interested in this?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 5: Stories from the other side

Smart guys go FP (lambdalicious, phunkie)The Little SchemerScala is the rage!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 6: Stories from the other side

What is this, OOP + FP?Where did these paradigms start?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 7: Stories from the other side

The term "Object Oriented Programming" was coined by Alan Kay

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 8: Stories from the other side

Original image by Marcin Wichary - Thanks!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 9: Stories from the other side

Amongst many other achievements,Alan Kay created Smalltalk

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 10: Stories from the other side

"OOP to me means only messaging,local retention and protection

and hiding of state-process, and extreme late-binding of all things"

It can be done in Smalltalk and in LISP."~ Alan Kay

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 11: Stories from the other side

"It can be done in Smalltalk and in LISP."

wait... WHAT?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 12: Stories from the other side

OOP in LISP?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 13: Stories from the other side

((lambda (f) (define pick (lambda (n lat) (cond ((zero? (sub1 n)) (car lat)) (else (pick (sub1 n) (cdr lat))))))

It's lists and recursion all the way down

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 14: Stories from the other side

How does OOP work with that?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 15: Stories from the other side

What is OOP?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 16: Stories from the other side

Is it about inheritance?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 17: Stories from the other side

"OOP to me means only messaging,local retention and protection

and hiding of state-process, and extreme late-binding of all things"

~ Alan Kay

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 18: Stories from the other side

Kay doesn't mention inheritance.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 19: Stories from the other side

Is OOP about classes?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 20: Stories from the other side

Kay doesn't mention classes either.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 21: Stories from the other side

If OOP is not aboutclasses or inheritance,

what is it about?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 22: Stories from the other side

Lets start with

Messaging

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 23: Stories from the other side

I guess that means method calls.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 24: Stories from the other side

In class based OOP,the methods of a class define its

Interface.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 25: Stories from the other side

Interface==

Related MethodsMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 26: Stories from the other side

Quite similar to

related Functionswithin a Namespace

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 27: Stories from the other side

So OOP is about grouping methods together?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 28: Stories from the other side

"... local retention and protection ..."~ Alan Kay

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 29: Stories from the other side

Oh yeah, objects have states.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 30: Stories from the other side

What is a state?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 31: Stories from the other side

State in class based OOP:class Foo{ private static $class_state = 42;

private $instance_state = -0.2;}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 32: Stories from the other side

State in RL is more likepublic function __construct(...){ $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames();}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 33: Stories from the other side

What properties do these examples of state have?

public function __construct(...){ $this->type = 'simple'; $this->customerSession = $customerSession; $this->scopeConfig = $scopeConfig; $this->counter = 0; $this->id = $_REQUEST['id']; $this->names = $db->getNames();}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 34: Stories from the other side

A literal $this->type = 'simple';

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 35: Stories from the other side

A shared object $this->customerSession = $customerSession;

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 36: Stories from the other side

Literal zero(but looks like that might change)

$this->counter = 0;

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 37: Stories from the other side

Global state $this->id = $_REQUEST['id'];

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 38: Stories from the other side

Stuff from the DB $this->names = $db->getNames();

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 39: Stories from the other side

Can we partition those examples?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 40: Stories from the other side

1. State that is always the same2. State that might change

(within the lifespan of the object)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 41: Stories from the other side

State that might change: → Counter→ Session

→ Filesystem / DB / Internet→ Globals

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 42: Stories from the other side

Non-changing state→ The Type ID

→ Application State→ Configuration

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 43: Stories from the other side

What can we do with this information?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 44: Stories from the other side

Pure Functions!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 45: Stories from the other side

Pure functions?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 46: Stories from the other side

→ Don't cause any side effects→ Given the same arguments

always return the same result

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 47: Stories from the other side

What areside effects?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 48: Stories from the other side

→ Changing state non-local to the function scope

→ DB or Filesystem or Network access→ Throwing Exceptions

→ Forking

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 49: Stories from the other side

No side effectswas one part.

What was the other?MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 50: Stories from the other side

Same Arguments==>

Same ResultMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 51: Stories from the other side

A function that always returns the same result given the same arguments is called

Referentially Transparent

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 52: Stories from the other side

Referentially Transparent Functions

must not use state that might change!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 53: Stories from the other side

Why should we care?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 54: Stories from the other side

Benefitsof Pure Functions!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 55: Stories from the other side

→ Easier to think about(reasonability)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 56: Stories from the other side

→ Easier to break apart(decomposability)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 57: Stories from the other side

→ Easier to combine(composability & reuseability)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 58: Stories from the other side

→ Easier to show correctness(testability)

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 59: Stories from the other side

→ Easier to parallelize(threadability ;))

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 60: Stories from the other side

Mkay.

But what about the changing things?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 61: Stories from the other side

How can we deal with changing state?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 62: Stories from the other side

We treat objects as snapshots of thestate of the world in one moment.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 63: Stories from the other side

The state of the world in thatmoment will never change.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 64: Stories from the other side

If change occurs, it means now is a different moment in time.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 65: Stories from the other side

We can represent change in two ways:→ by modifying the object

→ by creating a new instance

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 66: Stories from the other side

ImmutabilityMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 67: Stories from the other side

For example, what happens when an admin logs in?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 68: Stories from the other side

$loggedInAdmin = $loggedOutAdmin->login();

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 69: Stories from the other side

public function login(){ return new self($isLoggedIn = true);}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 70: Stories from the other side

But what if a method needs the

"current instance"?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 71: Stories from the other side

Either that object has to be recreatedwith the current instance,

or the current instance has to bepassed as a method argument.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 72: Stories from the other side

OOP style$authorizationManager->hasAccess($dashboard, $loggedInAdmin);

$authorizationManager->hasAccess($dashboard, $loggedOutAdmin);

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 73: Stories from the other side

In FP$has_access($dashboard, $loggedInAdmin);

$has_access($dashboard, $loggedOutAdmin);

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 74: Stories from the other side

There are more parameters in RL!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 75: Stories from the other side

Okay, lets reduce the argument count by adding properties to the object.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 76: Stories from the other side

$dashboardAuthorizationManager =

new class($authorizationManager, $dashboard) {

private $authorizationManager; private $page;

public function __construct($authorizationManager, $page) { $this->authorizationManager = $authorizationManager; $this->page = $page; }

public function hasAccess($admin) { return $this->authorizationManager ->hasAccess($this->page, $admin); }}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 77: Stories from the other side

Only one argument left:$pageAuthorizationManager->hasAccess($loggedInAdmin);

$pageAuthorizationManager->hasAccess($loggedOutAdmin);

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 78: Stories from the other side

How might that look like in FP?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 79: Stories from the other side

$has_access_to_dashboard =

function ($admin) use ($dashboard, $has_access) { return $has_access($dashboard, $admin); }

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 80: Stories from the other side

$has_access_to_dashboard($loggedInAdmin);

$has_access_to_dashboard($loggedOutAdmin);

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 81: Stories from the other side

Can we remove all arguments?

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 82: Stories from the other side

$loggedInAdminDashboardAuthorization =

new class($dashboardAuthorizationManager, $loggedInAdmin){ private $pageAuthorizationManager; private $admin;

public function __construct($pageAuthorizationManager, $admin) { $this->pageAuthorizationManager = $pageAuthorizationManager; $this->admin = $admin; }

public function hasAccess() { return $this->pageAuthorizationManager ->hasAccess($this->admin); }}

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 83: Stories from the other side

In FP:$has_logged_in_admin_access_to_dashboard =

function () use ($has_access_to_dashboard, $admin) { return $has_access_to_dashboard($admin); }

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 84: Stories from the other side

We can bundle stateand functions

without classes!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 85: Stories from the other side

We can bundle stateand process

without classes!MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 86: Stories from the other side

Look Ma,OOP in PHP without

hands classes!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 87: Stories from the other side

We have the choice:→ pass state with method arguments→ set state as object properties

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 88: Stories from the other side

The more volatile the object state,the more often we need to

create new instances...

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 89: Stories from the other side

...if we want to havePure Functions.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 90: Stories from the other side

Lesson from FP:Distinguish between mutable and

immutable object properties.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 91: Stories from the other side

"Mutable stateful objects are the new spaghetti code"

~ Rich Hickey

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 92: Stories from the other side

SummaryMageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 93: Stories from the other side

Concepts from the FP languages can be useful in OO PHP.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 94: Stories from the other side

We can benefit from designing ourOO code so it exhibits the properties of

pure functions.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 95: Stories from the other side

We barely scratched the surface.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 96: Stories from the other side

Don't be afraid of FP's mathematical pattern names. It's still just code.

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 97: Stories from the other side

Building these slides Ilearned something about OOP.

I hope you found it interesting, too!

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp

Page 98: Stories from the other side

(if (has-communication? you vinai) (communicate you vinai) (enjoy you remaining-agenda))

MageTitans Manchester, UK - #MageTitansMCR 2016-11-12 - twitter://@VinaiKopp


Top Related