Please wait

Interfaces

An interface in PHP is like a contract or a promise. When a class agrees to implement an interface, it's promising to provide certain functionality.

Creating an Interface

Imagine you are building something using blocks. You have different types of blocks, like squares, rectangles, and circles. Each of these shapes is like a class in PHP. A class is like a blueprint for creating objects in programming. For example, if you have a Car class, you can create car objects from that class.

Now, suppose you want to ensure that all of these shapes, regardless of whether they are squares, rectangles, or circles, have a certain feature. Let's say you want all of them to have a specific color. This is where interfaces come in.

If we create an interface called Colored, and we require that it has a method called setColor(), any shape (class) that implements this interface must have a setColor() method.

Interfaces are defined with the interface keyword followed by the name of an interface. It's common practice for interface names to use pascal casing.

interface Colored {
  public function setColor($color);
}

An interface can contain method declarations and constants. However, it cannot contain properties (variables). You can also declare constants in interfaces. Suppose we want to define a constant for the default color:

interface Colored {
  const DEFAULT_COLOR = 'blue';
 
  public function setColor($color);
}

Now DEFAULT_COLOR is a constant that's available to any class that implements Colored.

It's important to remember that the methods declared in an interface must be public, and they do not contain a body (i.e., no actual code, just the declaration). The code, or the "how it's done," is defined in the classes that implement the interface.

Implementing Interfaces

Classes don't extend interfaces. Instead, they implement interfaces. The main difference is that classes are limited to extending one class. However, multiple interfaces can be implemented in a class.

We can implement a class by using the implements keyword. This keyword must be placed after the class name. This keyword is followed by the interface name.

class Square implements Colored {
  protected $color;
 
  public function setColor($color) {
    $this->color = $color;
  }
}

In this example, Square is promising to implement Colored, so it must have a setColor() method. If it doesn't, PHP will throw an error. A class that implements an interface is referred to as a concrete class. This concrete class is required to define all methods outlined in the interface. Therefore, we can say the Square class is a concrete class for the Colored interface.

Extending Interfaces

It is possible to extend interfaces with other interfaces in PHP, just like you can extend classes with other classes.

Let's say we have another interface named Shaped, which has a method called draw(). If we want all colored shapes to also be drawable, we can have Colored extend Shaped. Here's how it would look:

interface Shaped {
  public function draw();
}
 
interface Colored extends Shaped {
  const DEFAULT_COLOR = 'blue';
 
  public function setColor($color);
}

Now, any class that implements Colored must also implement the draw() method from Shaped, in addition to the methods from Colored.

Here's how our Square class would look now:

class Square implements Colored {
  protected $color;
 
  public function setColor($color) {
    $this->color = $color;
  }
 
  public function draw() {
    // Code to draw the square goes here.
  }
}

As you can see, the Square class is now required to have a draw() method because it implements Colored, which extends Shaped. This is a great way to group related interfaces together and ensure classes fulfill multiple related contracts at once.

Implementing Multiple Interfaces

A class can implement multiple interfaces. This allows a class to inherit the method declarations from multiple sources.

Continuing from our previous example, suppose we have another interface, Sizable, that deals with the size of our shapes. It might look like this:

interface Sizable {
  public function setSize($size);
}

Now, let's say we want our Square to be both Colored and Sizable. We can implement both interfaces in the class definition like so:

class Square implements Colored, Sizable {
  protected $color;
  protected $size;
 
  public function setColor($color) {
    $this->color = $color;
  }
  public function draw() {
    // Code to draw the square goes here.
  }
 
  public function setSize($size) {
    $this->size = $size;
  }
}

In this example, the Square class is now implementing two interfaces: Colored and Sizable. This means that the Square class must provide the methods defined in both of these interfaces.

So, now our square has color and size, and we can be sure that every square (and any other shape implementing these interfaces) can have its color and size set and retrieved in a consistent way.

Why use interfaces?

Using interfaces in programming, including PHP, has several benefits:

  • Consistency and Predictability: Interfaces enforce a certain structure. If you know a class implements a specific interface, you can be sure that certain methods will be available in that class, even if you're not familiar with the specifics of the class. This can make code more predictable and easier to work with.
  • Flexibility: A class can implement multiple interfaces, which means you can mix and match behaviors as needed. If you need an object that can perform two or three specific tasks, you can define interfaces for each of those tasks and create a class that implements all of them.
  • Abstraction and Decoupling: Interfaces allow you to write code that talks to the interface, not the specific class. This means you can change the underlying class without affecting the rest of your code. This is useful in a lot of scenarios, for instance, when you're testing your code or when you want to replace one part of your system with a better version.
  • Code Organization and Readability: By looking at the interfaces a class implements, you can quickly understand what that class is supposed to do. This can make your code more self-explanatory and easier to maintain.

Key Takeaways

  • Interfaces in PHP are like contracts for classes. They define what a class should do, but not how it should do it.
  • An interface can only contain public method declarations. It can't contain any variables or implement any methods (i.e., provide any code).
  • Interfaces can contain constants.
  • A single class can implement multiple interfaces.
  • Just like classes, interfaces can extend other interfaces, which allows one interface to inherit the method declarations of another.
  • Interfaces help ensure that certain classes all have the same set of methods.
  • Interfaces allow you to work with objects in an abstract way, focusing on what the object does rather than what it is. This can help make your code more flexible and adaptable.

Comments

Please read this before commenting