Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To inject or to new?

With regards to using class objects within another class what is the best practice? To pass the class objects in the class _construct statement or create a new class object?

Example 1:

class Foo {
    private $bar;

    public function __construct($bar){
       $this->bar = $bar;
   }
}

Or Example 2 :

class Foo {

    private $bar;

    public function __construct(){
        $this->bar= NEW bar;
    }    
}

I'm aware that obviously it's taken for granted that the class file must already be included somewhere else, and in the first instance a class object of this type would need to exist already, but I want to know what the advantages are each method are, as I have a lot of classes I need to code that use a database object and I need the best way to pass this into the classes. Is there a third option that's better than these two?

From what I can understand, the advantage of the first one could be a few less lines of coding and in the case of a DB, not having a new connection created. The second one might be better however because it's more self contained? Anyhow I thought I'd ask the experts.

like image 992
user1770717 Avatar asked Oct 24 '12 09:10

user1770717


People also ask

What does it mean when you inject?

Injecting means administering drugs using a needle and syringe into a vein (intravenous), into a muscle (intramuscular), or under the skin (subcutaneous).

How do you use inject in a sentence?

Examples of inject in a SentenceShe told a few jokes to inject a little humor into her speech. We need to inject some life into this party.

What is the synonym of inject?

In this page you can discover 36 synonyms, antonyms, idiomatic expressions, and related words for inject, like: shoot, interpolate, throw-in, inoculate, insert, force into, place into, vaccinate, jab, shoot up and mainline.

What is the verb form of inject?

-ing form injecting. /ɪnˈdʒektɪŋ/ /ɪnˈdʒektɪŋ/ jump to other results. [transitive, intransitive] to put a drug or other substance into a person's or an animal's body using a syringe.


2 Answers

The first. (This approach is called Dependency Injection).

The constructor asks for whatever the object in questions needs in order to work. This way, it's pretty clear from the methods alone (what they need, and what they return), what it does. Without even looking at the source code.

A way to improve your code would be to introduce type hinting into your method:

class Foo {
    private $bar;

    public function __construct(Bar $bar){
       $this->bar = $bar;
   }
}

So that only Bar objects may be passed in.


Advantages of Dependency Injection

  • Very readable.
  • Ability to tell the method's dependencies without viewing the source code.
  • Makes Unit Testing possible.
  • *Saves kittens from God's wrath.

* Disclaimer: No kittens were harmed during the manifestation of this answer

like image 176
Madara's Ghost Avatar answered Sep 28 '22 00:09

Madara's Ghost


You should go for option 1, as this is the simplest form of dependency injection.

In option 1:

  • classes are independent of each other
  • classes can be tested independent, using a mock for the bar class
like image 31
JvdBerg Avatar answered Sep 27 '22 23:09

JvdBerg