R
R
Romi2021-07-14 08:49:48
PHP
Romi, 2021-07-14 08:49:48

What is the practical meaning of using interfaces in PHP?

Let's say I don't use constructs like

function someFunc(SomeClass $someObj) 
{ 
...


and it turns out that for me the practical value of interfaces is zero?

I'm trying to understand the nuances of OOP now, and some things are not obvious.

What is the main point of interfaces?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Gennady S, 2021-07-21
@gscraft

You need to look towards the principles of SOLID, understand why these approaches are justified. Interfaces perform several tasks in connection with these principles.
First, when developing an application, starting with interfaces, the programmer is more focused on encapsulation, since he is more abstracted from the final implementation, from the way data is stored.
Second, the interface guarantees that the end programmer will implement a particular behavior. This is very widely applied. Take the same PSRs: thousands of libraries implement the same approaches, and all programmers can use the same libraries based on these interfaces without thinking about the final implementation of the classes.
Thirdly, a well-thought-out interface allows you to create more interchangeable implementations - final classes. Whoever uses an interface when designing a class is responsible for developing all the obligations of the interface.
Fourth, when interacting with software entities, relying on interfaces, the developer makes the entities even more replaceable (including using such patterns as dependency injection, which, however, can be used without interfaces).
Designing an application around interfaces is a good practice, although it is not necessary. If a small application is being written and there is an exact certainty that it will not expand and change further, or if teamwork is not expected, then interfaces can be neglected. If the application is large-scale, will be developed and maintained by several specialists, or it is expected to be further developed - interfaces are literally a must.
I will add, by the way, about dependency injection. Indeed, this approach can also be used without interfaces, the fact that inversion of control and dependency injection are used most often with interfaces is somewhat of a coincidence. A good example is the Yii 2 framework or the Pimple library, where arbitrary (or otherwise convention-based) strings are often used as markers for dependency injection. This is to say that DI is not necessarily the clearest example of using interfaces (although more valuable than other options).

D
Dmitry Kuznetsov, 2021-07-14
@dima9595

And where did you actually see the interface here?
An interface is a description of public methods, which are only the method name, a description of their arguments, and a return type. The body of the method in the interface is not described.
Those. you create a certain class in which certain methods will be predefined, which will be called in a mandatory (sort of) order. That is, if the interface has the needCall() method, then in the class where the interface is connected (via implements) you will need to call it.
https://www.php.net/manual/en/language.oop5.interf...

S
SagePtr, 2021-07-14
@SagePtr

For example, we can easily replace one class that implements this interface with another of the same class (provided that it correctly implements the same interface), without the need to nail a specific class to the program.
As in life - we have, for example, a PCI-E interface, there is also a PCI-E slot on the motherboard - we can plug in any device with a PCI-E interface - be it a video card, sound card or anything, without being tied specifically to video cards .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question