1 Answer
Alok Prasad

In this article, we will figure out how to utilize the PHP interface that is one of the most significant structure patterns in PHP object-oriented programming.

An Interface enables us to make programs, indicating the public methods that a class must execute, without including the complexities and procedure of how the specific methods are implemented. This implies that an interface can define method names and arguments, but not the contents of the methods. Any classes implementing an interface must implement all methods defined by the interface.

Interfaces are characterized similarly as a class, however, only the interface keyword replaces the class phrase in the declaration and without any of the methods having their contents defined.

Let' create an interface and implement it with a simple example.

   Interface MyInterface {
      public function getName();
      public function getAge();
   class MyClass implements MyFirstInterface{
      public function getName() {
            echo "My name A".'<br>';
      public function getAge(){
            echo "My Age 12";
   $obj = new MyClass;


My name A
My Age 12


Here we have declared an interface MyFirstInterface with two methods getName and getAge inside it without any content. Then the class MyClass implements this interface and uses the available methods according to requirement.

Let's learn some important Characteristic of the interface:

  • An interface comprises of methods that have no content, which means the interface methods are abstract methods.
  • Every one of the methods in interfaces must have public visibility scope.
  • Interfaces are not quite the same as classes as the class can inherit from one class but the class can implement one or more interfaces.
  • No variables cant be present inside interface.


We can achieve multiple inheritances utilizing interface because a class can implement more than one interface whereas it can extend only one class.

Let's test this with a simple example.

   interface a{
      public function printData();
   interface b{
      public function getData();
   interface c extends a, b{
      public function addData();
   class d implements c{
      public function printData(){
         echo "I am printing";
      public function getData(){
         echo "I am reading data";
      public function addData(){
         echo "I am adding"
   $myobj = new class d();


I am printing
I am adding

Explanation :

Here we have declared three interfaces i.e 'interface a',' interface b', 'interface c'.In this case interface c also extends the previous two interfaces. Then we have declared a class that implements only the interface c, but as interface c extends the previous two interfaces, all the methods declared in the 'interface a', 'interface b' and 'interface c'  are available for the use in class d. This is how we can achieve multiple inheritances by implementing the interface.


We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.