Introduction
In Object-Oriented Programming (OOP), the most important concepts are Class and Object. PHP supports object-oriented programming, which helps developers write structured, reusable, and maintainable code.
A class can be defined as a blueprint or template used to create objects. It contains the properties (variables) and methods (functions) that define the behavior of an object.
An object is an instance of a class. It represents a real-world entity that has state, behavior, and identity.
- Class β Blueprint
- Object β Instance created from the class
Example:
- Car
- Bike
- Employee
- Student
All these are real-world objects.
An object has three main characteristics:
1. State
State represents the data or values of an object.
Example: color of a car, name of an employee, age of a student.
2. Behavior
Behavior represents the actions or functionality of an object.
Example: a car can start, stop, or accelerate.
3. Identity
Identity represents the unique identification of an object in memory. Each object created in PHP has its own unique reference.
Classes and objects are the foundation of PHP OOP concepts.
Syntax
The basic syntax for creating a class in PHP is shown below.
<?php
class ClassName {
// properties (variables)
// methods (functions)
}
?>
To create an object of a class, we use the new keyword.
<?php
$objectName = new ClassName();
?>
Explanation:
- class β keyword used to declare a class
- ClassName β name of the class
- properties β variables inside the class
- methods β functions inside the class
- new keyword β used to create an object
Example:
Letβs look at a simple example of a class and object in PHP.
<?php
class Employee {
public $empname;
function display_empname($empname) {
return $empname;
}
}
$emp_obj = new Employee();
echo $emp_obj->display_empname("John");
?>
Output:
In this example:
- Employee is the class.
- $empname is a property.
- display_empname() is a method
- $emp_obj is the object created using the new keyword.
Real-world Example
Letβs understand a practical example using a Student class.
Suppose a school system needs to manage student information like name and roll number.
<?php
class Student {
public $rollno;
public $name;
function studentInfo($rollno, $name) {
echo "Roll No: " . $rollno . "
";
echo "Name: " . $name . "
";
}
}
$stu1 = new Student();
$stu1->studentInfo(101, "Rahul");
echo "
";
$stu2 = new Student();
$stu2->studentInfo(102, "Amit");
?>
In this example:
- We created a Student class.
- The class contains properties and a method.
- Two objects (stu1 and stu2) are created.
- Each object stores different data.
This demonstrates how classes and objects work in real-world applications
Output
The output of the above program will be:
Name: Rahul
Roll No: 102
Name: Amit
This shows that multiple objects can be created from the same class, each containing different values.
Explanation
Letβs understand how the code works step by step.
First, we create a class called Student.
class Student
Inside the class, we declare two properties:
public $rollno;
public $name;
Properties store the data related to the object.
Next, we define a method called studentInfo().
function studentInfo($rollno, $name)
This method accepts two parameters and displays student details.
After defining the class, we create an object using the new keyword.
$stu1 = new Student();
Here:
- $stu1 is an object.
- It refers to the Student class
Then we call the method using the arrow operator (->).
$stu1->studentInfo(101, "Rahul");
The -> operator is used to access properties and methods of an object.
Similarly, we create another object:
$stu2 = new Student();
Both objects are independent and store different values.
Memory Allocation
When an object is created using the new keyword, PHP allocates memory in the heap area.
Example:
$emp1 = new Employee;
$emp2 = new Employee;
Here:
- $emp1 and $emp2 are reference variables.
- Each object gets its own memory space.
Object and Class β Interview Questions
Q 1: What is a class in PHP?
Q 2: What is an object?
Q 3: How to create an object?
Q 4: Can a class contain methods?
Q 5: Why use OOP in PHP?
Conclusion
Classes and objects are the foundation of Object-Oriented Programming in PHP. A class acts as a blueprint that defines properties and methods, while an object is an instance of that class.
Using classes and objects helps developers organize code efficiently, improve code reusability, and build scalable applications. Multiple objects can be created from a single class, and each object can hold different data.
Understanding how to create classes, objects, and methods is the first step toward learning advanced PHP OOP concepts such as inheritance, polymorphism, abstraction, and encapsulation.
Mastering these concepts will help developers build powerful and maintainable web applications using PHP.
Object and Class β Objective Questions (MCQs)
Q1. A class in PHP is used to ______.
Q2. Which keyword is used to create an object in PHP?
Q3. Objects in PHP can contain ______.
Q4. Which syntax is correct to declare a class?
Q5. Objects in PHP are instances of ______.