Friends, repeatedly wondered and asked a question here, worried and haunted the thought about why we need interfaces in php for example.

There are heaps of examples where only that they explain are given a hard-typed structure for other classes that implement and are required to implement public methods, but the point is really only in this, surfing the articles on the interfaces were such opinions that they allow to bypass non-multiple inheritance, That is, as? Explain, please, an elementary example of communication, how does this happen?

Let's say there is an interface with one method. The classes that implement it implement it to fit their needs as they would like, but everywhere in different ways, is that all? Suppose we inherit from the class that implemented the interface, then what?

Update

<?php interface lol { public function game(); } class outlol implements lol { public function game() { return "outlol"; } } class inlol implements lol { public function game() { return "inlol"; } } class anon { public function you(inlol $inlol) { echo $inlol->game(); } public function to(outlol $outlol) { echo $outlol->game(); } } $obj = new anon(); $obj->you(new inlol()); echo "<br />"; $obj->to(new outlol()); echo "<br />"; 

Will return the appropriate lines, this is an example of the implementation of interfaces?

  • Profit in polymorphism, can start understanding it? - likerRr

2 answers 2

With interfaces you can solve many different problems in those languages ​​where there is no duck typing.

For example. You can declare an interface that will describe the output of the element on the screen. Next, write a few classes that implement this interface. Then create a common output function that will receive a pointer to the output class / object one of the parameters. If there were no interfaces, I had to inherit all of these classes from one common ancestor. And this is not always possible. And if you need to add such functionality to a third-party class, a successor is written very quickly with the necessary implementation.

The second plus of interfaces is a class that can implement many interfaces. This is true where there is no multiple inheritance.

The third plus of interfaces is interlingual communication (I really don’t know if it works in php, but it definitely works in C / C ++, Delphi, .NET). The interface can be described in one place, and implemented in a completely different language. And it does not interfere with its use. Windows itself actively uses interfaces.

  • I have just commented on the example just above, I think your answer is similar to my example - Archibaldo Spicentrosel

Consider the interface as a contract for a class to implement a number of methods. You need to look not from the side of the class that implements the interface, but from the classes that use it. If a class implements an interface, then other classes will be sure that this class has all the methods listed in the interface, and they can use them. An example is the interface Дверь . All you need to know a program that uses the Дверь is that it can be opened and closed. At the same time, it doesn’t matter at all whether it is a regular door, a double door or a bank safe. In this case, the fundamental difference from inheritance in this case is that the class that implements the Дверь interface can implement other interfaces, and even inherit from some class, while avoiding the problems of multiple inheritance.

Concerning multiple inheritance. Suppose we have classes A and B , in each of which the doSth() method is implemented. From them the class C inherited. This raises the problem: which method to call when calling C.doSth() - A.doSth() or B.doSth() ? After all, both methods were inherited, and now each time you call C.doSth() you need to explicitly indicate which method of ancestor class to use. It is quite another thing if A and B interfaces. If class C implements interfaces A and B , then it is guaranteed to have a doSth() method, while there will be no collisions, since methods cannot be implemented in interfaces.