In object oriented programming, a class is a construct that defines a collection of properties and methods. It can be viewed 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.

Another way to understand the difference between classes and objects is to think of classes as the mold and objects as the items produced using the mold.

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.

Share this comparison:

If you read this far, you should follow us:

"Class vs Object." Diffen.com. Diffen LLC, n.d. Web. 7 Dec 2016. < >