Object and Class

What is Object

An entity that has state and behavior is known as an object eg:- as car,cycle,table,chair etc.
OR
Object is an instance of a class.
Note:- Object can be Physical or logical.
An object has three characteristics:
(i) state (ii) behavior (iii) identity
(i) state:- it is represent the data(value) of the object.
(ii) behavior:- it is represent the behavior(functionality) of the object.
(iii) identity:- it is represent object unique id.user can not see this id.
object

What is Class?

A class is the blueprint of the object. The class contains the methods and properties, or the characteristics of the object.

A class in php contain
(i) data member(variable)
(ii) method(function)
(iii) constructor
(iv) class and Interface

Syntax:-
class classname{
data-member(variable),
method(function)
}

Example of class:-
In this example we define a class employee, which has variable $empname and method name is display_empname()
and we create a object with new keyword.

class_employee


<?php 
class Employee {
	public $empname;
	function display_empname($empname) {
		return $empname;
	}

}
$emp_obj = new Employee;
echo $emp_obj -> display_empname('John');
?>
Output:- John

classification of class

Ques:- How to create a variable in the class?
Ans:- we can create a variable through public or var or protected or private.
class student{
var $stu_rollno;
public $stu_name;
protected $stu_marks;
private $stu_class;
}
Note:- var variable work as a public.

Ques:- what is the roll of Method in a class?
Ans:- It is used to expose behavior of an object.

Ques:- How to create a method in the class?
Ans:- we can create a method through public or protected or private.


<?php 
class student{
var $stu_rollno;
public $stu_name;
protected $stu_marks;
private $stu_class;
pubic function displayname($stu_name)
{
return $stu_name;
}
}
?>

Ques:- What is new keyword?
Ans:- The new keyword is used to allocate memory at runtime.

Ques:- How to create object or instance variable?
Ans:- with the help of new keyword, we create object(instance variable).


<?php 
class student{
var $stu_rollno;
public $stu_name;
protected $stu_marks;
private $stu_class;
pubic function displayname($stu_name)
{
return $stu_name;
}
}


$stu= new student;
echo $stu->displayname('Tony'); //Output is Tony
?>

Note:-$stu is a object in this program.

Ques:- How to call method of a class?
Ans:- there are many types for calling method
(i) If method is not static then
(a) call outside of the class then we use -> operator through object of the class.
eg:- $classobj->methodname;
in this eg $classobj is a object of the class.
(b) if method call inside the class then we use $this->
eg:- $this->methodname;

(ii) If method is static then
(a) call outside of the class then we use :: operator through the class.
eg:- classname::methodname;

(b) if method call inside the class then we use self::
eg:- self::methodname;

Ques:- How many create a object of a class?
Ans:- We can create multiple objects of a class.
Example:-
$stu1= new student;
$stu2= new student;

Memory allocation of object

The new keyword is used to allocate memory at runtime.
Example:-


<?php 
class Employee {
	public $empname;
	public $age;
	function empinfo($empname,$age) {
		echo $empname.' age is '.$age;
	}

}
$emp1 = new Employee;
$emp1 -> empinfo('John',22);
$emp2 = new Employee;
$emp2 -> empinfo('Tony',19);
?>
Output:- John age is 22
Tony age is 19

memory_allocation
In this figure, object gets the memory in Heap area and reference variable refers to the object allocated in the Heap memory area. Here, $emp1 and $emp2 both are reference variables that refer to the objects allocated in memory.