Skip to main content

3. Object-Oriented Programming (OOP)

What is a Class and an Object?​

Junior

Answer:

  • Class: A blueprint or template for creating objects. It defines properties (attributes) and methods (behaviors).
  • Object: An instance of a class. It is a concrete realization of the blueprint with its own state.

What are the main characteristics of OOP?​

Junior

Answer:

  • Encapsulation: Bundling data and methods that operate on that data within a single unit (class) and restricting direct access.
  • Inheritance: A mechanism where a subclass inherits properties and behaviors from a superclass.
  • Polymorphism: The ability of different classes to be treated as instances of the same interface or parent class.
  • Abstraction: Hiding complex implementation details and showing only the essential features of the object.

What are access modifiers in PHP?​

Junior

Answer:

  • public: Accessible from anywhere.
  • protected: Accessible within the class itself and by inheriting classes.
  • private: Accessible only within the class that defines it.

What are constructors and destructors?​

Junior

Answer:

  • __construct(): A special method called automatically when an object is instantiated.
  • __destruct(): A special method called when an object is destroyed or the script ends.

What is the difference between this, self, and static?​

Middle

Answer:

  • $this: Refers to the current object instance.
  • self: Refers to the current class. Used for static properties/methods.
  • static: Refers to the called class (Late Static Binding). this vs self vs static

What is the difference between an Interface and an Abstract Class?​

Middle

Answer:

  • Interface: Defines a contract. Can only have public methods and no implementation. A class can implement multiple interfaces.
  • Abstract Class: Can have both abstract methods and concrete methods with implementation. A class can only extend one abstract class. Interface vs Abstract Class Comparison

What are Traits?​

Middle

Answer: Traits are a mechanism for code reuse in single inheritance languages like PHP. They allow you to include sets of methods in several independent classes.


What is Method Overriding?​

Middle

Answer: Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class.


What are the different types of inheritance?​

Middle

Answer:

  • Single: A class inherits from one parent class.
  • Multilevel: A class inherits from a parent that is itself a subclass.
  • Hierarchical: Multiple subclasses inherit from a single parent class.
  • Hybrid: A combination of two or more of the above (using traits/interfaces). Note: PHP does not support Multiple Inheritance directly.

What are Namespaces in PHP?​

Middle

Answer: Namespaces provide a way in which to group related classes, functions and constants to avoid naming collisions.


What is the difference between Abstraction and Encapsulation?​

Middle

Answer:

  • Abstraction: Hiding complex implementation details and showing only the essential features of the object (Focus on "What").
  • Encapsulation: Bundling data and methods together and restricting direct access to some of the object's components (Focus on "How").

What are Magic Methods?​

Middle

Answer: Special methods that start with __ and are triggered by specific events (e.g., __get, __set, __call, __toString, __sleep, __wakeup).


What is Method Overloading in PHP?​

Senior

Answer: PHP does not support traditional method overloading (same name, different arguments). Instead, it uses magic methods like __call() to simulate it. Method Overloading in PHP