PHP Interfaces

PHP interfaces provide a way to define a set of methods that a class must implement, without specifying how they should be implemented.

Interfaces are a powerful tool for building loosely-coupled, reusable code and are an essential part of modern object-oriented programming.

The goal of this article is to introduce you to the use of interfaces in PHP OOP, both in terms of creating them and implementing them, as well as some best practices that you should follow in order to make them work effectively in your code.

An interface is a set of methods that are required to be implemented by a class, but the interface itself is not able to provide any implementations.

It allows the code to be more flexible and extensible, as different classes are able to implement the same interface in a unique manner, allowing for greater flexibility and extensibility.



What are PHP Interfaces?

In PHP, an interface is a collection of abstract methods that a class can implement.

The purpose of PHP interface is to specify what methods should be implemented by a class.

Interfaces are a good way to make it easy to use for a variety of different classes to be used in the same manner.

There is a term called “polymorphism“, which refers to an implementation in which multiple classes use the same interface.

Interfaces are declared using the “interface” keyword and define a set of methods that a class must implement.

Syntax

<?php
interface InterfaceName
{
public function method1();
public function method2($arg1, $arg2);
}
?>

In above syntax, we have defined an interface called “InterfaceName” with two abstract methods named method1() and method2().

Note:  Interfaces only contain method signatures (the method name, parameter list, and return type), and do not contain any implementation details.

 

PHP Interfaces – Implementation

The implement keyword is used by classes in order to implement an interface.

To implement an interface in PHP, we must create a class that implements all the methods defined in the interface.

When a class implements an interface, all of its methods must be implemented by that class.

Example: 

<?php interface Car { public function setModel($name); public function getModel(); } class microCar implements Car { private $model; public function setModel($name) { $this -> model = $name; }public function getModel() { return $this -> model; } } $microcar = new microCar(); $microcar->setModel("Swift"); echo "This is a ".$microcar->getModel(); ?>

In the example above, let us say that we would like to write software that manages a group of cars on a daily basis.

There are some actions that every car is capable of performing, but every car performs those actions differently.

We can use interfaces to write some code that can be used for all of the cars, even if each one behaves differently from the other:

Example: 

<?php // Interface definition interface Cars { public function mileage(); } // Class definitions class microCar implements Cars { public function mileage() { echo "Micro car gives mileage upto 65 MPG <br>"; } } class hatchBack implements Cars { public function mileage() { echo "HatchBack cars gives mileage from 20 MPG to 55 MPG <br>"; } } class familyCar implements Cars { public function mileage() { echo "Family cars gives mileage from 230 MPG to 460 MPG <br>"; } } // Create a list of animals $micro_car = new microCar(); $hatchback = new hatchBack(); $family_car = new familyCar(); $cars = array( $micro_car, $hatchback, $family_car ); // Tell the mileage of a car in category foreach ($cars as $car) { $car->mileage(); } ?>

Example Explanation:

This is a PHP code that demonstrates the use of interfaces in object-oriented programming.

First, an interface named “Cars” is defined with a single method named “mileage()“. This interface serves as a contract that specifies that any class that implements it must define a method called “mileage()”.

Next, three classes – “microCar“, “hatchBack“, and “familyCar” – are defined that implement the “Cars” interface. Each of these classes defines its own implementation of the “mileage()” method.

After the classes are defined, an array named “$cars” is created that contains instances of each of the three classes.

Finally, a foreach loop is used to iterate over the cars in the array and call the “mileage()” method on each of them.

When this code is executed, it will output the mileage of each type of car, as defined in the “mileage()” method of each class.

This demonstrates the flexibility and extensibility of using interfaces in object-oriented programming, as each class can implement the same interface in its own unique way.

If you liked this article and found it informative regarding PHP OOP, you can leave your feedback by reacting below.


Why Use Interfaces in PHP?

Interfaces are useful in PHP for several reasons:

  • PHP Interfaces allow us to create a hierarchy of related classes, making it easier to write code that works with multiple related objects.
  • Interfaces provide a way to define a contract between two components without specifying how they should be implemented. This reduces the coupling between components, making code more modular and easier to maintain.
  • Interfaces provide a way to define a common set of functionality that can be reused across multiple classes.
  • PHP Interfaces provide a way to standardize the behavior of related classes, making it easier to write code that works with different implementations of the same interface.

PHP Interfaces vs. Abstract Classes

The concept of interfaces is similar to the concept of abstract classes.

The main difference between an interface and an abstract class is as follows:

  • Abstract classes can have properties, but an interface cannot have any.
  • As far as interface methods are concerned, they are all required to be public, whereas methods of abstract classes can be either public or protected.
  • The methods in an interface are all abstracted, so they can’t be implemented in code, and for this reason, the abstract keyword is not needed.
  • The implementation of an interface can be done while the class is inheriting from one another at the same time.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *