How can we create an interface composed of other interfaces?
Example:
<?php
namespace Domain\Interfaces;
interface Arrayable
{
public function toArray();
}
interface Jsonable
{
public function toJson();
}
interface Manager implements Arrayable, Jsonable
{
// void
}
Using implements
generates an error:
syntax error, unexpected 'implements' (T_IMPLEMENTS), expecting '{'
To be able to make interfaces composed of other interfaces instead of using
implements
we must useextends
. Interfaces do not implement anything but can extend other interfaces. Also, unlike class inheritance, interfaces in php do allow multiple inheritance.Example:
Another alternative may be to create an Abstacta class that implements both interfaces:
Example:
Documentation: