Here are Top 50 PHP OOPs (Object-Oriented Programming) Interview Questions and Answers tailored for beginner to intermediate levels, commonly asked in interviews:
1. What is OOPs in PHP?
Answer: Object-Oriented Programming (OOP) in PHP is a programming paradigm that uses objects and classes to structure code, promoting reusability and modularity.
2. What are the main principles of OOP?
Answer:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
3. What is a class in PHP?
Answer: A class is a blueprint for creating objects. It defines properties and methods.
class Car {
public $color;
public function drive() {
echo "Driving...";
}
}
4. What is an object in PHP?
Answer: An object is an instance of a class.
$car = new Car();
5. What is the difference between class and object?
Answer:
- Class: Blueprint
- Object: Instance of the blueprint
6. What is a constructor in PHP?
Answer: A special function __construct()
called automatically when an object is created.
7. What is a destructor in PHP?
Answer: The __destruct()
method is called when an object is destroyed.
8. What is inheritance in PHP?
Answer: It allows a class (child) to inherit properties and methods from another class (parent).
9. What is the parent
keyword in PHP?
Answer: Used to call parent class methods or constructors.
10. What are access modifiers in PHP?
Answer: Define visibility:
public
private
protected
11. What is encapsulation?
Answer: Binding data and methods that operate on the data within one unit (class), and restricting access using access modifiers.
12. What is abstraction?
Answer: Hiding internal implementation and showing only the necessary features.
13. What is polymorphism in PHP?
Answer: The ability to call the same method on different objects and have different behavior.
14. What is method overloading?
Answer: PHP does not support method overloading directly, but you can use __call()
for similar functionality.
15. What is method overriding?
Answer: Redefining the parent class method in the child class.
16. What is an interface in PHP?
Answer: An interface defines method signatures but no implementation.
17. What is an abstract class in PHP?
Answer: A class that cannot be instantiated and may contain abstract methods.
18. Difference between interface and abstract class?
Answer:
- Interface: only method declarations.
- Abstract: can have both declared and defined methods.
19. Can PHP class implement multiple interfaces?
Answer: Yes, PHP supports multiple interface inheritance.
20. What is the final
keyword in PHP?
Answer: Prevents method overriding or class inheritance.
21. What is the static
keyword in PHP OOP?
Answer: Allows methods/properties to be accessed without object instantiation.
22. What is $this
keyword in PHP?
Answer: Refers to the current object instance.
23. What is self
keyword in PHP?
Answer: Refers to the current class (used with static members).
24. What is late static binding in PHP?
Answer: Refers to the use of static::
to reference the called class in a static context.
25. What is object cloning in PHP?
Answer: Creating a copy of an object using the clone
keyword.
26. What is the __clone()
method?
Answer: A magic method called when an object is cloned.
27. What are magic methods in PHP?
Answer: Predefined methods like __construct()
, __destruct()
, __call()
, __get()
, __set()
, etc.
28. What is the use of __call()
in PHP?
Answer: Triggers when invoking inaccessible or undefined methods.
29. What is the use of __get()
and __set()
?
Answer: Used to get or set values of inaccessible/private properties.
30. How to check if an object is of a specific class?
Answer: Using instanceof
keyword.
31. What is autoloading in PHP?
Answer: Automatically includes class files when needed using spl_autoload_register()
.
32. What is the difference between include
, require
, include_once
, require_once
?
Answer:
include
: gives warning on failurerequire
: gives fatal error_once
: ensures file is included only once
33. What is traits in PHP?
Answer: Used to reuse methods in multiple classes, avoiding multiple inheritance.
34. Can constructors be overloaded in PHP?
Answer: No, but optional parameters or __call()
can be used to simulate it.
35. What is the difference between composition and inheritance?
Answer:
- Inheritance: “is-a” relationship
- Composition: “has-a” relationship
36. What is dependency injection?
Answer: Injecting object dependencies from outside rather than creating them internally.
37. What is a namespace in PHP?
Answer: Used to avoid name conflicts by grouping logically related classes and functions.
38. How to define a namespace in PHP?
namespace App\Controllers;
39. How to create and use a trait in PHP?
trait Logger {
public function log($msg) {
echo $msg;
}
}
class App {
use Logger;
}
40. What is SPL in PHP?
Answer: Standard PHP Library – a set of classes and interfaces.
41. What is PSR in PHP?
Answer: PHP Standards Recommendations – coding standards for PHP development.
42. What is the use of __toString()
?
Answer: Converts an object to a string when treated like a string.
43. What is a singleton class in PHP?
Answer: A class allowing only one instance to exist.
44. What is overloading vs overriding?
Answer:
- Overloading: Same name, different signatures (not directly supported in PHP).
- Overriding: Subclass modifies inherited method.
45. What is an anonymous class in PHP?
Answer: A class without a name used once, introduced in PHP 7.
$object = new class {
public function sayHi() {
return "Hi!";
}
};
46. How to handle exceptions in OOP PHP?
Answer: Using try
, catch
, and finally
blocks.
47. What is the difference between ==
and ===
in PHP?
Answer:
==
: compares value only===
: compares value and type
48. How to implement interfaces in PHP?
interface Animal {
public function speak();
}
class Dog implements Animal {
public function speak() {
echo "Bark";
}
}
49. What is object composition in PHP?
Answer: Using objects of one class inside another.
50. Can a class extend multiple classes in PHP?
Answer: No, PHP does not support multiple inheritance via classes. Use traits instead.
1 comment
[…] Top 100 PHP Interview Questions and Answers… […]