Dependency injection is an exceptional context, but I think sometimes itβs overkill.
Why introduce an additional method or redefine the constructor if the only thing you need in your example is to know if the current user can swim?
So IMMO, if the use case is simple, you can very well go for the second option that you have provided and omit the dependency only on the value on which it depends:
goSwimming($sf_user->can_swim);
Of course, it all depends on the actual concepts used and on how extended one class is from another.
If this is only this case (the result depends on user negligence to swim), than this is easy to simplify. But if the called method / class can ultimately use more attributes than this, it is probably best to go with the Injection Dependency technology that has already been discussed. UserTable probably depends on sfUser for other things, so this seems like a complete class dependency.
source share