PHP OOP - Interfaces

PHP - What are Interfaces?


An interface in PHP defines a contract that classes must follow. Any class that implements an interface must define all its methods.


Key Characteristics of Interfaces

✅ Defined using the interface keyword.

✅ Cannot have properties (only method declarations).

✅ All methods are public by default.

✅ A class can implement multiple interfaces (unlike abstract classes).

✅ Interface methods cannot have implementations (must be defined in the implementing class).


Syntax


<?php
  interface InterfaceName {
    public function someMethod1();
    public function someMethod2($name, $color);
    public function someMethod3() : string;
  }
?>



PHP - Interfaces vs. Abstract Classes


Both interfaces and abstract classes are used to define blueprints for other classes, but they have key differences in structure, usage, and flexibility.

Feature

Interface

Untitle

Definition

Uses interface keyword

Uses abstract class keyword

Method Implementation

Cannot have method implementations (only method declarations)

Can have both abstract (no implementation) and concrete (implemented) methods

Properties (Variables)

Cannot have properties

Can have properties

Method Access Modifiers

Always public

Can be public, protected, or private

Constructors

Cannot have constructors

Can have constructors

Multiple Inheritance

A class can implement multiple interfaces

A class can extend only one abstract class

Use Case

Used when multiple unrelated classes need to implement the same behavior

Used for common functionality among related classes

Example
<?php
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>

Try it yourself

From the example above, let's say that we would like to write software which manages a group of animals. There are actions that all of the animals can do, but each animal does it in its own way.


Using interfaces, we can write some code which can work for all of the animals even if each animal behaves differently:

Example
<?php
// Define the Animal interface
interface Animal {
public function makeSound();
}
// Implementing the interface in Cat class
class Cat implements Animal {
public function makeSound() {
return "Meow!";
}
}
// Implementing the interface in Dog class
class Dog implements Animal {
public function makeSound() {
return "Woof!";
}
}
// Implementing the interface in Mouse class
class Mouse implements Animal {
public function makeSound() {
return "Squeak!";
}
}
// Creating an array of different animals
$animals = [new Cat(), new Dog(), new Mouse()];
// Loop through the animals and call makeSound() without knowing their type
foreach ($animals as $animal) {
echo $animal->makeSound() . "<br>";
}
?>

Try it yourself

Explanation

1. Define an Interface (Animal)

  • The Animal interface declares the makeSound() method.
  • Any class that implements Animal must define makeSound().

2. Implementing Classes (Cat, Dog, Mouse)

  • Each animal implements Animal and defines makeSound() differently.

3. Polymorphism: Handling Different Animals in a Loop

  • We store different objects in an array.
  • The loop calls makeSound() on each object, without needing to check its type.
  • This is an example of polymorphism—different objects respond differently to the same method.

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.