thePHP.cc Logo Deutsch Contact
mood

SOLID

The acronym SOLID describes five principles whose application significantly increases the maintainability of software. The five principles are:

These principles have been propagated since the turn of the millennium by "Uncle Bob" Robert C. Martin. Michael Feathers later invented the acronym SOLID.

The Single Responsibility Principle states that each class (and method) should have exactly one responsibility. This principle must not be interpreted too strictly, but the concept of responsibility must be seen from a certain "flight level". Otherwise you would end up with objects that always have only one method with exactly one line of code.

Another very useful way of looking at the single responsibility principle is that there should be exactly one reason to change a class. If there are multiple reasons, then the class has too many responsibilities.

The Open/Closed Principle states that a class should be open for extensions but closed for modifications. From this principle we can gain many insights regarding the visibility of properties and methods and the use of inheritance in general: minimal visibility, all classes final, inherit only from abstract base classes, and consistently mark all public methods there as final.

Liskov's Substitution Principle demands that one must be able to replace objects with derived classes at any time. This principle results in many restrictions regarding modified method signatures in inheritance hierarchies, which in PHP are fortunately so embedded in the programming language that it becomes difficult to violate Liskov's substitution principle in PHP at all. If you think about the behaviour of objects and thus also about the return values or possibly thrown exceptions, you can definitely make mistakes in PHP that limit substitutability.

The Interface Segregation Principle states that you should not depend on interfaces that you do not use. So instead of defining one big interface with many methods, you should define and implement several smaller and coherent interfaces. Since version 8.1, the type system of PHP offers interesting possibilities to implement the interface segregation principle even more consistently with Union Types and Intersection Types.

The Dependency Inversion Principle , not to be confused with Dependency Injection, is perhaps the most important of the five SOLID principles. It basically motivates a hexagonal architecture, but it can also be applied on a small scale and used as a rationale for introducing value objects. For example, it is better to depend on an abstraction introduced by a value object than on implementation details such as scalar types.

Presentation The Five Rules of PHP Craftsmanship More presentations
Presentation SOLID MVC More presentations
Article Unfortunately we have no framework More articles