Essential PHP OOP Questions

Object-Oriented Programming (OOP) in PHP can feel tricky, especially when it’s packed with keywords, principles, and magic methods. Here’s a curated list of the most common PHP OOP questions, simplified with short examples.

Core OOP Principles

Q1: What are the main principles of OOP in PHP?
A: The four pillars are Encapsulation, Inheritance, Polymorphism, Abstraction.

  • Encapsulation – keep properties and methods inside a class and control access (public, protected, private).
  • Inheritance – a class can extend another and reuse its code.
  • Polymorphism – different classes can share method names but behave differently.
  • Abstraction – abstract classes/interfaces define the structure, while child classes implement the details.

Q2: What is the role of public, protected, and private?
A: They are visibility modifiers:

  • public – accessible from anywhere.
  • protected – accessible inside the class and its children.
  • private – accessible only inside the same class.

Encapsulation uses these to protect and organize data.


Q3: What’s the difference between constructor and destructor in PHP?
A:

  • __construct() runs automatically when an object is created. Good for initialization.
  • __destruct() runs when the object is destroyed. Good for cleanup (like closing a file or database connection).
class FileHandler {
  private $file;
  public function __construct($path) {
    $this->file = fopen($path, 'w');
  }
  public function __destruct() {
    fclose($this->file);
  }
}

Abstraction and Inheritance

Q4: Abstract class vs Interface?
A:

  • Abstract class → can have normal methods + abstract methods. Extend with extends.
  • Interface → only method definitions, no bodies. Implement with implements.

One class can extend only one abstract class, but implement many interfaces.


Q5: What is method overriding vs overloading in PHP?
A:

  • Overriding → a child class redefines a parent method.
class Animal { function speak() { echo "sound"; } }
class Dog extends Animal { function speak() { echo "bark"; } }
  • Overloading in PHP → not like Java. It means using magic methods (__call, __get, etc.) to handle calls to undefined methods/properties.

Q6: What does final mean in PHP?
A:

  • A final class cannot be extended.
  • A final method cannot be overridden.
final class Logger {}
class User {
  final public function save() {}
}

Static, Traits, and Magic Methods

Q7: Static vs non-static methods/properties?
A:

  • Static → belongs to the class, not an object. Call with ClassName::method().
  • Non-static → tied to an instance. Call with $obj->method().
class Counter {
  public static $count = 0;
  public static function inc() { self::$count++; }
}
Counter::inc();
echo Counter::$count; // 1

Q8: How do traits work in PHP?
A: Traits let you reuse code across unrelated classes.

trait Logger {
  public function log($msg) { echo $msg; }
}
class User { use Logger; }
(new User)->log("hello");

If two traits have the same method, use insteadof or as to resolve conflicts.


Q9: What do __get() and __set() do?
A: They’re magic methods for handling inaccessible properties.

class Demo {
  private $data = [];
  public function __get($name) { return $this->data[$name] ?? null; }
  public function __set($name, $value) { $this->data[$name] = $value; }
}

Q10: What happens when cloning an object?
A: By default, PHP makes a shallow copy. Use __clone() for custom behavior.

class User {
  public $name;
  public function __clone() { $this->name .= " (copy)"; }
}

Advanced OOP Concepts

Q11: What is dependency injection?
A: Instead of creating dependencies inside a class, pass them from outside. This makes code more testable and flexible.

class User {
  private $db;
  public function __construct(Database $db) {
    $this->db = $db;
  }
}

Q12: What is late static binding?
A: When using static:: in a parent method, PHP calls the method from the called class, not the defined one.

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(); // B

Q13: What happens if you serialize an object with private properties?
A: PHP serializes them with a special internal key. On unserialize, they restore fine, but if the class structure changes, it can break.
For full control, use the Serializable interface or __sleep() / __wakeup() methods.


Q14: Traits vs Inheritance — which is more memory efficient?
A: Traits are injected at compile-time, avoiding deep inheritance chains. In practice, both are efficient, but traits are more flexible and prevent rigid hierarchies.


Quirky Edge Cases

Q15: Can a child class access a parent’s private property?
A: No. It must use a getter/setter or the property must be protected.

Q16: What if you call a static method with an object instance?
A: It works, but PHP will throw a notice. Best practice is ClassName::method().

Q17: What if a class has no constructor?
A: PHP doesn’t add one automatically. If the parent has one, the child inherits it (unless overridden).


Closing Thoughts

These questions cover the most common traps and concepts in PHP OOP: from encapsulation basics to serialization quirks. If you can explain these with examples, you’re already ahead of many developers.