Site icon DED9

What is an Interface ?

In team programming, it is sometimes necessary for each group member to implement a specific class. Which usually has its standard for naming class functions and variables. This process causes confusion and illegibility in the code. To avoid this problem in Object-Oriented programming, a concept called Interface has been created, which we intend to explain below.

What is an interface?

The interface is a concept similar to a class, except that no method can be implemented. An interface is just a set of contracts in which different methods are defined without defining body and function. The class that implements this interface must use that function within itself and define it. Do not forget that each class can implement more than one interface. For example, in the following PHP code, we have implemented an interface called Shape:

interface  Shape  {
     public  function  getArea () ;
}

This means that from now on, every class that implements the Shape interface is required to implement the [tag] [ltr] getArea () [/ ltr] [/ tag] method. In the following code, the two classes, Square or Square and Rectangle or Rectangle, have implemented the Shape interface:

class  Square  implements  Shape  {
     private  $ width ;
    public  function  __construct ( $ width )
     {
         $ this -> width = $ width ;
    }
    public  function  getArea ()
     {
         return  $ this -> width * $ this -> width;
    }
}
class  Rectangle  implements  Shape  {
     private  $ width ;
    private  $ height ;
    public  function  __construct ( $ width , $ height )
     {
         $ this -> width = $ width ;
        $ this -> height = $ height ;
    }
    public  function  getArea ()
     {
         return  $ this -> width * $ this -> height;
    }
}

 

You may be wondering why we used the Interface in this example when we could easily have a class called Shape that gets the length and width of each shape in its constructor and then in the [tag] method [ ltr] getArea () [/ ltr] [/ tag]. In this case, it was enough for the other classes to inherit from it. And have all the methods and properties of Shape according to the law of inheritance. In that case, the Shape class and its child classes would be written as follows:

class  Shape  {
     private  $ width ;
    private  $ height ;
    public  function  __construct ( $ width , $ height )
     {
         $ this -> width = $ width ;
        $ this -> height = $ height ;
    }
    public  function  getArea () {
         return  $ this -> width * $ this -> height;
    }
}

Suppose we want to have a class called Circle in our set of classes of shapes. Unlike a rectangle and a square, the area of a circle is not based on its length and width but on its radius. [While the [tag] [ltr] getArea () [/ ltr] [/ tag] method is implemented in its parent class, Shape, based on length and width! So you see, the best option for this example was to use an interface. Using the Circle class interface, it is easily implemented as follows:

class  Circle  implements  Shape  {
     private  $ radius ;
    public  function  __construct ( $ radius )
     {
         $ this -> radius = $ radius ;
    }
    public  function  getArea ()
     {
         return  $ this -> radius * ( 3.14 ** 2 );
    }
}
$ circle = new Circle ( 2 );
echo  $ circle -> getArea ();

After implementing the Shape interface, if we do not implement all its methods in the class that used this, we will receive a fatal error, and the program will stop.

A practical example of Interface

The best way to implement a database connection in any program does not depend on the type of database and the type of connection implementation. With these sentences, you can probably easily understand the importance and application of this in this example. Using the interface, we require all implementers or database drivers, such as MySQL and SQLServer, to connect and query methods. Using the interface in this example, it no longer matters what method is used to connect to the database later. The method of accessing data is always the same!

 SQLDriver  interface {
     public  function  connect () ;
    public  function  query ( $ query ) ;
}
class  MySQL  implements  SQLDriver  {
     public  function  connect ()
     {
         // connecting to a mysql database
    }
    public  function  query ( $ query )
     {
         echo  $ query ;
        // query to a mysql database
    }
}
class  SQLServer  implements  SQLDriver  {
     public  function  connect ()
     {
         // connecting to a sqlServer database
    }
    public  function  query ( $ query )
     {
         // query to a sqlServer database
    }
}

Now to use the database, it is enough to have an interface class like [tag] DatabaseConsumer [/ tag] and pass the object of the database driver to it so that everything is done automatically:

class  DatabaseConsumer  {
     private  $ provider ;
    public  function  __construct ( SQLDriver $ sqlDriver )
     {
         $ this -> provider = $ sqlDriver ;
        $ this -> provider-> connect ();
    }
    public  function  query ( $ query ) {
         $ this -> provider-> query ( $ query );
    }
}
$ dc = new DatabaseConsumer ( new MySQL ());
$ dc -> query ( "SELECT * FROM` posts` " );

In the example above, in the constructor method or [tag] [ltr] __ construct () [/ ltr] [/ tag], the SQLDriver object, which is the driver object passed to DatabaseConsumer, is stored in the provider attribute of this class. By doing this, we now have access to all the public methods of the desired driver.

Now it is enough to start the database in the constructor method using the connect method of the desired driver. Other interface class methods also perform the requested operation by connecting to the appropriate methods in the given driver class. To use the database, create an object from the driver class and pass it to the DatabaseConsumer class constructor outside the class.

Conclusion

Like inheritance, the concept of Interface is one of the basic and important topics in learning object-oriented programming, which we hope we have helped you understand. Many people are looking to learn different languages ​​for various reasons, such as being interested or making money through programming. Do not forget that SevenLearn is always by your side in the path of learning programming.

Exit mobile version