Table of contents
1.
Introduction
2.
What are OOP concepts in PHP?
3.
Benefits of OOP in PHP
4.
Classes and Objects in OOP
4.1.
Syntax
4.2.
PHP
5.
Example of a Class and Object
5.1.
PHP
6.
Frequently Asked Question
6.1.
What are the 4 concepts of OOP?
6.2.
How to create an OOP in PHP?
6.3.
Can PHP use OOP?
7.
Conclusion
Last Updated: Nov 5, 2024

Object-Oriented Programming (OOP) in PHP

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Object-Oriented Programming (OOP) is a powerful programming paradigm that organizes code into reusable, modular structures called "objects," each representing a specific aspect of the application. In PHP, adopting OOP principles makes your code more organized, scalable, and easier to maintain, especially for complex projects. Whether you’re building a web application, API, or complex system, understanding and using OOP in PHP will help you structure your code logically and enable you to take advantage of features like inheritance, encapsulation, and polymorphism.

Object-Oriented Programming (OOP) in PHP

What are OOP concepts in PHP?

Object-Oriented Programming (OOP) in PHP is built around four key concepts:

  1. Encapsulation: Bundles data and methods into a single unit (class) and restricts access to some components, enhancing data protection.
  2. Inheritance: Allows a class to inherit properties and methods from another class, promoting code reuse and creating a hierarchical relationship.
  3. Polymorphism: Enables methods to perform differently based on the object that is calling them, allowing for dynamic method resolution.
  4. Abstraction: Hides complex implementation details and exposes only the essential features, simplifying interaction with objects.

These principles foster code organization, scalability, and maintainability in PHP applications.

Benefits of OOP in PHP

The benefits of using OOP in PHP are discussed below

  • Allows us to write the proper code in a structured way.
  • Code once written can be reused at multiple places.
  • Data that remains hidden cannot be accessed by anyone unless made public.
  • Visualizing in the real world is easy.
  • Helps to keep the PHP code DRY(Don't Repeat Yourself), and also makes the code easier to maintain, debug and modify

Classes and Objects in OOP

Wrapper that encapsulates data and related functions is termed as a class. For example, a student class would include roll number and name data; a teacher class would include name, id, and subjects. Simply put, class is the blueprint of Objects.

Now take, for example, “John” is a Student, and so data and functions operating on him will come from the Student class. “John” is the Object here, one instance of Student class and will hold property values as described in the class definition.

The below diagram illustrates classes and objects

ClassesObjects
City

Jaisalmer

Jodhpur

Patna

Country

India

Nepal

USA

Syntax

To define a class in PHP, use the “class” tag with the name of the class. Refer below for example

class City{
// code...
}

Now we will define the blueprint with data and required functions as below

class City{
//properties of the class
public $state;

//methods that operate on property
function set_state_name($state){
$this->state=$state;
}
function get_state_name($state){
return $this->state;
}
}

Creating an object out of a class is done with the help of the “new” keyword. Each object instance will have its property values. Refer below to create an instance of the city named “Jaisalmer”.

$jaisalmer=new City();

More Example

  • PHP

PHP

<?php
class Car {
public $manufacturer;
public $color;

function set_manufactuer($name) {
  $this->manufacturer = $name;
}
function get_manufacturer() {
  return $this->manufacturer;
}
}
$tigor=new Car();
$tigor->set_manufactuer('Tigor');
echo $tigor->get_manufacturer();
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Tigor

 

Know What is Object in OOPs here in detail.

Must Read PHP Projects With Source Code

Example of a Class and Object

Let's consider a simple example involving a class called Car.

  • PHP

PHP

<?php
class Car {
// Properties
public $make;
public $model;
public $color;

// Constructor
public function __construct($make, $model, $color) {
$this->make = $make;
$this->model = $model;
$this->color = $color;
}

// Method to display car details
public function displayInfo() {
return "Car: {$this->color} {$this->make} {$this->model}";
}
}

// Creating an object of the Car class
$myCar = new Car("Toyota", "Corolla", "blue");

// Accessing properties and methods
echo $myCar->displayInfo();
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

Car: blue Toyota Corolla

Explanation

  • Class Definition: The Car class is defined with three properties: make, model, and color. These properties will hold the specific details of a car.
  • Constructor: The __construct method is a special method that initializes the properties when a new object is created. It assigns the values passed as arguments to the object's properties using $this, which refers to the current object.
  • Method: The displayInfo method is defined to return a string containing details about the car. It accesses the object's properties using $this->propertyName.
  • Creating an Object: The line $myCar = new Car("Toyota", "Corolla", "blue"); creates a new object called $myCar from the Car class, initializing it with specific values for make, model, and color.
  • Accessing Properties and Methods: Finally, echo $myCar->displayInfo(); calls the displayInfo method on the $myCar object, which outputs the car's details.

Frequently Asked Question

What are the 4 concepts of OOP?

The four concepts of OOP are encapsulation, inheritance, polymorphism, and abstraction, which promote modular, reusable, and organized code.

How to create an OOP in PHP?

To create OOP in PHP, define classes using the class keyword, create objects with the new keyword, and use methods and properties.

Can PHP use OOP?

Yes, PHP fully supports OOP, allowing developers to implement classes, objects, and all core OOP principles for better code organization and reusability.

Conclusion

Congratulations on getting through this article. We got introduced to object oriented programming i.e., OOP in PHP. We understood how it is different from procedural programming. We saw how to define classes and instance objects with illustrations and examples. 

Recommended Readings: 

Recommended Readings:

You can take a look at our PHP archives section and see many more interesting topics related to it.

Live masterclass