Beautiful Models in PHP

8,484 views

Published on

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
  • Interesting, but how could you suggest to manage joins between models, especially when the data is a DB?
    For example, if we want the list of Customer and PhoneNumber, is convenient to have a single JOIN query instead of 2 queries.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
8,484
On SlideShare
0
From Embeds
0
Number of Embeds
848
Actions
Shares
0
Downloads
17
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Beautiful Models in PHP

  1. 1. Beautiful Models In PHP By Brandon Savage
  2. 2. Do your models look like this?
  3. 3. You never wanted your code to look this way. Let’s fix it.
  4. 4. Who am I?• Software engineer at Mozilla.• Blogger, speaker, author of Mastering Object Oriented PHP• Instrument rated private pilot.
  5. 5. Questions we’ll answer1.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!)
  6. 6. What is a model?
  7. 7. (Conceptual) Model - a method forrepresenting ‘concepts’ (entities) and the relationship between them. (Wikipedia)
  8. 8. A model is a method of representing data in a logical way.
  9. 9. Friends Blog postsCustomers News stories Sermons Users Clients Political partiesTransactions Television shows Comments Lovers
  10. 10. Sometimes models map directly to a database table. A lot of times they don’t.
  11. 11. Models != Database Tables
  12. 12. In fact, models may not have databases at all.
  13. 13. MySQL/PostgreSQLREST APIs Amazon Redis Elastic Search Memcache APC MongoDB File system Web services Other web apps
  14. 14. What are thecomponents of a model?
  15. 15. 1. The thing you’re modeling.
  16. 16. Friends Blog postsCustomers News stories Sermons Users Clients Political partiesTransactions Television shows Comments Lovers
  17. 17. 2. The place the data is stored.
  18. 18. MySQL/PostgreSQLREST APIs Amazon Redis Elastic Search Memcache APC MongoDB File system Web services Other web apps
  19. 19. 3. The method forinteracting with these two items.
  20. 20. Domain modeling
  21. 21. Single responsibility principle
  22. 22. How do you design a model?
  23. 23. Start with a question:what am I modeling?
  24. 24. Bank customers have accounts.They also have phone numbers.
  25. 25. <?phpclass Customer { ... }<?phpclass PhoneNumber { ... }<?phpclass Account { ... }
  26. 26. <?phpclass Customer { protected $phone_numbers = array(); protected $accounts = array(); public function get_phone_numbers() { ... }}<?phpclass Account { protected $customer;}
  27. 27. Each value object is populated by the gateway object.
  28. 28. Items that can bemodeled individually should be.
  29. 29. Items that are too small to model individually shouldn’t be.
  30. 30. What parts of an app belong in a model?
  31. 31. Data storage andretrieval operations
  32. 32. Business logic
  33. 33. Validation logic
  34. 34. What parts of an app do not belong in a model?
  35. 35. Display logic
  36. 36. Object creation
  37. 37. Data passing and collection
  38. 38. 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
  39. 39. The Object Oriented PHP MasterclassGet hands on experience using what you just learned. Coming in March. Seats are limited! Sign up for updates at brandonsavage.net
  40. 40. Questions?• Email: brandon@brandonsavage.net• Twitter: @brandonsavage• Website: http://www.brandonsavage.net

×