PHP Interfaces

by Vincy. Last modified on June 2nd, 2021.

In PHP, the interface blocks which declares set of functions to be defined with a class to implement this interface. A class can extend more than one interface, thereby, we can simulate multiple inheritances in PHP. Let us see all about it, in detail with this article.

PHP class implements Interface

While implementing an interface with a PHP class, we must define all the functions declared in that interface as it is, without changing arguments, return type, access modifiers or anything.

For example, the following code has deals with PHP interface and class to show how to implement an interface for a class.

interface UserInterface {
public function setMenu($menu_array);
public function getContent($page);
class WebApp implements UserInterface {
public function setMenu($menu_array) {
foreach($menu_array as $k=>$v) {
$menu_bar .="<a href='" . $menu_array[$k]["menu_link"] . "'>" . $menu_array[$k]["menu_caption"] . "</a>  ";
return $menu_bar;
public function getContent($page) {
$content = "<h1>Welcome to the " . ucwords($page) . " page</h1>";
return $content;

In the above example, we have created a PHP interface named as UserInterface, and this is implemented by a class WebApp using implements keyword. We can implement this interface, wherever we want the same set of user interface components that is menu and content for our web application.

While implementation of the above interface, two functions declared at UserInterface has been defined in the class which implements it.

While defining, if any one of those functions, or any signature, like arguments, return type are changed, then it will cause some PHP error. For example, if the WebApp class is not having getContent() definition, then, the error will be,

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...

Interface extends Interface

In PHP, an interface can extend another interface and thereby it inherits all functions declared with this interface. For that, we should use extends keyword, as we used for inheriting classes.

For example, let us add one more interface named Widget, with the above program, in which getPopularTags() function is declared. Now, we are going to extend this Widget interface for UserInterface.

interface Widget {
public function getPopularTags();
interface UserInterface extends Widget {
public function setMenu($menu_array);
public function getContent($page);

By extending Widget interface, meaning that, the UserInterface is extended with one more function declaration for getPopularTags(). Now, for implementing UserInterface, a PHP class must define getPopularTags() also.

Multiple Inheritance in PHP

As we have seen in PHP inheritance article, PHP supports only single inheritance. But, using this property of extending PHP interfaces, we can simulate multiple inheritances.

An interface extends two interfaces, as like as a class inherits two parent classes where multiple inheritances is supported. For example, in the following program, UserInterface extends both interfaces named Advertisement, Widget to inherit the functions declared with them.

interface Widget {
public function getPopularTags();
interface Advertisement {
public function getAdsTile();
interface UserInterface extends Widget, Advertisement{
public function setMenu($menu_array);
public function getContent($page); 

If any PHP class which implements User interface must have a definition for all the functions declared in User interface and also in other interfaces, that is, Widget, Advertisement,  it extends with.

PHP Interface Constants

In PHP, the constants are defined inside interface block, by using const keyword. For example, a PHP Interface constant can be defined as follows.

const MAX_LENGTH = "100";

We can access the interface constants by using interface name, or by using the name of the class, and also the name of the class instance, which implements the interface.

For example, if we define MAX_LENGTH for User interface of our first example, then this constant can be accessed by using the following possible number of ways.

  • Usernterface ::MAX_LENGTH
  • WebApp ::MAX_LENGTH
  • Instance of WebApp (if any).
  • $this::MAX_LENGTH (within class definition).


  • All functions declared in PHP interfaces should be specified with public modifier to allow its visibility on defining them for classes which implement this interface
  • We cannot extend the class with an interface. For example,
    class-name extends interface-name

    will cause error, like,

    Fatal error: Class ... cannot extend from interface UserInterface in...
  • Similarly, we can not implement an interface into another interface,  which will cause T_IMPLEMENTS error.

Download PHP Interface Source Code

Leave a Reply

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

↑ Back to Top

Share this Article