Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:32:47+00:00 2026-06-16T23:32:47+00:00

I’ve recently read that using the keyword new in a constructor is highly frowned

  • 0

I’ve recently read that using the keyword “new” in a constructor is highly frowned upon, but I’m not sure I understand why? For example, how is:

class A {
    public $foo;

    function __construct() {
        $this->foo = new Bar();
    }
}

Any different from:

class A {
    public function someMethod() {
        $foo = new Bar();
    }
}

???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:32:48+00:00Added an answer on June 16, 2026 at 11:32 pm

    This is really the theory behind dependency injection.

    It’s not that using “new” is a bad idea, per se. Rather, by instantiating objects inside of your class your are creating hard dependencies which can never be changed or switched out without changing the class itself.

    It also violates the paradigm of “coding to interface, not implementation”

    Example:

    class Phone {
        protected $network;
    
        public function __construct() {
            $this->network = new Verizon();
            $this->network->distinctiveRing();
        }
    }
    
    class Verizon {
        public function call($number) {
            ....
        }
    
        public function distinctiveRing() {
    
        }
    }
    

    Now, suppose one day you wanted to create an ATT, TMobile and Sprint phone? Surely they are all able to make calls, and can do so having just a phone number. Also, the phone class shouldn’t care who the carrier is, as it’s job is to facilitate entering a number – not to actually make a network connection, right?

    So, that being said, we shouldn’t have to create a new SprintPhone class that can instantiate another Sprint network object, right? Right.

    So what’s the better way?

    class Phone {
        protected $network;
    
        public function __construct(NetworkInterface $network) {
            $this->network = $network;
        }
    }
    
    interface NetworkInterface {
        public function call($number);
    }
    
    class Verizon implements NetworkInterface {
        ...
    }
    
    class Sprint implements NetworkInterface {
        ...
    }
    

    Well now, you can just say: $phone = new Phone(new Sprint()) or $phone = new Phone(new Verizon())

    Also notice that our call to distinctiveRing is gone. Why? Well, because we don’t know that any object implementing NetworkInterface will necessarily support a distinctive ring. But this is good because now our Phone can support ANY Network with no code changes.

    If you want support for distinctive ring, you can always create a new interface that supports the distinctiveRing method. In your Phone object, you can check if your Network implements DistinctiveRingerInterface and, if so, make your distinctive ring. But you’re no longer tied to a specific network with this approach. Better yet, you were forced to do this because you took the right approach from the start.

    And, any other network which can feasibly be created later down the road. More importantly, your class no longer has to care about what kind of Network object it’s given. It knows, (because it received an object implementing NetworkInterface), that the Network object is able to make a call with a $number.

    This also tends to lead to much better code, with a better separation of concerns.

    And finally: testing.

    With the first example, if you tried to test your Phone object, it was going to make a call on the Verizon network. Sucks to be the person getting called all day because you’re running unit tests, right? Right.

    Well, simply create a TestNetwork class that implements NetworkInterface, and pass that to your phone object. Your TestNetwork class can do anything you want inside of it’s call method – or do nothing.

    Furthermore, you can just create a mock object using PHPUnit, and ensure that the call method on your TestNetwork actually gets called. You couldn’t do this before because the Network was being instantiated inside of your Phone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.