Jun 06, 2012, by admin
In the earlier PHP5 tutorial you learnt how to create a class in PHP5. In this tutorial you will learn how to create an object of a PHP5 class. But before we begin, lets understand what is an object.
An object is a living instance of a class. This means that an object is created from the definition of the class and is loaded in memory. A good analogy to understand this is to compare objects with humans – and understand that all of us (you and I) are objects. If God wants to send a human to earth, what is easy for Him to do? Create and define properties and attributes of each human separately or create a one time template and generate objects out if it. Therefore, this onetime template is a Class and you, I & everyone in this world is an object – that is a living instance of class Human.
Creating PHP5 Class Object
To create an object of a PHP5 class we use the keyword new. Below is the syntax style of how to create class objects in PHP5:
Look at the example below:
class Customer {
private $first_name, $last_name;
public function getData($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function printData() {
echo $this->first_name . ” : ” . $this->last_name;
}
}
$c1 = new Customer();
$c2 = new Customer();