Home Programming PHP Class vs Object - the difference explained

Class vs Object - the difference explained

E-mail Print
Share/Save/Bookmark

Class

A class is a code template used to generate objects. It is some sort of a blueprint that describes the object.
In PHP a class name can be alphanumerical string that cannot begin with a number. Class code must be enclosed with braces and it consists of attributes and operations.

Example

class Car {
//some variable
private $owner;

//operation
public function setOwner() {
//something to do ...
}

}

Object

An object is data that is structured according to the template defined in a class. It is instance of a class.

Example

$myCar = new Car();
$obamasCar = new Car();

print $myCar; //outputs Object id #1
print $obamasCar; //outputs Object id #2

In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other cars in existence, all of the same make and model even owned by the same person. Each car was built from the same blueprint. In object-oriented terms, we say that the car is an instance of the class of objects known as cars.
Hits: 968
Comments (0)Add Comment

Write comment

busy
Last Updated ( Friday, 08 January 2010 21:46 )  

Donate

Do you find content useful? Please donate so I can cover my hosting expenses! Thanks!