1. Can you explain the difference between abstract classes and interfaces in PHP?

Advanced

1. Can you explain the difference between abstract classes and interfaces in PHP?

Overview

Understanding the difference between abstract classes and interfaces in PHP is crucial for object-oriented programming. This knowledge helps in designing flexible and maintainable code, enabling developers to define blueprints for classes and dictate certain behaviors that classes must implement, promoting a more structured and organized approach to coding.

Key Concepts

  1. Inheritance vs. Polymorphism: Abstract classes are used to provide a common definition of a base class that can be shared by multiple derived classes. Interfaces, on the other hand, purely enforce a contract for what a class can do, without specifying how it does it.
  2. Abstract Class Features: They can have both abstract methods (without an implementation) and concrete methods (with an implementation). They cannot be instantiated and require subclasses to provide implementations for the abstract methods.
  3. Interface Features: An interface can only declare methods but not implement them. All methods in an interface are implicitly abstract, and they must be public. A class can implement multiple interfaces.

Common Interview Questions

Basic Level

  1. What is an abstract class and an interface in PHP?
  2. How do you declare an abstract class and an interface in PHP?

Intermediate Level

  1. Can a PHP class implement multiple interfaces or extend multiple abstract classes?

Advanced Level

  1. How would you choose between using an abstract class and an interface in PHP?

Detailed Answers

1. What is an abstract class and an interface in PHP?

Answer: An abstract class in PHP is a class that cannot be instantiated on its own and is designed to be subclassed. It can contain both abstract methods (without bodies) and concrete methods (with implementation). An interface is a completely abstract class that only contains abstract methods (without bodies) and constants; it specifies what a class must do but not how.

Key Points:
- Abstract classes can have both abstract and concrete methods.
- Interfaces can only have abstract methods.
- Both are used to achieve abstraction in PHP.

Example:

abstract class Animal {
    public function sleep() {
        echo "Sleeping";
    }

    abstract public function makeSound();
}

interface Movable {
    public function move();
}

2. How do you declare an abstract class and an interface in PHP?

Answer: To declare an abstract class in PHP, you use the abstract keyword before the class keyword. To declare an interface, you use the interface keyword.

Key Points:
- Abstract classes are declared using abstract class ClassName {}.
- Interfaces are declared using interface InterfaceName {}.
- Abstract methods in both abstract classes and interfaces do not have a body.

Example:

abstract class Vehicle {
    abstract public function startEngine();
}

interface Flyable {
    public function fly();
}

3. Can a PHP class implement multiple interfaces or extend multiple abstract classes?

Answer: A PHP class can implement multiple interfaces, but it can only extend one abstract class. PHP does not support multiple inheritance for classes, making interfaces a powerful tool for polymorphism.

Key Points:
- Multiple interfaces can be implemented by a single class using the implements keyword, separated by commas.
- A class can only extend one other class (abstract or not) using the extends keyword.

Example:

interface Walkable {
    public function walk();
}

interface Runnable {
    public function run();
}

class Human implements Walkable, Runnable {
    public function walk() {
        echo "Walking";
    }

    public function run() {
        echo "Running";
    }
}

4. How would you choose between using an abstract class and an interface in PHP?

Answer: The choice between using an abstract class and an interface depends on the design requirement. If you need to provide a common base class with shared method implementations, an abstract class is appropriate. Use interfaces when you want to enforce a particular contract across classes without dictating how they achieve it, especially useful when the classes are unrelated and need to implement the same set of methods.

Key Points:
- Use abstract classes for closely related objects with shared behavior.
- Use interfaces for unrelated classes to implement the same interface or for classes that require multiple inheritances.
- Abstract classes allow you to define some common behavior that subclasses can inherit or override, while interfaces purely specify a set of methods that implementing classes must provide.

Example:

// Use an abstract class
abstract class ElectronicDevice {
    public function turnOn() {
        echo "Device is on";
    }

    abstract public function operate();
}

// Use an interface
interface Chargeable {
    public function charge();
}