The 25 PHP OOP questions with syntax examples for better understanding

1. What is OOP in PHP?

Answer: OOP uses classes and objects to structure code.

   class Animal {
       public $name;
       public function speak() {
           echo "Animal speaks!";
       }
   }

   $dog = new Animal();
   $dog->speak();

2. What is a Class in PHP?

  • Answer: A class is a blueprint for creating objects.
   class Car {
       public $make;
       public $model;
   }

3. What is an Object in PHP?

  • Answer: An object is an instance of a class.
   $myCar = new Car();
   $myCar->make = 'Toyota';
   $myCar->model = 'Corolla';

4. What is a Constructor in PHP?

  • Answer: A constructor initializes object properties.
   class User {
       public $name;
       public function __construct($name) {
           $this->name = $name;
       }
   }

   $user = new User("John");

5. What is Inheritance in PHP?

  • Answer: Inheritance allows a class to inherit properties and methods from another class.
   class Vehicle {
       public function start() {
           echo "Starting engine...";
       }
   }

   class Car extends Vehicle {}
   $car = new Car();
   $car->start();

6. What is Method Overriding in PHP?

  • Answer: A child class can override a method from the parent class.
   class Animal {
       public function speak() {
           echo "Animal speaks";
       }
   }

   class Dog extends Animal {
       public function speak() {
           echo "Dog barks";
       }
   }

   $dog = new Dog();
   $dog->speak();

7. What is the final keyword?

  • Answer: final prevents a class from being inherited or a method from being overridden.
   final class DatabaseConnection {}

8. What is the self keyword in PHP?

  • Answer: self refers to the current class (for static members).
   class Example {
       public static function display() {
           echo "Inside class";
       }
   }
   Example::display();

9. What is the this keyword in PHP?

  • Answer: this refers to the current object instance.
   class Person {
       public $name;
       public function setName($name) {
           $this->name = $name;
       }
   }

10. What is Polymorphism in PHP?

  • Answer: Polymorphism lets objects be treated as instances of their parent class.
   interface Speakable {
       public function speak();
   }

   class Dog implements Speakable {
       public function speak() {
           echo "Bark";
       }
   }

11. What is an Interface in PHP?

  • Answer: An interface defines a contract for classes to implement.
   interface Drawable {
       public function draw();
   }

   class Circle implements Drawable {
       public function draw() {
           echo "Drawing circle";
       }
   }

12. What is an Abstract Class in PHP?

  • Answer: An abstract class can have abstract and non-abstract methods.
   abstract class Shape {
       abstract public function draw();
   }

   class Circle extends Shape {
       public function draw() {
           echo "Drawing circle";
       }
   }

13. What is Encapsulation?

  • Answer: Encapsulation restricts access to class properties and methods.
   class BankAccount {
       private $balance;

       public function deposit($amount) {
           $this->balance += $amount;
       }
   }

14. What are Access Modifiers in PHP?

  • Answer: Access modifiers (public, protected, private) control property and method visibility.
   class Example {
       public $publicVar;
       protected $protectedVar;
       private $privateVar;
   }

15. Explain Static Properties and Methods.

  • Answer: Static members belong to the class, not instances.
   class Counter {
       public static $count = 0;
       public static function increment() {
           self::$count++;
       }
   }

16. What is the __autoload() function?

  • Answer: __autoload() loads undefined classes automatically.
    Deprecated: Use spl_autoload_register().
   function __autoload($class) {
       include $class . '.php';
   }

17. What is spl_autoload_register()?

  • Answer: It registers a custom autoload function.
   spl_autoload_register(function ($class) {
       include $class . '.php';
   });

18. What is a Trait in PHP?

  • Answer: A trait allows code reuse in classes.
   trait Logger {
       public function log($message) {
           echo $message;
       }
   }

   class User {
       use Logger;
   }

19. Difference between == and ===?

  • Answer: == compares values, === compares values and types.
   if (1 == '1') {} // true
   if (1 === '1') {} // false

20. What are Magic Methods in PHP?

  • Answer: Magic methods (like __construct, __destruct) start with __.
   class MagicExample {
       public function __construct() {
           echo "Constructor called";
       }
   }

21. What is Dependency Injection?

  • Answer: Dependencies are passed into a class instead of created within it.
   class Database {}
   class User {
       private $db;
       public function __construct(Database $db) {
           $this->db = $db;
       }
   }

22. Explain Late Static Binding.

  • Answer: static binds to the class called at runtime.
   class A {
       public static function who() {
           echo __CLASS__;
       }
       public static function test() {
           static::who();
       }
   }

   class B extends A {
       public static function who() {
           echo __CLASS__;
       }
   }
   B::test(); // Outputs "B"

23. What is the instanceof operator?

  • Answer: It checks if an object is an instance of a class.
   if ($user instanceof User) {
       echo "User instance";
   }

24. How to handle errors in PHP OOP?

  • Answer: Use exceptions with try and catch.
   try {
       throw new Exception("An error occurred");
   } catch (Exception $e) {
       echo $e->getMessage();
   }

25. What is a Namespace in PHP?

  • Answer: Namespaces prevent name conflicts.
   namespace MyApp;
   class User {}
Scroll to Top