PHP (OOP) Object Oriented Programming - Interview Questions and Answers

MCQ like Questions

These one line answer questions on PHP oop (object oriented programming) are like multiple choice questions (MCQ), meaning they will help you in solving any MCQ.

What is object oriented programming (OOP)?

Answer: Object-oriented programming is a programming based on the concept of "objects", which can contain data, in the form of fields, and code, in the form of procedures.

How a class is declared in PHP?

Answer: A class is declared using the keyword "class" followed by the name of the class and code in curly braces.

Example:
class MyClass
{
    // data fields
    // code for methods
}

What is the relationship between class and object?

Answer: A class is structure or blueprint for creating objects. An object is an instance of a class which occupies some memory.

What is serialization?

Answer: The process of converting an object into byte stream representation that can be stored into a file is called a serialization. [This is useful for persistent data. This can be achieved by serialize() and unserialize() functions.]

How to create an object in PHP?

Answer: An object is an instance of a class. to create an instance of a class we use new keyword.
Example:
<?php
class MyClass
{
   public $name;

   function hello() 
   {
     echo "Hello $this->name";
   }
}

$instance1 = new MyClass();

// We can also perform this as 
$name = "Hello";
$instance2 = new $name();

What is an interface?

Answer: An interface is a pure abstract class which contains only method declarations but not definitions and can also contain constants.

Do private members (variables) get inherited we inherit a class?

Answer: No. A subclass does not inherit the private members of its parent class.

What is the use of $this variable?

Answer: $this is a pseudo variable which is used inside a class, usually within member functions to access the non-static members (variables and functions) of the current object). It is also used to return the implicit object.

Which function is used to check if a class exists or not?

Answer:class_exists() function is used to check if a class exists or not. It takes a single class name string argument and returns a boolean value.

"A function can have variable number of arguments." State true or false.

Answer: True. [To specify varibale arguments to function leave the argument block empty, for example function hello().]

What is introspection?

Answer: Introspection is the ability of a program to examine object's characteristics such as name parent class (if any), properties and methods.

Write any four functions for introspection.

Answer:Introspection functions are:
  1. get_class()
  2. get_object_vars()
  3. is_object()
  4. get_parent_class()

Which functions are used to notify objects that they are being serialised or unserialised?

Answer: _sleep() and _wakeup() functions are used to notify objects that they are being serialised and unserialised respectively.

Which operator is used to access properties and methods of object?

Answer: -> operator is used to access properties and methods of an object.

How would you declare an empty class called php_oop which has no methods and properties?

Answer:
<?php
class php_oop
{

}
?>

Given a class MyClass(), how will you create an object that is an instance of it?

Answer:We can create an instance of an object using new keyword.
$instance1 = new MyClass();

5 Marks Brief Questions Previous Exam

Write a short note on introspection.

Answer:1) Introspection is the ability of a program to examine an object's statistics, such as its name, parent class (if any), properties and methods. [With introspection, we can write code that operates on any class object. we need not to know which methods and properties are defined when we write the code; instead, it can discover that information at runtime, which makes it possible for us to write generic debuggers, serializers, profilers etc.]
The introspective functions provided by PHP are as follows:

2) class_exists() takes class name as string and returns a boolean value depending on whether that class exists or not.
Example:
$existence_status = class_exists(classname);

3) get_declared_classes() function returns an array classes defined, using this array we can check if a particular class exists or not.
Example:
$classes = get_declared_classes();

4) get_class_methods() function returns an array of method names defined in class and also which are inherited from superclass.
Example:
$methodNames = get_class_methods(classname);

5) get_class_vars() function is similar to get_class_methods() but it returns an associative array in which the keys are the property names in a class and values are their respective values of each property.
Example:
$properties = get_class_vars(classname);

6) get_parent_class() function returns the parent class name of class passed as arguments.
Example:
$superclassName = get_parent_class(class and);

7) get_object_methods(), get_object_vars() functions are similar to get_class_methods() and get_class_vars() but they require an object as parameter and return the respective methods and properties of an object.

8) is_object() function is used to check if the parameter passed is an object or not, it returns a boolean value.
Example:
$object_status = is_object(obj);

9) get_class() function is used to get class corresponding to an object passed as arguments.

What is an abstract class? What are the features of an abstract class?

Answer:1) An abstract class is an incomplete class which cannot be instantiated. It includes properties, method declaration and definition constructors and some other method declarations leaving definition to subclasses.

2) Features of abstract class are:
  1. An abstract class cannot be instantiated
  2. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
  3. An abstract class may contain abstract methods and accessors.
  4. It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings. The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

Write a note on interface.

Answer:1) An interface is like a pure abstract class which contains only method declarations and can also contain constants as well. Basically an interface is a set which provides the behaviour or operations of a class.

2) Syntax for an interface definition:
interface interfacename  superinterfacename
{
   function functionname1();
   function functionname2();
          ....
   function functionnameN();

   const constantName1 = value;
   const constantName2 = value;
          ....
   const constantNameN = value;
}

3) To implement an interface for a class, implements keyword is used followed by any number of interface names. When a class implements an interface, a class must define all the methods of the interface otherwise PHP engine will give you an error.

4) Example:
interface shape
{
   function area();
}

class square implements shape
{
   float side;

   function area()
   {
      return side*side;
   }
}

5) PHP does not support multiple inheritance for classes. However it does for interfaces. An interface may inherit from other interfaces as long as none of the interface it inherits from declare methods with the same name as those declared in the child interface. Using this concept we can also implement multiple inheritance for classes.

Write a program to check properties of a class and print.

Answer: See PHP Introspection Functions Program