PHP OOPS Interview Questions

OOPS (Object Oriented Programming) 

OOP is a design concept. In object oriented programming, everything will be around the objects and class. By using OOP in php you can create modular web application. By using OOP in php we can perform any activity in the object model structure. There are many benefits of using oop over the parallel or procedural programming


Any thing is the world is an object. Look around and you can find lots of objects. Your laptop, pc, car everything’s an object. In this world every object has two thing properties and behaviors. Your car has property (color, brand name) and behavior(it can go forward and backward). If you are able to find properties and behaviors of real object. Then it will be very easy for you to work with Object Oriented Programming.


The house blueprint is a class

Class is something which defines your object. For example your class is Car. And your Honda car is object of car class. 

Blueprint of the object is class. Class represents all properties and behaviors of an object. 

Class consists of properties and methods. Here we can take Human Being as a class.


Class House {

    public $postCode = “560121”;

    public function ringBell() {

    echo “Ding Dang Dong”;




Advantage of OOPS

  • Re-Usability of your code
  • Easy to Maintain
  • Good Level of Abstraction:
  • Molecularity


Abstraction is the concept of hiding the internal details and describing things in simple terms.

For example, a method that adds two integers. The internal processing of the method is hidden from the outer world.

There are many ways to achieve abstraction in object-oriented programming,

such as encapsulation and inheritance.


Encapsulation is the technique used to implement abstraction in object-oriented programming.

Encapsulation is used for access restriction to class members and methods.

Access modifier keywords are used for encapsulation in object oriented programming.

For example, encapsulation in java is achieved using private, protected and public keywords.


Inheritance is the object-oriented programming concept where an object is based on another object.

Inheritance is the mechanism of code reuse.

The object that is getting inherited is called superclass and the object that inherits the superclass is called subclass.

The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods.


class Fruit {

  public $name;

  public $color;

  public function __construct($name, $color) {

$this->name = $name;

$this->color = $color;


  public function intro() {

echo “The fruit is {$this->name} and the color is {$this->color}.”;



// Strawberry is inherited from Fruit

class Strawberry extends Fruit {

  public function message() {

echo “Am I a fruit or a berry? “;



$strawberry = new Strawberry(“Strawberry”, “red”);





Polymorphism is the concept where an object behaves differently in different situations.

There are two types of polymorphism – compile time polymorphism and runtime polymorphism.

Compile-time polymorphism is achieved by method overloading. For example, we can have a class as below.


public class Circle {

public void draw(){

System.out.println(“Drwaing circle with default color Black and diameter 1 cm.”);


public void draw(int diameter){

System.out.println(“Drwaing circle with default color Black and diameter”+diameter+” cm.”);


public void draw(int diameter, String color){

System.out.println(“Drwaing circle with color”+color+” and diameter”+diameter+” cm.”);





Association is the OOPS concept to define the relationship between objects.

The association defines the multiplicity between objects.

For example Teacher and Student objects. There is a one-to-many relationship between a teacher and students.

Similarly, a student can have a one-to-many relationship with teacher objects.

However, both student and teacher objects are independent of each other.


Interfaces are defined to provide a common function names to the implementers. Different implementers can implement those interfaces according to their requirements. You can say, interfaces are skeletons which are implemented by developers.

As of PHP5, it is possible to define an interface, like this −

interface Mail {

   public function sendMail();


Abstract Classes

An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this −

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same visibility.

abstract class MyAbstractClass {

   abstract function myAbstractFunction() {



Static Keyword

Declaring class members or methods as static makes them accessible without needing an instantiation of the class.

Final Keyword

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Overloading and Overriding in PHP

According to Object Oriented Programming (OOP) concept if a class has methods of the same name but different parameters then we say that we are overloading that method. Also if we were to create a method in the child class having the same name, same number of parameters and the same access specifier as in its parent then we can say that we are doing method overriding.

As we know that PHP is not type strict, means if we implement overloading in C++ or Java, the function will look like add(int,float) is different from add(float,int) or even add(float,int,int) but it is not possible in PHP. Actually polymorphism is not easy in PHP


Each method and property has its visibility. There are three types of visibility in PHP.

They are declared by keywords public, protected and private.

Each one of them controls how a method/property can be accessed by outsiders.

Public: It allows anyone from outside access its method/property.

This is the default visibility in PHP class when no keywords are prefixed to a method/property.

Protected: It only allows itself or children classes to access its method/property.

Private: It does not allow anyone except itself to access its method/property.

Constructor Functions

Constructor Functions are a special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.

PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

Access Modifiers

Properties and methods can have access modifiers which control where they can be accessed.

There are three access modifiers:

public – the property or method can be accessed from everywhere. This is default

protected – the property or method can be accessed within the class and by classes derived from that class

private – the property or method can ONLY be accessed within the class

//Abstract classes    

An abstract class is a class that contains at least one abstract method.

An abstract method is a method that is declared, but not implemented in the code.

An abstract class or method is defined with the abstract keyword:


abstract class ParentClass {

  abstract public function someMethod1();

  abstract public function someMethod2($name, $color);

  abstract public function someMethod3() : string;



What are Traits?

PHP only supports single inheritance: a child class can inherit only from one single parent.

Traits are used to declare methods that can be used in multiple classes.

Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

Traits are declared with the trait keyword:


trait message1 {

public function msg1() {

echo “OOP is fun! “;



class Welcome {

  use message1;


$obj = new Welcome();



//Static Methods

Static methods can be called directly – without creating an instance of a class.

Static methods are declared with the static keyword:


class ClassName {

  public static function staticMethod() {

echo “Hello World!”;




Does PHP support multiple inheritances?

PHP does not support multiple inheritances. To implement the features of multiple inheritances, the interface is used in PHP.

Here, two interfaces, Isbn and Type are declared and implemented in a class, book details to add the feature of multiple inheritance in PHP.

interface Isbn { 

public function setISBN($isbn);


interface Type{

public function setType($type); 


class bookDetails implements Isbn, Type {

private $isbn; 

private $type; 

public function setISBN($isbn)

$this -> isbn = $isbn; 


public function setType($type)

$this -> type = $type; 



Leave a Reply

Your email address will not be published. Required fields are marked *