By applying the SRP principle you are bound to have a lot of classes. If this works fine for a small project how can you handle and organize the amount of classes on a large project ?
This applies across all types of libraries. Not just SRP.
Organization of classes/functions can be a headache, but really there are only a few things you need to bear in mind.
By 2 I mean: /library/muscleGroup/useType_nameOfClass.php
for files/folders where useType is something like "factory" "abstract" "data / dto" or whatever patterns you are using. Then, in each file the class should be the exact same thing as nameOfClass and each method name should strictly follow a pattern. [Action][on what][with what conditions]
and keep a list of actions and 'on whats' and stick to them RELIGIOUSLY.
Do that, and you can't duplicate functionality, since you can easily find the right classes and methods for the things you want. Since they have logical names like Get_User_ById
and Get_Transactions_ByNewest
or Combine_Ingredients_FromRecipes
.
That last one might have a comment above it like:
// Combines many recipes into one ingredient list
// $recipes = an array of recipe objects
// returns an array of ingredient objects with their correct quantities
List of sample Actions: (should be pretty generic and apply to any application)
List of sample "On What"s: (should be application specific)
Depends on the project, but if it was something that has a lot of CRUD without much actual business rules, I would go for a structure that is simple, with script files to support that decision. I have done this in the past, and it is super fast, but slow to change. Typically called the Smart UI anti-pattern by Eric Evans:
"Put all the business logic into the user interface. Chop the application into small functions and implement them as separate user interfaces, embedding the business rules into them. Use a relational database as a shared repository of the data. Use the most automated UI building and visual programming tools available [Evans p.77]."
If you are making something with lots of real business rules and have more time, or you expect it to have a long life:
As for resources to help you further, check out Clean Code by Bob Martin, and Domain-Driven Design (can't post the other link) by Eric Evans. Both books are brilliant, and offer tactical and strategic steps, respectively, to tackling that chaos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With