0

Is it a good idea to instantiate the db connection in a parent class then its children classes can inherit by it?

For instance, this is my core model,

class CoreModel
{
    protected $connection;

    public function __construct() 
    {
        // Construct database connection data.
        $this->connection = new Database(DSN,DB_USER,DB_PASS);
        $this->connection->connect();
    }

}

then I have these classes that extend from CoreModel,

class Article extends CoreModel
{
    public function __construct()
    {
        parent::__construct();
    }
}

class Image extends CoreModel
{
    public function __construct()
    {
        parent::__construct();
    }
}

class Controller extends CoreModel
{
    public function __construct()
    {
        parent::__construct();
    }
}

Does it cause the db connection to be made endless times? so that would be 3 db connections in my case above?

If this is a bad idea, what is the base way to instantiate the db connection just once so that all the classes can depend on it?

EDIT:

$connection = new Database(DSN,DB_USER,DB_PASS);
$connection->connect();

$controller = new Controller($connection);
$article = new Article($connection);

class Article {

    protected $connection;

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

}
tereško
  • 56,151
  • 24
  • 92
  • 147
laukok
  • 47,545
  • 146
  • 388
  • 689

1 Answers1

2

Yes, it's a bad idea. The constructor code will be executed individually for each child, you'll have multiple database connections in the end. Beyond that, it's a bad idea to hardcode a certain class with a specific database connection. You should be dependency injecting the database connection into the constructor:

class Article {

    protected $connection;

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

}

Though it also seems like a bad idea to have a "model class" with direct database access. You should peruse this question: Looking for a way to handle/approach PHP OOP websites

Community
  • 1
  • 1
deceze
  • 471,072
  • 76
  • 664
  • 811
  • Thanks for the answer. Do you mean that I should do it in the method just as in my edit above? – laukok Jul 30 '14 at 13:39
  • also what does it for to have a prefix `Database` in `public function __construct(Database $connection){...}` – laukok Jul 30 '14 at 13:40
  • also what do you mean `hardcode a certain class with a specific database connection.` in `Beyond that, it's a bad idea to hardcode a certain class with a specific database connection.`? – laukok Jul 30 '14 at 13:46
  • No, you instantiate the `$connection` outside of `Article`, then pass it to `Article` when you construct it: `new Article($connection)`. And the `Database` in the function signature is a *type hint*, requiring a `Database` object. – deceze Jul 30 '14 at 13:47
  • 1
    I mean you're making your code inflexible by instantiating a database connection in its constructor. Please read [How Not To Kill Your Testability Using Statics](http://kunststube.net/static/), then read the other Q&A linked to in the question, then read up on *dependency injection*. It's not hard really, but all the thinking and reasoning can't be explained in one comment. – deceze Jul 30 '14 at 13:49