PHP OOPs Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. 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 2. 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 3. 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 4. 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 5. 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 6. 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 7. 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 8. 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 9. 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 */ }
Most helpful rated by users: