Answer the question
In order to leave comments, you need to log in
Which style of OOP should I choose?
Welcome all.
I’ll make a reservation right away, I’m not sure that what I’m asking about is called style.
Below are 3 examples in PHP, I'm interested in which implementation of the class is preferable for what purpose or in what context, and is any of the following bad / good form?
$id = 1;
// пример 1
$class = new SomeClass();
$result = $class->setItem($id)->doSomething();
// пример 2
$class = new SomeClass();
$result = $class->doSomething($id);
// пример 3
$class = new SomeClass($id);
$result = $class->doSomething();
Answer the question
In order to leave comments, you need to log in
example 2 and only
I'll explain. For example,
SomeClass
is a logging service.
Then we will make an interface for it, create 2 subtypes - a logger on files and in the database.
Total 1 interface, 2 implementation classes.
Method parameters are described in the interface..
So we have that the method will be called with a specific argument...
In other cases, the behavior of subclasses that will implement this is not clear.. Namely, in terms of what setItem should do in 1 case, and essno interfaces cannot describe constructors as in example 3 ...
In total, we have that you can easily replace an object with another object ONLY in example 2.
As for getters - IMHO, it's better to create private properties and a getter + setter
Because on large projects, most often it turns out to be noodles, and in terms of multithreading, and again, interfaces, just properties are not good ...
1. Looks like a "pie recipe". To bake a cake - do a, b, c, .... and put it in the oven.
Cons: You need to know the recipe, what ingredients can be used, what order to put them in, etc. This will require the study of manuals.
2. Can be used if the method parameter is some kind of "refinement" for the method, and not its dependency.
3. I use this one. Small classes, easy to test and extend. In the constructor, you must specify the type for the parameter. Then cooking the pie comes down to providing certain dependencies, and the "baking" process itself (calling the method).
In general, forget about the extra word get - it's overkill.
1) Everything is OK, the setItem method theoretically can return the newly added item, it all depends on how the class is used. I don't know if this is possible in PHP, but the method can, in principle, return this or a reference to itself. What can be used to implement the builder pattern, for example.
2) Bad. The class must return the stored item, it does not need to know about its internal representation about some of its methods or fields. They returned the item, then ask him for the data.
3) I did not understand the third example. Yes, in general, the class has some data, then yes, it can return them. It's not a problem.
PS What you are asking about is called OOD. It has little to do with the programming language.
PPS Next time, ask questions about a more specific model. A couple of simple classes describing, well, there, a bookshelf, for example, on which you can put a book and pick it up. Your example is too abstract.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question