PHP Object and Class

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.

πŸ“–
Important:
  • 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:

John

In this example:

  1. Employee is the class.
  2. $empname is a property.
  3. display_empname() is a method
  4. $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:

Roll No: 101
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:

  1. $emp1 and $emp2 are reference variables.
  2. Each object gets its own memory space.

Object and Class – Interview Questions

Q 1: What is a class in PHP?
Ans: A class is a blueprint for creating objects.
Q 2: What is an object?
Ans: An instance of a class.
Q 3: How to create an object?
Ans: Using the new keyword.
Q 4: Can a class contain methods?
Ans: Yes.
Q 5: Why use OOP in PHP?
Ans: For better code organization and reusability.

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 ______.






Related Object and Class Topics