5

I was reading a blog post when I came across this code:

<?php
include_once 'config.php';
class User
{
//Database connect 
public function __construct() 
{
$db = new DB_Class();
}

In the comments someone posted the following:

NEVER INITIATE db connection in the constructor

But as with all comment warriors they never give a reason why? Why is this wrong or a bad practice to do?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
twigg
  • 3,753
  • 13
  • 54
  • 96
  • This has been [asked before on CodeReview](http://codereview.stackexchange.com/questions/5732/mysql-database-connection-in-the-constructor) – Robin Kanters Jan 03 '13 at 12:10

4 Answers4

8

Constructors should not do any real work:

Work in the constructor such as: creating/initializing collaborators, communicating with other services, and logic to set up its own state removes seams needed for testing, forcing subclasses/mocks to inherit unwanted behavior. Too much work in the constructor prevents instantiation or altering collaborators in the test.

Instead, use Dependency Injection and pass any collaborators to the constructor. This will create code that is easier to test. When you new something in the ctor, it is much more difficult to mock/stub that with a Test Double.

Also, by injecting collaborators, you are making it easier to swap out collaborators with different implementations, thus reducing coupling, fostering code reuse and coding towards interfaces instead of concrete implementations.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
1

Passing instance of the Data Base to the constructor called "dependency injection". This is done for decoupling of objects, meaning lowering the dependencies between objects. One of the benefits of decoupling is greater flexibility in the system, easier maintenance, and writing unit tests.

But you can enjoy all of this benefits mainly if you build your software using design patterns, practicing test driven development, and well defined architecture - as for example practiced in domain driven design.

But if you only starting to explore the OOP way of developing software or just starting to learn programming in general - maybe you shouldn't bother yourself with these kind of questions just yet.

Vlad Lyga
  • 1,133
  • 9
  • 10
0

I would argue that initializing a DB connection in a constructor violates the general contract for a constructor. When you do new User() you expect to get an object describing a user. If this required a working database connection what is the state of the object if establishing the connection failed? A constructor should not throw an exception, because it's only purpose is to ensure the proper object state. (At least that's my view...I know other people don't think exceptions from constructors are bad. However, I do.)

And furthermore: You can't reuse the database connection if it's encapsulated within an object.

Till Helge
  • 9,253
  • 2
  • 40
  • 56
0

In this special case, perhaps a model for working with user data, it can be very useful. But what if ou suddenly want this class to be portable and work with a different database? I have run into this myself when I want to use my user manager class with a different project.

Doug Wolfgram
  • 2,064
  • 4
  • 27
  • 42