A class is user defined data type that contains attributes or data members; and methods which work on the data members.

To create a class, you need to use the keyword class followed by the name of the class. The name of the class should be meaningful to exist within the system.The body of the class is placed between two curly brackets within which you declare class data members/variables and class methods.

Example:
Code:

class student
{
var $name; 
function set_name($value)
{
$this->name=$value;
}
function   get_name()
{
echo $this->name;
}
}
$student1=new student();
$student1->set_name("Rahul");
$student1->get_name();

Output:

Rahul

Explanation:

Here the name of class is “student” and $student1 is instance of a class student.we make the instance of a class using “new” keyword with class name.$this is a reference to the calling object.

eXTReMe Tracker