Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is OOP in PHP?
2.1.
Encapsulation 
2.2.
Polymorphism
2.3.
Abstraction 
2.4.
Inheritance 
3.
Benefits of OOP in PHP
4.
Classes and Objects in OOP
4.1.
Syntax
5.
Frequently Asked Question
6.
Key takeaway
Last Updated: Mar 27, 2024

Introduction to OOP in PHP

Author Aditya kumar
0 upvote

Introduction

Object-oriented programming is followed by many languages, including C++Javapython. This article will first give you a basic understanding of OOP in PHP, then discuss classes and objects in detail. We will also see a few illustrations implementing OOP in PHP.

What is OOP in PHP?

The object-oriented programming is about wrapping data and related functions in classes. While in procedural programming, data is open and accessible to all, and we write functions to operate on them. We cannot add more properties with ease, making it a heavy task for the developer. Languages like C, Fortran, BASIC follow such approaches.

OOP follows four principles: Encapsulation, Polymorphism, Data abstraction and inheritance.

Encapsulation 

It refers to the bundling of data and functions operating on the same data. This keeps a separation between all of the different types of data present in the program. Data hiding can be done while encapsulating code with the help of public, private keywords.

For example

class User{
private $id;
private $pwd;
public function getId(){
return $id;
}
}
You can also try this code with Online PHP Compiler
Run Code

Here we created private fields which are not directly accessible but only with the public function getId it is accessible. Password cannot be accessed by anyone.

Polymorphism

It simply means being able to exist and function in multiple forms. In PHP, interfaces are used to implement polymorphism. Common functionality is defined in the interface and implemented in multiple classes. In the example below, calcArea can be used by both circle and rectangle.

For example

interface common{
public function calcArea();
}
class Circle implements common {
$radius;
public function __construct($radius){
        $this->radius=$radius;
    }
    public function calcArea(){
    $area=$this->radius*2*3.14;
    return $area;
    }
}
You can also try this code with Online PHP Compiler
Run Code

Abstraction 

This concept allows for hiding the implementation details of the class and displays only the functionality. Abstraction can be achieved by using abstract classes, and interfaces. For example, we can declare an abstract class named ‘Car’ which can be extended by various car companies like Audi, Mercedes implementing the same common functionality as mentioned in the abstract class.

Inheritance 

Classes can use certain common attributes from another class. For example, a student, a teacher comes under a common class of users, and so both need to inherit userId from their parent “staff”. This concept of inheriting common fields from one common class is termed as inheritance.

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

Classes Objects
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...
}
You can also try this code with Online PHP Compiler
Run 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;
}
}
You can also try this code with Online PHP Compiler
Run Code

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

Frequently Asked Question

What is $this in PHP?

Ans: ‘this’ keyword refers to the current object instance. It can only be called inside the methods to refer to the properties associated or declared inside that class. 

For example

<?php
class Car{
public $manufacturer;
function set_manufacturer_name($name){
$this->manufacturer=name;
}
}
?>
You can also try this code with Online PHP Compiler
Run Code

 

What is ‘instanceOf’ and how is it used in OOP?

Ans: ‘instanceOf’ keyword is used to check if an object is an instance of a particular class or simply if the object is created using a particular class.

For example

<?php
class Fruit{};
class Vegetable{};
$mango = new Fruit();
echo ($apple instanceOf Fruit)
echo ($apple instanceOf Vegetable) 
?>
You can also try this code with Online PHP Compiler
Run Code

 

Output

1 // truthy 

0 //falsy

Key takeaway

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:

Features of Object Oriented Programming

Difference Between Procedural and Object Oriented Programming

Characteristics of OOPS

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

Happy Learning!

Live masterclass