PHP uses the interface keyword to define method contracts that implementing classes must follow. Interfaces keep code flexible, let one class implement several contracts, and separate what a component must do from how it does it—useful when a project needs room to grow or change.

Multiple Choice

What keyword is used to create an interface in PHP?

In PHP, the keyword used to create an interface is "interface." This keyword allows developers to define a contract that classes must adhere to if they implement the interface. An interface can contain method declarations, which means it defines the methods that a class must implement, but it does not provide any actual implementation for those methods. Using interfaces promotes a design principle known as "programming to an interface, not an implementation," allowing for greater flexibility and the ability to swap out different implementations of functionality without changing the code that relies on the interface. Interfaces can also be used in multiple inheritance scenarios, as a class can implement multiple interfaces, enabling a more modular approach to defining functionality. The other options serve different purposes. The "class" keyword is used to define a class, which is a blueprint for creating objects. "Abstract" is used to declare abstract classes that may have abstract methods, allowing for some methods to be defined and some to be left for subclasses. Lastly, "public" is an access modifier that determines the visibility of properties and methods, and is not related to the creation of interfaces.

PHP Interfaces: A Clear Contract for Your Classes

In PHP, the interface keyword defines a contract that a class can agree to follow. The interface lists methods the class must provide, while leaving the method bodies to the class itself. That simple split can make an application easier to extend: code can depend on what an object promises to do, rather than on the details of how it does it.

Think of an interface as a set of rules for a team. One person may handle a task one way, another may take a different route, but both need to meet the same expectations. In PHP, those expectations are written into an interface, and any class that implements it must fulfill them.

The keyword that sets the contract

Here’s a small example:


interface Logger

{

public function log(string $message): void;

}

The declaration begins with interface, followed by a name and a block of method declarations. The log() method has a parameter and a return type, but no body. There’s no opening brace after the method signature and no code inside it. The interface describes the method; it doesn’t carry out the work.

A class connects itself to that contract with implements:


class FileLogger implements Logger

{

public function log(string $message): void

{

// Write the message to a file.

}

}

Because FileLogger implements Logger, it must provide a compatible log() method. If it doesn’t, PHP reports an error. That may feel strict, but it catches a mismatch early—before one part of an application relies on a method another part never supplied.

The distinction between the two keywords matters: interface creates the contract, while implements says that a class accepts it.

Why this small idea matters

At first glance, an interface can seem like an extra layer. Why define a method in one place, then write it again in a class? The value becomes clearer when different objects need to offer the same capability.

For instance, an application might send notifications by email or text message. Each method of delivery works differently, but the rest of the application may only need to know that it can send a message.


interface Notifier

{

public function send(string $recipient, string $message): void;

}

class EmailNotifier implements Notifier

{

public function send(string $recipient, string $message): void

{

// Send an email.

}

}

class TextNotifier implements Notifier

{

public function send(string $recipient, string $message): void

{

// Send a text message.

}

}

Both classes make the same promise. A separate service can work with a Notifier without needing to know whether the message travels by email or text. The implementation can change, but the contract stays steady.

This is the idea behind “programming to an interface, not an implementation.” It doesn’t mean that concrete classes are unimportant. They do the actual work. It means that code which depends on a capability can rely on a shared contract instead of being tied to one specific class.

That separation is useful in large codebases, but it’s not reserved for them. Even a small project can benefit when a feature may gain another implementation later.

One class, several capabilities

PHP lets a class implement more than one interface. This offers a form of multiple inheritance: a class can take on several contracts, even though it can extend only one parent class.


interface Loggable

{

public function log(string $message): void;

}

interface Exportable

{

public function export(): string;

}

class Report implements Loggable, Exportable

{

public function log(string $message): void

{

// Record the message.

}

public function export(): string

{

return 'Report data';

}

}

Report now promises to log messages and export data. Each interface describes a focused capability. That’s often easier to understand than one large interface that demands a long list of unrelated methods.

An interface can also extend another interface:


interface TimestampedLogger extends Loggable

{

public function logAt(string $message, DateTimeImmutable $time): void;

}

A class implementing TimestampedLogger must meet the requirements inherited from Loggable as well as provide logAt(). Interfaces can extend multiple interfaces, too, allowing related contracts to be combined.

Interfaces and abstract classes aren’t the same

Interfaces and abstract classes both help define how classes should behave, so it’s easy to mix them up. The key difference is that an interface sets a contract, while an abstract class can also share code and state.

An abstract class may contain properties, concrete methods with working bodies, and abstract methods that subclasses must define. A class can extend just one class, abstract or otherwise. By contrast, a class can implement several interfaces.

Use an interface when you want to describe a capability that unrelated classes can share. Use an abstract class when a group of related classes should inherit common behavior or data. Neither tool is automatically better; they solve different design problems.

For example, EmailNotifier and TextNotifier may have little in common internally, yet both can implement Notifier. A family of database connection classes, on the other hand, might share code in an abstract parent class.

A few PHP details worth knowing

Interface methods are public. They form part of the contract, so an implementing class can’t make one private or protected. The class must also use compatible parameter and return types. If a method declares : void, for example, its implementation must honor that return expectation.

Interfaces can define constants as well. These constants belong to the interface and can be accessed through the interface name:


interface Status

{

public const ACTIVE = 'active';

}

In modern PHP, interfaces can also declare methods with features such as parameter types, return types, and—in supported versions—static methods. They still don’t provide ordinary method implementations. The point remains the same: an interface states what must be available, not the operational details behind it.

Names matter, too. In many PHP projects, interface names end with Interface, such as LoggerInterface, though this is a style choice rather than a language rule. Frameworks and teams may set their own conventions, so consistency is more useful than treating one naming style as universal.

Keep the contract focused

A useful interface should describe a clear role. If a single interface asks a class to send messages, write files, calculate tax, and render a page, it has likely become too broad. Classes may then be forced to implement methods they don’t need, sometimes with empty or awkward code.

Smaller contracts are often easier to reuse. A Loggable interface can describe logging; an Exportable interface can describe exporting. A class that needs both can implement both. This keeps the promises clear and lets each part of a system change with less friction.

PHP’s interface keyword is simple to learn, but it supports a powerful way of organizing code. It gives classes a shared vocabulary, lets different implementations fit behind the same contract, and helps keep dependencies flexible. When a class implements an interface, it’s making a concrete promise—and that promise can make the whole application easier to reason about.