HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Interfaces

An **Interface** is a contract that defines which methods a class must implement, without specifying how these methods should be coded. While abstract classes can contain some implementation, interfaces are purely structural—they contain only method signatures and constants.


What is an Interface?

Interfaces are defined using the interface keyword. They allow you to create code that can work with any class that "follows the contract" of the interface, regardless of its inheritance background.

<?php
    interface Animal {
        public function makeSound();
    }
?>

Implementing an Interface

A class uses the implements keyword to implement an interface. Like abstract classes, a class that implements an interface must provide the body for every method defined in that interface.

<?php
    interface Animal {
        public function makeSound();
    }

    class Cat implements Animal {
        public function makeSound() {
            echo "Meow";
        }
    }

    $animal = new Cat();
    $animal->makeSound();
?>
Key Fact: All methods in an interface must be public. You cannot use private or protected modifiers within an interface.

Multiple Interfaces

One of the biggest advantages of interfaces is that a single class can implement **multiple** interfaces. This is the way PHP overcomes the limitation of single inheritance.

<?php
    interface CanFly {
        public function fly();
    }

    interface CanSwim {
        public function swim();
    }

    class Duck implements CanFly, CanSwim {
        public function fly() {
            echo "I am flying! ";
        }
        public function swim() {
            echo "I am swimming! ";
        }
    }

    $duck = new Duck();
    $duck->fly();
    $duck->swim();
?>

Interfaces vs. Abstract Classes

Feature Abstract Class Interface
Methods Can have both abstract and regular methods. Can only have abstract methods.
Properties Can have properties (variables). Cannot have properties (only constants).
Inheritance A class can only extend ONE parent. A class can implement MULTIPLE interfaces.
Visibility Can be public, protected, or private. Must always be public.

Why Use Interfaces?

  • Interoperability: Allow different classes to be used interchangeably as long as they implement the same interface.
  • Decoupling: Write code that depends on an interface rather than a specific class, making your app easier to change.
  • Standards: Ensure that all classes in a specific category (e.g., all Logger classes) have the same set of methods.
  • Multiple Implementation: Combine features from different sources into a single class.
Tip: Interfaces are perfect for defining "capabilities" (e.g., JSONSerializable, Countable, Authenticatable).

Summary

  • interface keyword defines the contract.
  • implements keyword is used by classes to follow the contract.
  • Interfaces can only contain public methods.
  • Interfaces cannot contain properties (variables).
  • A class can implement many interfaces at once.