PHP OOPs Interview Questions and Answers
Experienced / Expert level questions & answers
Ques 1. 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 2. 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 3. 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 4. 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 5. 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 6. 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 7. 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 8. 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 9. 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 10. 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 11. 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 12. 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 13. 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: