In PHP, the interface blocks declare a 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.
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 deals with PHP interface and class to show how to implement an interface for a class.
<?php
interface UserInterface
{
public function setMenu($menu_array);
public function getContent($page);
}
class WebApp implements UserInterface
{
public function setMenu($menu_array)
{
$menu_bar = "";
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 the 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 implementing of the above interface, two functions declared at UserInterface have 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 a 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 ...
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 the getPopularTags() function is declared. Now, we are going to extend this Widget interface for UserInterface.
<?php
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.
As we have seen in the 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 a class inherits two parent classes where multiple inheritances are supported. For example, in the following program, UserInterface extends both interfaces named Advertisement, and Widget to inherit the functions declared with them.
<?php
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 a User interface must have a definition for all the functions declared in the User interface and also in other interfaces, that is, Widget, Advertisement, it extends with.
In PHP, the constants are defined inside the interface block, by using the const keyword. For example, a PHP Interface constant can be defined as follows.
<?php
const MAX_LENGTH = "100";
?>
We can access the interface constants by using the 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 the User interface of our first example, then this constant can be accessed by using the following possible number of ways.
Note:
class-name extends interface-name
will cause errors, like,
Fatal error: Class ... cannot extend from interface UserInterface in...
Download PHP Interface Source Code