41
Beautiful Models In PHP By Brandon Savage

Beautiful Models in PHP

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Beautiful Models in PHP

Beautiful Models In PHP

By Brandon Savage

Page 2: Beautiful Models in PHP

Do your models look like this?

Page 3: Beautiful Models in PHP

You never wanted your code to look

this way.

Let’s fix it.

Page 4: Beautiful Models in PHP

Who am I?

• Software engineer at Mozilla.

• Blogger, speaker, author of Mastering Object Oriented PHP

• Instrument rated private pilot.

Page 5: Beautiful Models in PHP

Questions we’ll answer

1.What is a model?

2.What are the components of a model?

3.How do you design a model?

4.What parts of an app belong in a model?

5.What parts of an app do not belong in a model?

(last bullet slide I swear!)

Page 6: Beautiful Models in PHP

What is a model?

Page 7: Beautiful Models in PHP

(Conceptual) Model - a method for representing ‘concepts’ (entities)

and the relationship between them. (Wikipedia)

Page 8: Beautiful Models in PHP

A model is a method of

representing data in a logical way.

Page 9: Beautiful Models in PHP

Users

Customers

Transactions

Clients

Blog posts

Comments

Sermons

Political parties

News stories

Lovers

Friends

Television shows

Page 10: Beautiful Models in PHP

Sometimes models map directly to a database

table.

A lot of times they don’t.

Page 11: Beautiful Models in PHP

Models != Database Tables

Page 12: Beautiful Models in PHP

In fact, models may not have databases

at all.

Page 13: Beautiful Models in PHP

MySQL/PostgreSQL

MongoDB

Redis

Memcache

File system

REST APIs

Elastic Search

Amazon

Web services

Other web apps

APC

Page 14: Beautiful Models in PHP

What are the components of a

model?

Page 15: Beautiful Models in PHP

1. The thing you’re modeling.

Page 16: Beautiful Models in PHP

Users

Customers

Transactions

Clients

Blog posts

Comments

Sermons

Political parties

News stories

Lovers

Friends

Television shows

Page 17: Beautiful Models in PHP

2. The place the data is stored.

Page 18: Beautiful Models in PHP

MySQL/PostgreSQL

MongoDB

Redis

Memcache

File system

REST APIs

Elastic Search

Amazon

Web services

Other web apps

APC

Page 19: Beautiful Models in PHP

3. The method for interacting with these two items.

Page 20: Beautiful Models in PHP

Domain modeling

Page 21: Beautiful Models in PHP

Single responsibility

principle

Page 22: Beautiful Models in PHP

How do you design a model?

Page 23: Beautiful Models in PHP

Start with a question: what am I

modeling?

Page 24: Beautiful Models in PHP

Bank customers have accounts.

They also have phone numbers.

Page 25: Beautiful Models in PHP
Page 26: Beautiful Models in PHP

<?php

class Customer { ... }

<?php

class PhoneNumber { ... }

<?php

class Account { ... }

Page 27: Beautiful Models in PHP

<?php

class Customer { protected $phone_numbers = array(); protected $accounts = array(); public function get_phone_numbers() { ... }

}

<?php

class Account { protected $customer;

}

Page 28: Beautiful Models in PHP

Each value object is populated by the

gateway object.

Page 29: Beautiful Models in PHP

Items that can be modeled

individually should be.

Page 30: Beautiful Models in PHP

Items that are too small to model

individually shouldn’t be.

Page 31: Beautiful Models in PHP

What parts of an app belong in a

model?

Page 32: Beautiful Models in PHP

Data storage and retrieval operations

Page 33: Beautiful Models in PHP

Business logic

Page 34: Beautiful Models in PHP

Validation logic

Page 35: Beautiful Models in PHP

What parts of an app do not belong

in a model?

Page 36: Beautiful Models in PHP

Display logic

Page 37: Beautiful Models in PHP

Object creation

Page 38: Beautiful Models in PHP

Data passing and collection

Page 39: Beautiful Models in PHP

Want to learn more?Stop struggling

with object oriented PHP. Start mastering it instead.

DC PHP members get 10% off!Use DCPHP10 at checkout.

www.masteringobjectorientedphp.com

Page 40: Beautiful Models in PHP

The Object Oriented PHP Masterclass

Get hands on experience using what you just learned.

Coming in March. Seats are limited!

Sign up for updates at brandonsavage.net

Page 41: Beautiful Models in PHP

Questions?

•Email: [email protected]

•Twitter: @brandonsavage

•Website: http://www.brandonsavage.net