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

class vs object vs instance in PHP OOP

Hi,

Could you explain about class vs object vs instance?

Thanks, Ross

4 Answers

A class is a "blueprint" that can be used to "instantiate" different "objects".

So for instance let's go with a simple example such as a Car.

You could have a class called Car that specifies the different properties and methods that a car may contain.

Now let's say you want to "create" your first Car using the Car class as a blueprint.

Let's call our first car $car_one

To create our $car_one "object" we will use the statement $car_one = new Car();

The "new" keyword tells PHP that you want a new "instance" of the Car class

Now $car_one is an object that is an "instance" of the Car class

You can now access the different properties and methods defined by the Car class "blueprint". Example: $car_one->start();

Hopefully I didn't confuse you, but let me try to sum it up.

A class is a "blueprint" that can be used to make different "instances". Those different instances are objects of the class which was used to create them.

Hi Colton Ehrman,

Thanks for your answer,

I understand almost and I am going to ask one more.

Object and Instance are same?

Thanks, Ross

What do you think about the following example?

$car_one = new Car(); $car_two = new Car(); $car_three = new Car(); All these cars are different instances

But they are ALL "objects" of the Car class.

Yeah you are right. That makes more sense.

So all the cars are Car objects, but different instances.