Date post: | 20-Dec-2014 |
Category: |
Technology |
Upload: | brandonsavage |
View: | 7,692 times |
Download: | 0 times |
Beautiful Models In PHP
By Brandon Savage
Do your models look like this?
You never wanted your code to look
this way.
Let’s fix it.
Who am I?
• Software engineer at Mozilla.
• Blogger, speaker, author of Mastering Object Oriented PHP
• Instrument rated private pilot.
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!)
What is a model?
(Conceptual) Model - a method for representing ‘concepts’ (entities)
and the relationship between them. (Wikipedia)
A model is a method of
representing data in a logical way.
Users
Customers
Transactions
Clients
Blog posts
Comments
Sermons
Political parties
News stories
Lovers
Friends
Television shows
Sometimes models map directly to a database
table.
A lot of times they don’t.
Models != Database Tables
In fact, models may not have databases
at all.
MySQL/PostgreSQL
MongoDB
Redis
Memcache
File system
REST APIs
Elastic Search
Amazon
Web services
Other web apps
APC
What are the components of a
model?
1. The thing you’re modeling.
Users
Customers
Transactions
Clients
Blog posts
Comments
Sermons
Political parties
News stories
Lovers
Friends
Television shows
2. The place the data is stored.
MySQL/PostgreSQL
MongoDB
Redis
Memcache
File system
REST APIs
Elastic Search
Amazon
Web services
Other web apps
APC
3. The method for interacting with these two items.
Domain modeling
Single responsibility
principle
How do you design a model?
Start with a question: what am I
modeling?
Bank customers have accounts.
They also have phone numbers.
<?php
class Customer { ... }
<?php
class PhoneNumber { ... }
<?php
class Account { ... }
<?php
class Customer { protected $phone_numbers = array(); protected $accounts = array(); public function get_phone_numbers() { ... }
}
<?php
class Account { protected $customer;
}
Each value object is populated by the
gateway object.
Items that can be modeled
individually should be.
Items that are too small to model
individually shouldn’t be.
What parts of an app belong in a
model?
Data storage and retrieval operations
Business logic
Validation logic
What parts of an app do not belong
in a model?
Display logic
Object creation
Data passing and collection
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
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
Questions?
•Email: [email protected]
•Twitter: @brandonsavage
•Website: http://www.brandonsavage.net