PHP5 Tutorial - Learn to Create a PHP5 Class Object

In the earlier PHP5 OOPS 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.

Definition of 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 Objects in PHP5 Class

To create an object of a PHP5 class we use the keyword new. Below is the syntax style of how to create objects in PHP5:

        $obj_name = new ClassName();

In the above syntax style, $obj_name is a variable in PHP. ‘new’ is the keyword which is responsible for creating a new instance of ClassName.

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();

In the above example $c1 and $c2 are two objects of the Customer Class. Both these objects are allocated different blocks in the memory. Look at the diagram below:

PHP5 Tutorial - PHP5 Class Example Diagram

Therefore, an object is a living instance of a class. Each object / living instance has its own memory space that can hold independent data values.

In the next tutorial you will learn about Attributes of a Class in PHP5.




Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

Related Posts on PHP5 Tutorial - Object Oriented Programming (OOPS)

  1. PHP5 Tutorial - Learn to create a PHP5 Class
  2. PHP5 Tutorial - Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial - Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial - Defining Methods of a PHP5 Class
  5. PHP5 Tutorial - Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
  8. PHP5 Tutorial - Magic Methods - __toString() method
  9. PHP5 Tutorial - Magic Methods - __get() and __set()
  10. PHP5 Tutorial - Magic Methods - __isset() and __unset()
  11. PHP5 Tutorial - Magic Methods - __call() method
  12. PHP5 Tutorial - Magic Methods - __autoload() method
  13. PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
  14. PHP5 Tutorial - Magic Methods - __clone() method

Your email:  
Subscribe Unsubscribe  

del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Windows Live Furl Netscape Yahoo Bloglines Bookmark.it Ask Spurl Diigo

One Response to “PHP5 Tutorial - Learn to Create a PHP5 Class Object”

  1. […] my DNA is made up of 1s and 0s « PHP5 OOPS Tutorial - Introduction to PHP5 OOPS Features PHP5 OOPS Tutorial - Learn to Creating a PHP5 Class Object […]

Leave a Reply

Top Internet blogs Programming Blogs - Blog Catalog Blog Directory TopOfBlogs Technology blogs Top Blog Topsites List