PHP OOPs Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm that uses objects, which are instances of classes, to organize and structure code.
Ques 2. Explain the four pillars of OOP.
The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction.
Ques 3. What is encapsulation?
Encapsulation is the bundling of data and methods that operate on the data into a single unit, i.e., a class.
Ques 4. How does inheritance work in PHP?
Inheritance allows a class to inherit properties and methods from another class. It promotes code reusability.
Ques 5. What is polymorphism?
Polymorphism allows objects of different classes to be treated as objects of a common base class. It includes method overloading and overriding.
Ques 6. Explain the concept of abstraction.
Abstraction involves hiding the complex implementation details and showing only the necessary features of an object.
Ques 7. What is a class in PHP?
A class is a blueprint for creating objects. It defines properties (attributes) and methods that the objects will have.
Ques 8. How do you instantiate an object in PHP?
You can instantiate an object using the 'new' keyword followed by the class name. For example: $object = new MyClass();
Intermediate / 1 to 5 years experienced level questions & answers
Ques 9. What is a constructor?
A constructor is a special method in a class that is automatically called when an object is created. It is used for initializing object properties.
Example:
class MyClass {
public function __construct() {
echo 'Constructor called';
}
}
Ques 10. Explain the 'static' keyword in PHP.
The 'static' keyword is used to declare static methods and properties. Static methods can be called without creating an instance of the class.
Example:
class MathUtility {
public static function add($a, $b) {
return $a + $b;
}
}
Ques 11. What is method overloading?
Method overloading is the ability to define multiple methods in the same class with the same name but different parameters.
Example:
class Calculator {
public function add($a, $b) {
return $a + $b;
} public function add($a, $b, $c) {
return $a + $b + $c;
}
}
Ques 12. How is method overriding achieved in PHP?
Method overriding occurs when a child class provides a specific implementation for a method that is already defined in its parent class.
Example:
class Animal {
public function makeSound() {
echo 'Generic Animal Sound';
}
}
class Dog extends Animal {
public function makeSound() {
echo 'Bark';
}
}
Ques 13. What is an interface in PHP?
An interface is a collection of abstract methods. Classes that implement an interface must provide concrete implementations for all its methods.
Example:
interface Logger {
public function logMessage($message);
}
class FileLogger implements Logger {
public function logMessage($message) { // Implementation of logMessage method }
}
Ques 14. What is composition in OOP?
Composition is a way of creating complex objects by combining simpler objects or classes. It involves creating relationships between objects rather than relying on inheritance.
Example:
class Engine {
/* Engine properties and methods */
}
class Car {
private $engine;
public function __construct(Engine $engine) {
$this->engine = $engine;
}
}
Ques 15. Explain the difference between private, protected, and public visibility in PHP.
Private members are only accessible within the class, protected members are accessible within the class and its subclasses, and public members are accessible from outside the class.
Example:
class Example {
private $privateVar;
protected $protectedVar;
public $publicVar;
}
Ques 16. What is method chaining in PHP?
Method chaining is a technique where multiple methods can be called on the same object in a single line. It improves code readability and conciseness.
Example:
class Calculator {
private $result;
public function add($a, $b) {
$this->result = $a + $b;
return $this;
}
public function multiply($a) {
$this->result *= $a;
return $this;
}
}
$total = (new Calculator())->add(3, 5)->multiply(2)->getResult();
Ques 17. What is the purpose of the 'namespace' keyword in PHP?
Namespaces are used to avoid naming conflicts between classes, functions, and constants. They provide a way to organize code into logical and hierarchical structures.
Example:
namespace MyNamespace;
class MyClass { /* class definition */ }
Experienced / Expert level questions & answers
Ques 18. What is the 'final' keyword in PHP?
The 'final' keyword is used to prevent a class or method from being extended or overridden by other classes.
Example:
final class FinalClass {
/* class definition */
}
final function finalMethod() {
/* method definition */
}
Ques 19. Explain the concept of abstract classes.
Abstract classes cannot be instantiated and may contain abstract methods, which are methods without a body. Subclasses must provide implementations for all abstract methods.
Example:
abstract class Shape {
abstract public function calculateArea();
}
class Circle extends Shape {
public function calculateArea()
{
// Implementation for Circle's area calculation
}
}
Ques 20. What is a trait in PHP?
A trait is similar to a class but intended to group functionality in a fine-grained and consistent way. Traits are used to declare methods in a class.
Example:
trait Loggable {
public function log($message)
{
echo $message;
}
}
class User {
use Loggable;
}
Ques 21. What is the difference between an abstract class and an interface?
An abstract class can have both abstract and non-abstract methods, and it can have properties. Interfaces can only have abstract methods and constants.
Ques 22. Explain late static binding in PHP.
Late static binding allows static methods to reference the called class using the 'static' keyword. It helps resolve the class at runtime rather than at compile time.
Example:
class ParentClass {
public static function whoAmI() {
echo static::class;
}
}
class ChildClass extends ParentClass { }
ChildClass::whoAmI(); // Outputs 'ChildClass'
Ques 23. What is the use of the 'self' keyword in PHP?
The 'self' keyword is used to refer to the current class. It is used to access static properties and methods within the class itself.
Example:
class Example {
private static $counter = 0;
public static function incrementCounter() {
self::$counter++;
}
}
Ques 24. Explain the Singleton design pattern in PHP.
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It involves a private constructor and a static method to get the instance.
Example:
class Singleton {
private static $instance;
private function __construct() { /* private constructor */ } public static function getInstance() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}
Ques 25. Explain the concept of dependency injection.
Dependency injection is a design pattern where the dependencies of a class are injected from the outside rather than created within the class. It promotes loose coupling and testability.
Example:
class Database {
/* database operations */
}
class UserRepository {
private $database;
public function __construct(Database $database) {
$this->database = $database;
}
}
Ques 26. What is the purpose of the 'final' keyword when applied to a class method?
When a method is marked as 'final,' it cannot be overridden by subclasses. It provides a way to prevent further modification of a specific method in a class hierarchy.
Example:
class Example {
final public function cannotOverride() { /* method implementation */ }
}
Ques 27. Explain the concept of the Observer design pattern.
The Observer pattern defines a one-to-many dependency between objects, where if one object changes its state, all its dependents are notified and updated automatically.
Example:
class Subject {
private $observers = [];
public function addObserver(Observer $observer) {
$this->observers[] = $observer; }
public function notifyObservers() {
foreach ($this->observers as $observer) { $observer->update(); } } }
Ques 28. What is the purpose of the 'abstract' keyword in PHP?
The 'abstract' keyword is used to declare an abstract class or method. Abstract classes cannot be instantiated, and abstract methods must be implemented by the subclasses.
Example:
abstract class Shape {
abstract public function calculateArea();
}
Ques 29. Explain the concept of late static binding and how it differs from static binding.
Late static binding allows accessing the called class using the 'static' keyword, while static binding refers to the class in which the method is defined. Late static binding is resolved at runtime.
Example:
class ParentClass {
public static function whoAmI() {
echo static::class; } }
class ChildClass extends ParentClass { }
ChildClass::whoAmI(); // Outputs 'ChildClass'
Ques 30. What is the purpose of the 'clone' keyword in PHP?
The 'clone' keyword is used to create a copy of an object. It performs a shallow copy by default, but the __clone() method can be implemented to customize the cloning process.
Example:
class MyClass {
public $property;
public function __clone() { // Additional cloning logic if needed } }
$obj1 = new MyClass(); $obj2 = clone $obj1;
Most helpful rated by users: