2

I was thinking about single responsibility principle, and I came up with this question. Let's say a class needs to be injected by another. Is it better to send second class into the first one's constructor as parameter, or just include it in the first one's constructor?

To explain my question I wrote this little code. In the first code, A requires B and creates an object of B in its constructor.

require_once('classb.php');

class A {

    protected $b;

    public function __construct(){
        $this->b = new B;
    }
}

In the second code, A takes B as a parameter and assigns it to internal variable $b in the constructor.

class A {

    protected $b;

    public function __construct(B $b){

        $this->b = $b;
    }

}

I know that one benefit of using second way, if A needs to get C also in some cases; instead of sending class itself to A's constructor an interface will act like reference of the class that is needed to be done.

On the other hand, if we are sure that there will be only one class coming inside A, using first one saves us time and lines to initialize B every single time.

The question is that if doing like the first way disturbs any best practice principles or has some down sides?

1
  • 1
    You can do both at once: start with the no-arg constructor, and if the need arises, add the second parameterized constructor. You can even combine them into one: ..__construct( B $b = null ) { $this->b = $b === null ? new B : $b; }.
    – Kenney
    Dec 16, 2015 at 21:25

3 Answers 3

0

If you doubt, you can make
public function __construct(B $b = null){ if (isset($b)) $this->b = $b; else $this->b = new B; }

0

It depends on what A and B are actually used for. If B is just an implementation detail that the user of class A doesn't need to know anything about, go with method 1. If the implementation ever changes (maybe changing class B for class C or even making B obsolete), the user of class A doesn't need to change his code.

If the user of class A needs to know about B (maybe B is an interface and there are multiple choices for it), go with method 2.

0

i would go with method two with a little modifications of-course

instead of type hinting with a class ,use interface ( abInterface for example). why? because it allows you to inject any concrete implementation of abInterface

program to an interface not implmenation

lets Assume class A is a service , assuming you have application class or index.php file that creates service A object,if you allow your application to pass object B when it creates service A's object, then you are basically enabling the application to choose any class (it can pass any implementation of abInterface).if you do this your service would be open for extension but close for modification (you don't have to modify class A's constructor if you want to inject for example class C instead of class B

open close principle

you can also use dependency injection containers to connect all your objects, that way you don't need to worry about managing the dependencies i'm not saying you need DI container for this particular scenario but incase if you want to do that, you can only do it if you go with method two

2
  • Thanks @wonde for nice solid explanation, but not an answer for my question. I stated same in the part starting with "I know that one benefit of using second way....". Do you see any downsides of the first method? If we are sure that there will be no class C, do you think using first way is a bad habit? Dec 16, 2015 at 23:40
  • well if you are 100% certain that there won't be any object going to be injected other than class B's object, then i would say go with option A but one thing that i learned from my software development career is, requirements will change , i would rather write more lines of code and make my application open for unforeseen changes/extensions.
    – wonde
    Dec 17, 2015 at 18:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.