S
S
Sergei Abramov2020-01-17 17:01:17
PHP
Sergei Abramov, 2020-01-17 17:01:17

Why use an interface?

Almost always it turns out that I use an interface and classes that implement it. But when suddenly there is a need for an abstract class, it immediately becomes blunt to what to apply the interface to: to the implementing class:

class FooService extends AbstractFooService implements FooServiceInterface {}

or in this case already to an abstract class:
abstract class AbstractFooService implements FooServiceInterface {}

class FooService extends AbstractFooService {}

On the basis of what to make a choice? I still like the first option. But I don't have any for or against.
In general, of course, I try to avoid abstract classes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cheypnow, 2020-01-17
@Cheypnow

An interface describes a contract.
An abstract class describes the basic behavior that is common to multiple descendants.
What is the problem?

D
Dmitry, 2020-01-17
@Compolomus

Do not be afraid of abstract classes, it can be very convenient, the interface does not contain an implementation, it cannot have properties, only constants. Also, the interface has only public methods, when an abstract class can contain some private methods, where the general logic will be contained.
I am for the second option

K
Konstantin Zhikhor, 2020-01-17
@shevchenko__k

the first option is correct. I don't understand what is the problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question