OOPS based interview question in PHP

1. What is Object Oriented Programming?

OOPS, a well-known and highly adopted programming language model which revolves around objects, which can have the data and functions. OOPs allows developer to maintain the code easily as it allows to keep similar type of functionality into classes, thus increase code reusability and add an extra layer of security, bind up functionality into a several smaller unit.

2. What are the principle of OOPS? Explain them?

The most frequently and a very popular interview questions for developer, OOPs majorly have 3-4 principles:-
Wrapping up of data members and methods/function into a single unit i.e., in a class is known as encapsulation. It means hiding internal details of an object by using access specifier, thus adding a level of restriction to data members and function so that others objects cannot access them.

It hides the unnecessary data and show only the relevant information. We create abstract class and function in PHP to achieve abstraction.

It means a same name function can have many forms or can behave differently in different situation. It is of two type; run time polymorphism i.e, overriding and compile tile polymorphism i.e, overloading. PHP does not have overloading similar to other OOPS languages. It achieve overloading by magic methods.

It provides code reusability, by allowing the child class to inherit the functions from parent class rather than writing it again and again. In PHP we extends classes by using extends keywords.

3. What is the purpose of a constructor?
A constructor is a magic method in PHP which is called automatically during initialization of an object. Constructors are used to assign default value or to perform any action which we want to do on a creation of an object. In PHP constructor is created in following way:-

function __construct(){}

Note: – Unlike other languages constructor in PHP are not created with the class name.

4. What is difference between abstract and interface?
Abstract class may or may not have abstract method, it can also have concrete method but in case of interface all methods are defined as abstract no concrete method is allowed. Interface is used to achieve multiple level inheritance.

5. What is Polymorphism? Does PHP support overloading?
Review answer of question 2 for this.

6. What is difference between abstraction and inheritance?
Both abstraction and encapsulation hides the information but in case of encapsulation it solves problem at implementation level, thus hiding the internal details of object i.e, how object does something, how its method and data member are interacting While abstraction solves problem at design level, it hides only the unwanted data and gives all relevant data such as outer look of a car, allowing you to use steering but did not know how it’s actually working.

7. What are magic methods?
Magic methods are special method in php that start with double underscore and convey special meaning to compiler, they are never called explicility. They are called implicility whenever needed. Such as __construct is always called automatically when an object of class is created.

8. How to call parent constructor in php?
parent::__construct() is used to call parent class constructor.

9. What are access modifiers?
Public, protected and private are the keywords used as access modifiers to set the visibility for functions and data members. Public members are accessible everywhere, protected are accessible only within the class itself or by inheriting class.

10. What is Scope Resolution Operator?
This operator refer to the scope of some blocks, and is used to access properties and method of classes. Mainly we use it to access static variables and method.

11. What is Static Keyword in PHP?
We make class properties and methods as static to access them without instantiating the class. They cannot be accessed with an instantiated class objects. Static keyword is also used for late static binding.

12. What is late static binding?
Late static binding is used to refer a class that was used at runtime, we use static keyword for this. Let’s understand it with an example:-

    class Car
        public static function run()
            return static::getName();

        private static function getName()
            return 'Car';

    class Toyota extends Car
        public static function getName()
            return 'Toyota';

    echo Car::run(); // Output: Car, Here we are calling run function by referring car class. And run function calls getName with static keyword that means class car run function will be used.
    echo Toyota::run(); // Output: Toyota, Here we are calling run function by referring toyota class. And run function calls getName with static keyword that means class toyota run function will be used.

Thus in above example you can see that late static binding works by storing a class name in the last “non-forwarding call”.

13. What are Traits in PHP?
PHP only supports single inheritance i.e, a class can only inherit one other class. However, sometimes we need to inherit multiple classes.
Therefore PHP 5.4 introduces Traits, that reduce some limitations of single inheritance by allowing a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Traits cannot be instantiated.
Below is the code explaining traits implementation:-

class Base {
    public function sayHello() {
        echo 'Hello ';

trait SayWorld {
    public function sayHello() {
        echo 'World!';

class MyHelloWorld extends Base {
    use SayWorld;

$o = new MyHelloWorld();

The above example will output:- Hello World!

14. What is Object Iteration?
It is possible to iterate through an object in PHP 5 through a list of items, for example by using a foreach statement. By default, all visible properties will be used for the iteration.

15. What is Final Keyword in PHP?
We use final keyword to prevents child classes from overriding a method or to prevent class itself from overriding.
For example,

class ABC{
   final function xyz(){}
xyz is set final which means it cannot we overridden in any other class.

16. Can we make a constructor as private?
Yes, we can, it is generally a common practice for Singleton Pattern. Making constructor as private ensures that there can be only one instance of a Class and provides a global access point to that instance.

17. What is serialization?
PHP provides an option for object serialization. We use serialize() function that returns a string containing a byte-stream representation of any value that can be stored in PHP and unserialize() function for vice-versa. For example:-

Suppose I have code:-
$serialized_data = serialize(array('Math', 'Language', 'Science'));
echo  $serialized_data;

Then the output of above code is a:3:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";}

Leave a Comment.