β How complex can a constructor be?
docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards
en/articles/from-tg/architecture/constructor.md
new file +56
β0
1
+
# How complex can a constructor be?
2
+
3
+
Once, during the php-pro course, while I was talking about SOLID, one of the students asked:
4
+
5
+
> "How many dependencies in a constructor can be considered normal?
6
+
> Three? Four? Is five already bad?"
7
+
8
+
Good question. But the answer isn't really about "numbers".
9
+
10
+
There's a popular opinion that a constructor should be as simple as possible, because "otherwise it's a sign of an SRP violation".
11
+
12
+
I disagree. The mere fact of having many dependencies isn't necessarily bad: if your object has a clear responsibility but needs 6β8 dependencies to fulfill it, that's fine. The problem is when they don't fit together logically or the object tries to do too much.
13
+
14
+
But if the object coordinates a complex yet single business action, many dependencies are normal and even right.
15
+
16
+
```php
17
+
class OrderFinalizer
18
+
{
19
+
public function __construct(
20
+
private OrderRepository $orders,
21
+
private InventoryService $inventory,
22
+
private DiscountCalculator $discounts,
23
+
private TaxService $tax,
24
+
private PaymentGateway $payments,
25
+
private LoggerInterface $logger,
26
+
private EventDispatcherInterface $events,
27
+
) {}
28
+
}
29
+
```
30
+
31
+
Yes, there are 7 dependencies here, and all of them serve a single action β finalizing an order.
32
+
33
+
This is SRP in its purest form: a complex operation, but one responsibility.
34
+
35
+
If we start "smearing" this across factories or passing in 1 general-purpose service like `AppContext`, that's when it gets worse.
36
+
37
+
Because instead of an explicit contract you get opaque magic.
38
+
39
+
It's unclear what the class really needs β only by reading the code can you see what gets pulled out of the context.
40
+
41
+
This makes harder:
42
+
43
+
- refactoring (because the class looks like a black hole);
44
+
- testing (you mock everything because it's unclear what's actually used);
45
+
- understanding (a newcomer can't read the dependencies from the signature).
46
+
47
+
## Conclusion
48
+
49
+
> Don't be afraid to pass many dependencies if they are all needed.
50
+
> Be afraid of hiding dependencies in "convenient" wrappers like `AppContext`.
51
+
52
+
Code must be explicit. That is architecture.
53
+
54
+
---
55
+
56
+
*First published in Ukrainian in the Telegram channel [βCreated to dieβ](https://t.me/php_while_true/11) on June 19, 2025.*