Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStipe Stipic
17,520 PointsBenefit of using Collection of objects?
I'm stuck on the course where we build a collection to hold our recipe objects.
I understand how this all works but can anybody tell me is this a usually workflow that we take e.g. all recipes(users, news,..) from our database and put this into objects, and than into a collection class that holds this objects. And than we can add methods to the collection class like:
- give me all recipes sorted by the title.
- give me all recipes that have this tag ...
It confused me why go this way(make a collection class) if we can make direct queries to the mysql database to become this answeres:
- give me all recipes sorted by the title.
- give me all recipes that have this tag ...
what are the benefits (is it only performance)?
Stipe Stipic
17,520 PointsThanks for your answere. Ok, that is maybe one benefit. But let's assume we have a big users table in our DB (with lots of users), if we take this approach than we must for every visitor of our webpage take all users from the database "SELECT * FROM users" AND that can cost a lot of time, than put every of them into a collection class of objects Where we have methods e.g.:
- give me all users that live in germany
But if we make only the query "SELECT * FROM users WHERE state = 'germany'" and put an INDEX on the state field; let's assume we have 100 000 users and only three are from germany. with the first approach we load every time 100 000 users from the DB what can take a lot of time, but with the second approach we load only three users.
Dave StSomeWhere
19,870 PointsAha, I think I see the disconnect. My guess is that the video is including the data in the Class as an array for training purposes only and there most likely would be a database involved for real stuff. So, yes a collection class might not be practical, but it provides a clear use case to demonstrate fundamental concepts.
Currently the data is just an array - no persistence and you have methods like: addRecipie(), getRecipie(), and getRecipieTitles() - these methods currently access the array. If you enhance your class to use a database like MySQL then you make the adjustments inside the Class without the external users needing to change their code where they utilize the methods. Just like using PDO to access a MySQL data base - PDO is a Class (or group of classes - maybe referred to as a class library).
Sorry for the ramble, but the deeper I get into Classes the more I appreciate them. Utilizing PHPMailer, Slim and Twig (Class libs from other courses), helped reinforce the benefits.
Stipe Stipic
17,520 PointsThanks for answering.
I just wanted to know if anybody uses this in real applications (fetch all data from db into collection class). Because I was thinking that maybe I don't see the advantages.
Thanks Dave :)
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsWhat if you want/need to switch databases? By encapsulating the properties and methods into a class, your class user (php program) doesn't need to care how the database layer works (along with all the other good stuff your class does).