In object oriented programming, a class is a construct that defines a collection of properties and methods. You can think of it as a template. For example,

class Item {
  public $itemType; /* e.g. this could be "Book" or "CD" */
  public $price;
  public function printPrice() { 
    echo "The price of this {$this->itemType} is {$this->price} dollars."; 
  }
}

$catch22 = new Item();
$catch22->itemType = "Book";
$catch22->price = 25;
$catch22->printPrice(); /* outputs The price of this Book is 25 dollars. */

$americanPrayer = new Item();
$americanPrayer->itemType = "CD";
$americanPrayer->price = 22;
$americanPrayer->printPrice(); /* outputs The price of this CD is 22 dollars */

Note that in this example, $catch22 and $americanPrayer are 2 objects. Objects are instances of a class. They share the common structure that the class defines. This common structure consists of the properties ($itemType and $price in the above example) and methods (functions; printPrice() in the above example) of the class. However, the properties of different objects may be different.

In the above example, the price and item type are different for 2 objects of the same class. But both objects have a printPrice() method, a price property and an itemType property that can be used.

Comparison chart

Class versus Object comparison chart
Edit this comparison chartClassObject
Definition Class is mechanism of binding data members and associated methods in a single unit. Instance of class or variable of class.
Existence It is logical existence It is physical existence
Memory Allocation Memory space is not allocated , when it is created. Memory space is allocated, when it is created.
Declaration/definition Definition is created once. it is created many time as you require.

Special Cases

In some programming languages, e.g. Python, everything is an object. This means functions, variables, instances of a class and even actual classes are treated as objects by the programming language.

Share this comparison:

If you read this far, you should follow us:

"Class vs Object." Diffen.com. Diffen LLC, n.d. Web. 27 Mar 2017. < >