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 trial

PHP

Filipe Pacheco
seal-mask
.a{fill-rule:evenodd;}techdegree
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 Points

php oop and database

I've gone through the php oop course, but I'm wondering something.Let's say I create a class for a recipe, with title, description, ingredients, directions, tags, etc, but I also have a database with a recipe table with all these collums. I could get all the information directly from the database, without the need to use a class and create an object for each recipe. My question is, what would be the point in using oop in this situation and how can you mak good use of it?

Thanks

Simon Coates
Simon Coates
28,694 Points

You could use the row to create an instance or have the object wrap the row. In either event, you could add logic for dealing with the database row. Eg. a recipe object might define a method called containsGluten, or one which creates a formatted representation of the recipe. Sometimes you might not gain much, but other times, you might be able to get something that is more user friendly and abstract (represents an interface, with options to alter implementation details). Most objects are just simple data, rewritten to give you bundled functionality and a logical entity that's easy to think about. You can think of the Recipe as a recipe, not a table row. The organisation of an object can help with composition, and avoiding creating god objects. Alena Holligan also posted a long answer on when to use object code vs procedural at https://teamtreehouse.com/community/when-to-use-oop-over-procedural-coding if that helps.

Alena Holligan
Alena Holligan
Treehouse Teacher

The database just stores the data. When you retrieve that data you'll need to put it into SOME type of variable so you can actually do something with it. This variable would be either an array or an object, depending on how you decide to retrieve that data. So if your data is in a database, you would retrieve that data (with something like PDO) which would create the object for you. So the recipe object would use the PDO class, not the recipe class we wrote in the course. But if you wanted to add specific "methods" (functions) to the class, you could create your own class. That class could either extend the PDO class (which we haven't covered yet, but check the link to learn more), or pulls the data from the PDO object. Then your data and functionality could be wrapped together.