OOPS Concepts in PHP | Object Oriented Programming in PHP

PHP is a flexible platform in terms of accessing member functions and variables. OOP in PHP alludes to a programming style having an association of the class, objects, and various components.

PHP is a server-side programming language used for web development. Object-oriented programming in PHP helps developers build reusable and complex web applications. Object-oriented programming is a programming style that refers to the association of various components and revolves around the inheritance, polymorphism, encapsulation, and abstraction concepts.

OOP is programmed in such a way that the users can focus on the object while developing the program and code, but not the procedure. OOP also focuses on drawing programming close to real life. In this article, we will be exploring some of the core OOP concepts in PHP.

Major OOPS Concepts in PHP

PHP is an object-oriented programming language that supports several concepts. Some of them are as follows:

Class and objects– Class is a programmer-defined data type, including local variables and local methods. It is also a collection of objects, while objects have similar properties and behaviours. A single Data Structure instance defined by the class is an object. Class is generic and the object is specific. The developers can instantiate the object but not a class, and an object is an instance of a class.

Interfaces– Interface in PHP is a description of the actions that objects can perform. It is written in the same manner as the class is declared with every interface keyword. The methods declared in an interface are public, and they can be extended just like classes with the same extend operator.

Abstraction- Abstraction is a concept of shifting the focus from programming details and concrete implementation of things to their types and availability of the operations. Abstraction makes programming easy and general for the developer, and it is more like generalizing the specification.

Constructor– It is a special function that is called automatically whenever there is an object formation from the class.

Destructor– It is a special function that is called automatically whenever the object gets deleted or leaves the scope.

Overloading– It is a special type of polymorphism in which all or a few operators have various implementations depending upon the kind of argument. The same functions can get overloaded with multiple implementations.

Read: PHP Project Ideas & Topics

Principles and OOPS concept in PHP

The major object-oriented programming principles in PHP are as follows:

Encapsulation- This concept highlights the binding properties, methods, and hides implementation details. The prime objective of encapsulation is to limit the complications during software development, and it also simplifies using class objects. It also protects the object’s internal state and makes it easy to maintain.

Inheritance- This concept is aligned with the association among classes, and it highlights the relationship between a child class and parent class. Also, the child uses methods that are defined by the parent.

The core function of inheritance is reusability which is extremely useful when the developers have to offer basic functions like updating, adding, or deleting data components from the database. Inheritance is segmented as single level inheritance and multilevel inheritance.

Polymorphism- The term refers to using an individual instance under multiple ways of implementation. It is a concept that allows users to define a method by either changing their segments or changing how it is done. Polymorphism emphasizes on maintaining the applications along with conducting an extendable use case.

Also Read: PHP Interview Questions

Creating Objects in PHP

First and foremost, in PHP, classes are created where users can create several objects in the same class by their choice. Each object is created with the help of a new keyword. When a class is created, the developers can create as many numbers of objects as they want for the same class.

Calling Member Function

When the object is created, developers can access the method functions and variables of the class using the operator ‘->’. One member function is able to process the member variables of related objects only. Let us take an example that shows how to set the price and title for any three books by calling the member functions:

$physics->setTitle( “Physics for High School” );

$chemistry->setTitle( “Advanced Chemistry” );

$maths->setTitle( “Algebra” );

$physics->setPrice( 10 );

$chemistry->setPrice( 15 );

$maths->setPrice( 7 );

To get the value set you can call another member functions:







This will produce the following result:

Physics for High School

Advanced Chemistry





Creating abstract class


abstract class DBCommonMethods


    private $host;

    private $db;

    private $uid;

    private $password;

    public function __construct($host, $db, $uid, $password)


        $this->host = $host;

        $this->db = $db;

        $this->uid = $uid;

        $this->password = $password;





  • “Abstract class” refers to the class that cannot be used directly for creating an object.
  • “$host,$db …” are the class variables common in various implementations

Creating interface

Let us now create an interface containing standard methods to implement different database variables:


interface DBInterface


    public function db_connect();

    public function insert($data);

    public function read($where);

    public function update($where);

    public function delete($where);




  • “Interface” is a keyword for creating the interfaces
  • “Public function” is a standard method to implement

Let us create a concrete class that can extend DBCommon Methods classes and interfaces:

<?php class MySQLDriver extends 

DBCommonMethods implements DBInterface { public function __construct($host, $db, $uid, $password) 

parent::__construct($host, $db, $uid, $password); } 

public function db_connect() { //connect code goes here } 

public function delete($where) { //delete code goes here } 

public function insert($data) { //insert code goes here } 

public function read($where) { //read code goes here } 

public function update($where) { //update code goes here } 

} ?>

Function overriding

Function in child classes override within the same name as the parent class. In a child class, the developer can modify the function definition inherited from the parent class.

function getPrice() {

   echo $this->price . “<br/>”;

   return $this->price;


function getTitle(){

   echo $this->title . “<br/>”;

   return $this->title;



Each method and property in PHP has its visibility that is declared by keywords like private, public, and protected which are explained below: 

  • Public- It allows any user from outside to access the method and property.
  • Private- It does not give access to any user except itself.
  • Protected- It only allows children classes and itself to access the method and property.


The basic concept of object-oriented programming in PHP is mentioned in this article. 

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

Learn More

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses