PHP Classes and Objects : Tutorial


The real world is full of objects like the trees, birds, humans , buildings, computers , mobile phones etc.

In Object Oriented Programming concepts we consider everyting as Objects.

PHP Classes

A class is noting but a Blue-print of an Object.

We can create Objects by instanciating a Class.

A class is made up of two things,


 1. Member Variables
 
    - Member Variables are the variables that are defined inside a class.
    - These variables are invisible to the outside of the class.
    - We can access Member variables via member functions.
    
     
 2. Member Functions
 
    - Member Functions are the functions that are defined inside a class.
    - We can access member functions by creating object of the class.


Class Member variables are called as properties, attributes or fields.
Class Member variables can be defined as public, protected, or private.
var is treated as public in PHP 5 onwards
Syntax :
class className{

	// Member variables
	
	public/private/protected/var  $memberVariable1;
	public/private/protected/var  $memberVariable2;
	....
	public/private/protected/var  $memberVariableN;
	
	
	
	// Member functions
	
	function memberFunction1()
	{
	
	}
	...
	...
	
	function memberFunctionN()
	{
	
	}
	
}


Creating an instance of a Class.

We can create an instance of a class using new keyword.

Note : A Class should be defined before instantiating it.

Syntax :
$instance = new ClassName();


Example 1 :
In the below example, we have a class "MyDetails" having 3 member variables, $name, $age and $message and one member function showMyDetails() which takes in 3 parameters.

<?php

class MyDetails {
 	
 	var $name;
 	var $age;
 	var $message;
 	
 	//member function with 3 arguments
 	function showMyDetails($a,$b,$c)
 	{
 		$name    = $a;
 		$age     = $b;
 		$message = $c;
 		
 		echo "Name : $name <br>";
 		echo "Age : $age <br>";
 		echo "Message : $message <br><br>";
 	}
 	
 }

 //creating object of class MyDetails
 $myDetails = new MyDetails;
 
 
 //calling member showMyDetails functions of class MyDetails
 $myDetails->showMyDetails("Mike","19","I love PHP!");
 $myDetails->showMyDetails("Thomas","24","I love my iPhone!");

?>
Output

Name : Mike Age : 19 Message : I love PHP! Name : Thomas Age : 24 Message : I love my iPhone!


Getter and Setter functions

We can set a member variable value by using setter functions, $this is a special variable, it refers to the same object.

Syntax Setter function :
function setVariableName($variableName) {
		$this->variableName = $variableName;
	}


We can get a member variable value by using getter functions,

Syntax Setter function :
function getVariableName() {
		echo $this->variableName;
	}



Example 2 : Getter and Setter function
<?php

class MyDetails {

	var $name;
	var $age;
	var $message;


	function getName() {
		echo "Name : ".$this->name ."<br/>";
	}

	function setName($name) {
		$this->name = $name;
	}


	function getAge() {
		echo "Age : ".$this->age ."<br/>";
	}

	function setAge($age) {
		$this->age = $age;
	}



	function setMessage($message) {
		$this->message = $message;
	}
	
	function getMessage() {
		echo "Message : ".$this->message ."<br/>";
	}

}

$myDetails = new MyDetails;

//Calling Setter Functions
$myDetails->setMessage("I love PHP!");
$myDetails->setAge("19");
$myDetails->setName("Mike");

//Calling Getter Functions
$myDetails->getName();
$myDetails->getAge();
$myDetails->getMessage();


?>
Output

Name : Mike Age : 19 Message : I love PHP!