PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In PHP Object-Oriented Programming (OOP), interfaces and abstract classes are two mechanisms to define abstract types that cannot be instantiated directly. They serve as blueprints for creating concrete classes and provide a way to enforce a contract for the classes that implement or extend them. Here’s a detailed explanation of each:

Interfaces

Definition

An interface in PHP is a way to define a contract that any implementing class must follow. It specifies methods that the implementing classes must provide, but does not contain any implementation details.

Syntax

				
					interface InterfaceName {
    public function method1();
    public function method2($param);
}

				
			

Key Points

  1. No Implementation: Interfaces cannot contain any method implementations. All methods declared in an interface are implicitly abstract and public.
  2. Multiple Inheritance: A class can implement multiple interfaces, which is a way to achieve multiple inheritance in PHP.
  3. Constants: Interfaces can contain constants, but no properties or method implementations.
  4. Enforcement: Any class that implements an interface must provide concrete implementations for all methods declared in the interface.

Example

				
					interface Logger {
    public function log(string $message);
}

class FileLogger implements Logger {
    public function log(string $message) {
        // Code to log the message to a file
    }
}

class DatabaseLogger implements Logger {
    public function log(string $message) {
        // Code to log the message to a database
    }
}

				
			

Abstract Classes

Definition

An abstract class in PHP is a class that cannot be instantiated on its own and is intended to be subclassed. It can contain both abstract methods (without implementation) and concrete methods (with implementation).

Syntax

				
					abstract class AbstractClass {
    abstract protected function method1();
    
    public function concreteMethod() {
        // Implementation of a concrete method
    }
}

				
			

Key Points

  1. Abstract Methods: Abstract methods must be defined in any subclass of the abstract class. These methods provide no implementation in the abstract class itself.
  2. Concrete Methods: Abstract classes can also include concrete methods, which can be inherited by subclasses.
  3. Properties: Unlike interfaces, abstract classes can have properties.
  4. Single Inheritance: PHP supports single inheritance, so a class can only extend one abstract class.

Example

				
					abstract class Animal {
    protected $name;
    
    public function __construct($name) {
        $this->name = $name;
    }

    abstract public function makeSound();
    
    public function getName() {
        return $this->name;
    }
}

class Dog extends Animal {
    public function makeSound() {
        return "Woof!";
    }
}

class Cat extends Animal {
    public function makeSound() {
        return "Meow!";
    }
}

$dog = new Dog("Buddy");
echo $dog->makeSound(); // Outputs: Woof!
echo $dog->getName();   // Outputs: Buddy

				
			

Differences Between Interfaces and Abstract Classes

  • Implementation:
    • Interfaces cannot have any method implementations, whereas abstract classes can.
  • Multiple Inheritance:
    • A class can implement multiple interfaces but can extend only one abstract class.
  • Use Cases:
    • Use interfaces to define a contract that multiple classes should adhere to without dictating how they should implement the methods.
    • Use abstract classes when you want to share common code among related classes and also define methods that must be implemented in the subclasses.

Interfaces and abstract classes are both essential tools in PHP ‘OOP’ for defining structures and enforcing contracts. Interfaces provide a way to enforce that certain methods are implemented without dictating how they should be implemented. Abstract classes allow for a shared base of functionality and enforce certain methods to be implemented while also allowing for common method implementations and properties.

Scroll to Top