Answer the question
In order to leave comments, you need to log in
[Newbie] PHP OOP had doubts. What to do and how to be?
Hello developers!
I've been programming in PHP for quite some time, but some time ago I tried Java and then C# and was in awe of OOP and strong typing. That's why I decided to start "pulling up" knowledge, I decided, so to speak, to try OOP in PHP. And it seems like it even turns out, I kind of understand the basic concepts of OOP, the patterns are the same, but over time, questions began to appear with whether I was doing it right or not, I don’t have any gurus I know, so I decided to try with the toaster community.
1) Is this statement correct "you should not apply patterns absolutely everywhere where it smells of the slightest OOP"?
2) Is it correct to create a separate Comment, Post, User or File object for each elementary entity, be it a comment, a post or a user, which will contain any information for use in the handler. For example User as an argument to the AuthHandler constructor or File as an argument to one of the ImageHandler methods?
3) What is the maximum cost of using arguments for the constructor and for methods (to make it readable and "correct")?
4) Is it correct to use the database manager -> elementary entity for each such entity: For example: class UserManager extends Db and class User (the constructor accepts UserManager and stuffs data into fields. Or the File entity itself into which we can stuff binary data (we are not talking about the database here)
Answer the question
In order to leave comments, you need to log in
1. Patterns, like any other thing in programming, should be used only where it is required, and not absolutely everywhere. For each task, you need to select your own tool
2. Yes, this is a normal practice. But here you need to understand how you will work with them
3. I think no more than 3-4, otherwise it will not be very readable and convenient to work with.
4. No. To work with the database, you must have a separate class to implement. And already the entity should use it to receive data
1. Patterns are just examples of solving common problems. If you know a lot of patterns, then most likely you will not reinvent the wheel where it is not necessary. But you can always act according to the situation, because standard methods do not cover all requirements.
2. Depending on how you will work with them. If the entities are too elementary, a common array object is also possible
3. It's hard to say. If it seems to you that there is a lot - think about whether it is possible to pass an object as an argument at once. And does it make sense to create a new class for this. Sometimes it is enough to make enough constructors, and not be able to specify all the arguments.
4. Connection to the base must be one module.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question