Surfe.be - passive income

What is interface in PHP ?

Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined. All methods declared in an interface must be public. this is the nature of an interface . Note that it is possible to declare a constructor in an interface, what can be useful in some contexts .

How to declare Interface

interface School {
  	public function mySchool();
  }

Have a look , here we have declared an interface. Look , in interface we can just use only public access specifier and can not write any implementing code inside interface’s method. So we can implement interface method from class with override it . Hope you got it.

Let’s see the example

interface School{
  	public function mySchool();
  }

  class Teacher implements School{ // Using implements keyword to access School interface

  	public function __construct(){
          $this->mySchool();
  	}

  	public function mySchool()
  	{
  		echo "I am a school teacher";
  	}
  }

 $Teacher = new Teacher();

Look we override mySchool() method from Teacher class . Thus procedure we can use or implement of an interface’s method . Here we used constructor to auto load this method . Do you understand what i mean ? When we create a class and create an object of that class then a constructor is automatically loaded by compiler . So when we create a $Teacher object then in constructor $this->mySchool(); line will be automatically loaded .

Accessing Multiple Interface

 interface School{
  	public function School();
  }
  
  interface College{
    public function College();
  }

  interface Varsity{
    public function Varsity();
  }

  class Teacher implements School , College , Varsity{

  	public function __construct(){
       $this->School();
       $this->College();
       $this->Varsity();
  	}

  	public function School()
  	{
  		echo "I am a school teacher";
  	}
    public function College()
    {
      echo "I am a College teacher";
    }
    public function Varsity()
    {
      echo "I am a Varsity teacher";
    }
  }

  $Teacher = new Teacher();

Look here we used multiple interface and access multiple interface from a single class . Hope you got it.

Accessing from multiple class

interface School{
  	public function School();
  }
  
  interface College{
    public function College();
  }

  interface Varsity{
    public function Varsity();
  }

  class Teacher implements School , College , Varsity{

  	public function __construct(){
       $this->School();
       $this->College();
       $this->Varsity();
  	}

  	public function School()
  	{
  		echo "I am a school teacher";
  	}
    public function College()
    {
      echo "I am a College teacher";
    }
    public function Varsity()
    {
      echo "I am a Varsity teacher";
    }
  }
  class Student implements School , College , Varsity{

    public function __construct(){
       $this->School();
       $this->College();
       $this->Varsity();
    }

    public function School()
    {
      echo "I am a school Student";
    }
    public function College()
    {
      echo "I am a College Student";
    }
    public function Varsity()
    {
      echo "I am a Varsity Student";
    }
  }

  $Teacher = new Teacher();
  $Student = new Teacher();

Look here we accessed same multiple interfaces from different classes . So you can use multiple interfaces and access multiple or single interface from a single class . It is the best advantages of interface i think which is not possible for abstract class .

Note : In interface , you can just use public access specifier . You can just declare method which is not implementable in interface . We will implement it from another class using override . We can not create object of an interface . We can create constructor in interface . That’s it .

If you like this tutorial then please share with your friend and leave a comments . Best wishes for you .

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST