Twitter For Webanddesigners
RSS For Webanddesigners

Introduction to PHP – Part10

Classes and Objects:

The programming paradigm we have learned so far is called Procedural Paradigm. In Procedural programming paradigm, programmer writes a program as collections of independently behaving procedures and makes use of procedure call to access those procedures. As we already discussed in Part8, how procedural programming is a better choice than a simple unstructured programming. It has been a long time, programmers have started thinking about new paradigm called Object oriented paradigm (OOP) which provides a better way to develop big and complex system than procedural programming. We don’t want to confuse readers by putting too complex terms and advantages of OOP at this stage, we will discuss when required.

In OOP, a class is a language construct that is used as a template or blue print to create instances of class called objects. This tutorial explains step by step to make the things clear. 

Creating Class

Example#1

The example below creates a class called Person. It has two instance variables; name and address and few methods. The instance variables are not accessible outside the class when they are defined as private; we need methods to access them. But public variable can be accessed outside the class. All instance variables should be kept private and there should be some methods to access those variables. Here, we have SET and GET methods which are be public. All methods should be public; otherwise they cannot be accessed outside the class.

After the end of class, we have created a instance “p” of class “Person”. “p” is called object of class “Person”. Note that, “p” object binds instance variables and methods together, that property of OOP is known as encapsulation.

<?php
class Person
{
// Properties or Instance variables
private $name;
private $address;

//Methods or behaviours
//set methods are used to set the instance variables
              public function setName($aName)
             {
	         $this->name= $aName;
    	}
	public function setAddress($aAddress)
	{
       		$this->address= $aAddress;
    	}
	//get methods are used to get the instance variables
	public function getName()
	{
      		 return $this->name;
    	}
	public function getAddress()
	{
        		return $this->address;
    	}
}// end of class

//create a object of Person class
$p = new Person();
//Set name
$p->setName("Gary Tom");
//Set address
$p->setAddress("Unit 10 Nelson Bay, NSW 2030 Australia");
//Print name
echo $p->getName();
//Print Address
echo $p->getAddress();
?>

The output of the above code will be
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia

Constructor and Destructor

When we create an object constructor method is called, hence it is useful to initialize the instance variables before they are ready to use. Similarly, destructor method is called when we destroy the objects.

Example#2

The code we have in this example produces the same output as in example#1, but has been implemented in a different way. Here we initialize the instance variables using constructors.

<?php
class Person
{
    	// Properties or Instance variables
   	 private $name;
	private $address;

	//Default Constructor
	public function __construct()
	{
       		$this->name= "Gary Tom";
	   	$this->address= "Unit 10 Nelson Bay, NSW 2030 Australia";
   	}
      	//Default Destructor
   	function __destruct()
   	{
     		//This portion of the code will be executed when
	 	// we destroy an object explicitly.
   	}

  	//get methods are used to get the instance variables
	public function getName()
	{
       		return $this->name;
    	}
	public function getAddress()
	{
        		return $this->address;
    	}
}// end of class

//create a object of Person class
$p = new Person();
//Print name
echo $p->getName();
//Print Address
echo $p->getAddress();
?>

This code will also produces the same output as in example#1, that is
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia

Inheritance

As the name suggests, inheritance is a principle by which child class can inherit the methods form its parent class and the methods performs their original functionality.

Example#3

In this example, we extend a class called Student from the class Person. The subclass Student inherits printMe methods and all instance variables from the parent class Person. The printMe method of Person class has been rewritten with the same name in Student class. If a method with same name exists in parent and child class, the child class object has two version of the same module. In this situation the child class method overrides the parent class method, it known as method overriding.  When we call printMe method of Student object, it calls printMe of Student class not the printMe of Person class. Remember that all data members of Person class are available in Student class.

<?php
class Person
{
    	// Properties or Instance variables
    	private $name;
	private $address;

   	//Function to print Instance variables
   	public function printMe()
   	{
   		echo $this->name.", ".$this->address;
   	}
}// end of Person class

//Student class, inherits from Person class
class Student extends Person
{
	//Instance variables, remember that name and address is inherited from Person
	private $program;

	//Default Constructor
	public function __construct()
	{
       		$this->name= "Gary Tom";
	   	$this->address= "Unit 10 Nelson Bay NSW 2030 Australia";
	   	$this->program= "Masters in IT";
   	}

   	public function printMe()
   	{
   		echo $this->name.", ".$this->address.", ".$this->program;
   	}
}// end of Student class

//create a object of Person class
$s = new Student();
//Print s
$s->printMe();
?>

The output of the above code will be
Gary Tom, Unit 10 Nelson Bay NSW 2030 Australia, Masters in IT

This tutorial tries to describe Classes and Objects in short, for a complete reference please visit http://www.php.net/manual/en/language.oop5.php




Related Posts


bookmark bookmark bookmark bookmark Subscribe to RSS


4 Comments »

  1. Steve says:

    great tutorial, waiting for the remaining parts….

  2. Jehnee says:

    very clear explanation and example thanks.

  3. themeheven says:

    That’s good. Nice Tutotial Keep it up.

  4. JohnDi says:

    Good examples. Every little bit helps. Keep up the effort. Thanks.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Spam Protection by WP-SpamFree