1

I have db class which looks like that

class db {

    protected $db;

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

    protected function connect() {
        $this->db = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->db->error);
        $this->db->set_charset('utf8');
    }

}

Every class inside my PHP app extends this db class

Like

class registration extends db {

    var $validation;

    function __construct() {
        parent::__construct();
        $this->validation = new validation();
...

And validation looks like that

class validation extends db {

    var $ajax, $common;

    function __construct() {
        parent::__construct();
...

Getting error message "Too many connections". I feel that, this is not right approach: I'm every time reconnecting to db. So what's right way in your opinion? Is that possible to define('db', ...) 1 time and use everywhere inside app?

2 Answers 2

1

registration and validation are classes the use db but are not a sub-class of it.

Your code should look like:

$db = new DB();
$db->connect();
$registration = new Registration($db);

class Registration {

    private $db;

    public function __construct(DB $db) {
        $this->db = $db;
        ...

You pass a reference to an instance of $db to all classes that require it.

The reason you're opening too many connections is probably because currently each class makes it's own connection to your database, and that is not what you want to do, or need to do.

Sign up to request clarification or add additional context in comments.

1 Comment

@epic_syntax, yes, you can define the connection here as well. This solution is the same as mine except you pass the instance of the db class into all the other classes as they are instantiated in the application, whereas with Singleton each class can get an instance of the class itself, which will happen to the the same instance everywhere. This is perhaps an easier solution to understand if you're unfamiliar with design patterns, but I still encourage you to explore Singleton; database connectivity is a common use case for it.
0

You want to use composition here instead. Also might consider investigating Singleton pattern.

To elaborate, using composition, each class in your library will have an instance of the db class rather than be an instance of the db class.

Singleton will make the db class enforce only one instance of the class is ever created which is useful for shared resources like database connections. Have a look at this link for further reading on the topic.

http://php.net/manual/en/language.oop5.patterns.php

EDIT: Adding some code

Turning the db class into a Singleton

<?php
class db
{
    static private $_oInstance = null;
    protected $db;

    private function __construct()
    {
        $this->connect();
    }

    static public function getInstance()
    {
        if(self::$_oInstance === null)
            self::$_oInstance = new db();
        return self::$_oInstance();
    }

    protected function connect()
    {
        $this->db = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->db->error);
        $this->db->set_charset('utf8');
    }

}

Revising the rest of your classes to compose the db instance rather than extend the db class

class registration
{
    private $_oDb;
    public $validation;

    function __construct()
    {
        parent::__construct();
        $this->_oDb = db::getInstance();
        $this->validation = new validation();
    }

    // ...
}

3 Comments

yes, the db class is essentially unchanged from it's current design. you're simply changing how it is related to the rest of the code in your application.
A 'problem' with this approach is that any class has access to the database resource. You can not get a connection to a second database. And you can't have your code use a subclass of db. Dependency injection is generally considered to be a superior paradigm, especially in regard to modularity, maintenance and testing.
DI is also more complex and requires more boilerplate code in every class. Should be easy to support multiple database connections w/ singleton by parameterizing the constructor, maybe adding an array. DI is something I've found in practice to often be overkill, Singleton is more practical in most scenarios. When using design patterns one should be careful to choose a level of abstraction most suitable to the needs of the application. A pragmatic challenge of design patterns is choosing the right tool for the right job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.