Support the ongoing development of Laravel.io →
Architecture

http://stackoverflow.com/questions/36547515/php-constructor-based-dependency-inject-vs-new-which-one-is-more-efficient

Referring to my above question. What is the consequence of 'new'ing a class or inject a class in Laravel Controller?

Ex:

    public class ArticleController extends Controller
	{
		protected $articleRepo;
		protected $userRepo;
		protected $documentRepo;

		public function __construct() {
			$this->articleRepo = new ArticleRepo();
			$this->userRepo = new UserRepo();
			$this->documentRepo = new DocumentRepo();
		}
	}


	public class ArticleController extends Controller
	{
		protected $articleRepo;
		protected $userRepo;
		protected $documentRepo;

		public function __construct(ArticleRepo $articleRepo, UserRepo $userRepo, DocumentRepo $d    ocumentRepo) {
			$this->articleRepo = $articleRepo;
			$this->userRepo = $userRepo;
			$this->documentRepo = $documentRepo
		}
	}

I know Dependency injection is the best approach, but my question what is the consequence of 'new'ing them instead of injecting them? 'new'ing does it mean consumes memory?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.