Class and Object in PHP: Understanding the Basics

Class and Object in PHP: Understanding the Basics

Understanding Classes and Objects in PHP

What are Classes and Objects in PHP?

A class is a blueprint for creating objects in Object-Oriented Programming (OOP). It defines the structure and behavior of an object by specifying its methods and properties. A class allows the creation of multiple instances (objects) with the same structure but different data.

In OOP, the main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction. Classes and objects are essential for understanding these concepts, as they allow us to group related properties and methods.

In practice, a class does not hold data on its own. It acts as a definition. The objects created from a class hold actual data and interact with each other in the program.

Example: Employee Management System

Let’s see an example in PHP to understand how classes and objects work. Consider an Employee Management System where we manage employee details:

In my explanation, I have added comments for your better understanding.

<?php // Defining the Employee class. // A class is like a blueprint for creating objects. class Employee { // Properties to store employee's details. // These are variables defined inside the class. public $name; // Property for employee's name public $designation; // Property for job title public $salary; // Property for salary // Method to set employee details. // The method defines the actions that the object can perform. public function setDetails($name, $designation, $salary) { // $this refers to the current object. // It allows access to the object's properties and methods. // $this->name ensures that you're accessing or modifying the name property of the specific object. // Without $this, you wouldn't be able to distinguish between properties of different objects. // In simple terms, if the Object is a house, $this is the house you're standing in right now. $this->name = $name; $this->designation = $designation; $this->salary = $salary; } // Method to display employee details. //This method is used to show the values of the properties of the object. public function getDetails() { // Displaying employee's details using echo echo "Name: {$this->name}<br>"; // Display name of employee echo "Designation: {$this->designation}<br>"; // Display designation/job title echo "Salary: {$this->salary}<br>"; // Display salary of employee } } // Creating an employee object. // The "new" keyword is used to create an instance (object) of the class. $employee1 = new Employee(); $employee1->setDetails("John Doe", "Software Engineer", 50000); // Creating another employee object $employee2 = new Employee(); $employee2->setDetails("Jane Smith", "Project Manager", 75000); // Displaying the details of employee 1 echo "Employee 1 Details:<br>"; $employee1->getDetails(); // Displaying the details of employee 2 echo "Employee 2 Details:<br>"; $employee2->getDetails(); ?>

Output:

Employee 1 Details:
Name: John Doe
Designation: Software Engineer
Salary: 50000

Employee 2 Details:
Name: Jane Smith
Designation: Project Manager
Salary: 75000

In the above example, we define a class called "Employee" with properties like name, designation, and salary. The class has methods setDetails to assign values to these properties and getDetails to display them.

Two objects of the "Employee" class are created: $employee1 and $employee2. We use the setDetails method to set different details for each employee (John and Jane), and the getDetails method to display their respective details.

This demonstrates how a single class can be used to create multiple objects with their own data, showcasing the key OOP concept of using classes to manage related data.

Comments

Popular posts from this blog

Importance of Object-Oriented Programming (OOP) in Web Development

AI Tools Every Web Developer Should Use in 2025