Answer the question
In order to leave comments, you need to log in
When to use interfaces in PHP and when to use abstract classes?
I just want to figure out what and when to use it.
Just do not write messages like: "Interface is an interface ... And an abstract class is an abstract class. Everything is simple." So they write everywhere, without giving a specific answer.
It is desirable on a concrete example when it is better to use one, and when another.
Answer the question
In order to leave comments, you need to log in
From an architectural point of view:
An interface describes properties. Pay attention to the classic interface names: Throwable, Countable, Comparable, Iterable, etc. Take, for example, the interface Rollable (rolling), and Foldable (folding).
An abstract class describes an entity. For example, table: Table_Abstract. The table can be wooden, in which case Table_Wood extends Table_Abstract. Also, the table can be surgical: Table_Surgical extends Table_Abstract. In this case, Table_Abstract combines the common properties of all tables (say, surface area, presence of legs, etc.). A concrete class describes the essence of a certain type of tables.
Communication of interfaces and classes you describe properties. For example, a table can be rolled: Table_Abstract implements Rollable. A wooden table, for example, can be folded: Table_Wood implements Foldable.
As smart people (Schildt, Troelsen) write in their smart books, the interface defines functionality, behavior - “what exactly should be done, but not how to do it” (G. Schildt, The Complete Guide to C#). In an abstract class, "only the most general form is defined for all its derived classes, and the details of it are left to each of these classes" (ibid.).
A simple example, in the context of a graphical editor, you can define:
Abstract class - Figure (geometric figure), classes of specific shapes can be formed from it - for example, Rectangle, Circle, etc.
Interface - Drawable (what you can draw). It can be implemented both in all classes of concrete figures (Circle, Rectangle), and in other classes that are not formed from the abstract Figure.
I would say this:
An interface is an opportunity to set rigid semantics.
An abstract class is an opportunity to take out duplicate code and clearly mark it in the hierarchy.
For example, you need to write a class to work with the cache.
We have a Cache class that will do all the dirty work. He, in turn, will use the library for a certain type of cache (memcached, eaccelerator, ...). For consistency, the library must implement the cacheInterface interface so that the Cache class can work properly. Here is a small example.
Interface for the library:
Interfaces should be used when classes that are supposed to provide the same interface should not (or cannot be) hierarchically related.
Also, if you need to provide multiple interfaces and you don't have multiple inheritance (as in PHP), then interfaces are the only way to go.
An interface represents a set of function signatures that must be implemented during implementation. it's not a class, it's a separate entity. the implementation of methods in specific classes can be completely different. only the method signature is shared. an interface cannot have properties (fields, constants).
an abstract class assumes the presence of both signatures and some default implementation for some methods (as mentioned above, this is the ability to take out duplicate code). it is used during inheritance, in child classes you can override methods, or you can leave the implementation of the abstract class itself. An abstract class defines common behavior for objects of the same type, as opposed to an interface that can be used in classes of different unrelated objects.
you cannot initialize an object of an abstract class - this is different from using a regular class for inheritance.
accordingly, you need to take into account that when adding a new method signature to an interface, it will have to be implemented in all classes that use this interface. in an abstract class, you can implement the general default behavior for child classes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question