I recommend learning something like Doctrine to abstract your stuff with a db-to-object, other than for educational purposes.
Nevertheless, there are many ways to model this type of thing, but in general it seems that the libraries (homegrown or not) that process it tend to move to a high level:
- A class representing an object that maps to db
- A class that represents the way this object maps to db
- A class representing methods for extracting objects from db
Think about the different tasks that you need to complete, and try to encapsulate them cleanly. Demeter's law is useful to keep in mind, but not too bogged down, trying everything in the object-oriented theory of design this moment - it can be much more useful to think, design, code and see where weaknesses lie in your projects.
For your work with a single record, but without duplicating code problems, it is possible that something like that your loadAllFromUser methods are actually methods that call a private method that takes (for example) a parameter that is the number of records to write, where if this parameter is zero, it retrieves all records.
You can take this step further and implement __call in your loader class. Assuming that he can know or find out about the fields you want to load, you can build parameters for a function that performs software loading - look at the common parts of your functions, see what is different, and see if you can find a way to turn these different parts in functional parameters or something else that avoids repetition.
MVC is worth reading on your second question. At the very least, I probably want to have this in a separate class that expects a report to be rendered for rendering. The record probably should not care about how it is presented in html, what the markup for the record does not care about how the record is written. In general, you probably want to try to make things as autonomous as possible.
Itβs not easy to get used to, and most of the βgoodβ ones with this design are a matter of practice. For the actual functionality, tests can help a lot - let's say you write your loader class, and you know that if you call loadAllFromUser($me) , you should get an array of three specific records with your data set (even if it uses a data set for testing), if you have something that you can run that will call it on your bootloader and check the correct results, this can help you find out that your code is at least right in terms of behavior, if not design - and when you change the design, you You can make sure that he is still behaving correctly. PHPUnit is apparently the most popular tool for this in php-land.
Hope this points to a useful group of directions, not just confusion :) Good luck and goddess.