This project demonstrates the SOLID principles of object-oriented design, which are essential for creating maintainable and scalable software. Each principle is implemented in its respective PHP file.
- Single Responsibility Principle (SRP):
-
"A class should have only one reason to change or have only one job/responsibility."
-
Example - In Single-Responsibility.php, the
Userclass has a single responsibility of representing a user and other classes, such asUserRepositoryandUserNotifier, focus on their specific tasks.
- Open/Closed Principle (OCP):
-
"A class should be open for extensions but closed for modifications."
-
Example - In Open-Closed.php, the
Shapeclass is open for extensions (adding new shapes) but closed for modifications (changing thebehavior(methods)of existing shapes).
- Liskov Substitution Principle (LSP):
-
"Child or derived classes should be able to substitute their parent or base class without breaking the program's behavior."
-
Example -
-
In Liskov-Substitution.php, the
Humanclass is an interface that can be defined with its child classFatherorChildto implement thespeak()method. Means child class should have all the abilities of parent class, if it can't do it, then it is against the Liskov Substitution Principle. -
We could've also implemented the
Fatherinterface forChildclass butFathercould have some features whichChildmay not have.
-
- Interface Segregation Principle (ISP):
-
"Classes should not be forced to implement methods they do not use/need."
-
Example -
-
In Interface-Segregation.php, If the
PrinterInterfacehaving thescanDocument()method, It is not required, so we need to implement a separate interface theScannerInterfacefor it. -
Accrodingly
MultiFunctionPrinter, SimplePrinter, SimpleScannerclasses can implement the desired interfaces.
-
- Dependency Inversion Principle (DIP):
-
"High-level classes should not depend on low-level classes but rather on abstractions."
-
Example -
-
In Dependency-Inversion.php,
UserServiceclass is a High level class that does not directly depends onMysqlDatabasewhich is a low level class. -
UserServiceclass has a dependency onDatabaseInterfacewhich is an abstraction ofMysqlDatabaseandPostgresqlDatabaseclasses.
-
To explore the implementation of each principle, refer to the corresponding PHP files in this directory. Each file contains examples and explanations of how the respective principle is applied.