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


OOP question

Guys... I am just getting tired of this. That's the deal, I want to make a page with customers, Each item got its own id,name,price, etc.., now. From what I understand in oop each item will be an object. how can I do this.., That way :::::: down looks stupid to me. I just can't figure out how to do this., all tutorials show, $mike = new person, $eaaa = new person.. but how to make it with database...


$database = new mysqli("","raa","dfdfdf","random");
$query = $database->query("SELECT id,name,price FROM items");
while($row = $query->fetch_object()) {
    echo $row->name;


Hi there,

I added markdown to help make the code more readable. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.


3 Answers

You'll need three(ish) components.

1) Model - e.g. User, Person, Post, Comment

2) Database connector - e.g. PDO or wrapper class around PDO

3) Information Filler e.g. an object whose job it is to fill the model with the returned data.

It might be that your model extends the Information Filler and you can set attributes on the extending model that will define how it's filled with the data you get back from a database query.

I would highly recommend looking at a framework like Laravel to see how they do it.


Are you trying to learn how to use OOP to collect data from a database or are you just wanting to manipulate data that you collect from a database. By how you code looks, you can do what you want without OOP by using the fetch_assoc() function which will return all the values that match your query as an array and you would simply reference the array $row. If you want to understand how to use MySQLi's OOP you can simply go to php.net's website and search for the function you want to know about and it'll give you an explanation about it as well as detailed examples. OOP is a wonderful thing to learn but hard to grasp, if you want to go beyond what TeamTreehouse is teaching you, the best place to start is on PHP's manual website php.net. I hope this helps.


Shawn, you see what my question is bro?, I get what OOP is, I just don't know how to implement it into a code with databases... You see I know how to pull out information from DB and make it as an array, but I want each assoc array to fit a class and be an object.. Just can't figure out how to do that.. or maybe i'm wrong and just need to make a class which holds array of lets say customers.

Alright, Tom, I will make a connection with the database and get the information, pass it trough a filter class/object and then make it fill another class/object, seems fine to me :P, if i got you correctly