Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Using PHP with MySQL Connecting PHP to MySQL Introducing Classes

Has anyone got a really good analogy or simple explanation for what a 'class' is?

I'm referring to this PHP video, but I've got a feeling I came across it in Rails too..

Randy describes it in this video but I can't quite wrap my head around it. Can anyone help me out with an alternative description?

1 Answer

Andrew McCormick
Andrew McCormick
17,729 Points

Kind of the cliche example but suppose you are building houses, a class is like starting with a blueprint. If you decide to have both three- and four-bedroom houses, you can start with a blueprint (aka a class) called the house blueprint that has a ground floor and a second floor, but has no indoor walls drawn on the second floor. Then, you make two more secondfloor blueprints — one for the three-bedroom house and another for the fourbedroom house. (You name these new blueprints the three-bedroom house blueprint and the four-bedroom house blueprint.) So these other blueprints called 'three-bedroom' and 'four-bedroom' only describe the second floor, because they reference back to (or extend) the house blueprint which contains the info for the ground floor and the exterior of the second floor. So now rather than recreating all the directions for building the initial structure of a house, you can use your class (aka blueprint) to create many houses from one set of instructions.

class reference image

*partially taken from Java For Dummies

This is fantastic! Thank you :)

So the class is a framework/blueprint to create objects - it tell objects what methods and properties it should include - but can you add properties and methods to an object that has been created from a class? i.e. customize it?