Answer the question
In order to leave comments, you need to log in
What is the best way to resolve class dependencies?
There is a class "A" with a method "ab" (and many others). Method "ab" generates a result using methods of several classes, including methods of class "B".
What is the correct way to resolve dependencies of method "ab" on class "B"?
What methods I see (using Laravel as an example) and their disadvantages:
Answer the question
In order to leave comments, you need to log in
4. Pass B as method argument ab()
( function ab(B $b)
)
The first way is better. If the class becomes overgrown with dependencies, then you can think about refactoring. What if it doesn't overgrow?..
But no one will tell you more precisely without looking at the code - there is little data in the text of the question.
1) How often is the method called ab()
? In every request? or in one of hundreds of requests?
2) is it used B
elsewhere in the class A
?
3) How are dependencies passed through the constructor? new A(new B)
or $b = new B; new A($b); new A($b); new A($b);
or from container? After all, if it B
is already in the container, then passing it to the constructor is very simple and fast.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question