From Diffen
| Class | Object | ||
|---|---|---|---|
| Add Comparison Table | |||
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; /* 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.
[edit] Related Articles
Comments: Class vs Object [Add Comments] |
There are no comments for Class vs Object. You can post a comment. |


