0

I have a class used for database connection to mySQL databases, this class has a wide range of functions and is entirely my own work, I copy and paste it to new projects as they come up.

The class contains three private variables, for the datbase name, user and password.

Currently, these values are manually set in the class at construction time as:

class database {
    private $dbUser = "username";
    private $dbPass = "pass";
    private $dbName = "database";
    public  $dbLink = null; //the placeholder for the database object.

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

    private function dbConnect(){
        $this->dbLink = new mysqli("localhost", $this->dbUser, $this->dbPass, $this->dbName);

///simplified
        return true;
    }

}

However as I expand and edit my database class, and add new functionality and methods to the class, I am finding I want to update the class file across all the sites it runs on, each site has it's own database connection and log in so it's not as simple as copy/pasting or Save As... because I need to preserve the database connection details in each database class file.

So, What I would like to know is if there is an established "correct" way this should be done? if so, what is it, as I have found surprisingly little from Google and StackOverflow searches for importing data into a class from another file.

Possible ideas:

  • Have a base class that contains the connection details and then an extension class that contains all the methods etc. that can be easily updated without loosing the connection details.

  • saving the connection details in another file and importing them as a string using file_get_contents in the __construct method.

  • Use include() call(s) in the class to directly import variable values.

I was thinking of having another file which contained the connection details which can then be imported into the database class when the database class is constructed.

But I am wary, as I do not want the database connection details to be openly available, for example as a connection.txt file or something silly like that. Also I feel that making the class an extension of an empty original may be better overall but would be more work to get my current sites / database class objects into this new shape.

What way should I go about achieving a class using specific data that is not contained within the class itself. so I can continue to update and overwrite the class without worrying about overwriting database connection details?

updates/clarifiers

Passing details as params to the constructor does not solve the issue that I would need these params to be stored somewhere I the programmer can update them if needed but. I could pass them to the constructor from another file as mentioned, using file_get_contents.

connections: Each website will create an object for $dbLink from the class, the __construct will run once per page, per website.

5
  • How many places are the database connections called? Commented Apr 28, 2015 at 15:36
  • What about passing the details as params to the constructor and set the class vars there? Commented Apr 28, 2015 at 15:37
  • each website has it's own database class with website specific database connection details. I will update my question... Commented Apr 28, 2015 at 15:37
  • It's not a good idea to store the details hardcoded in such a generic class, since it's not very portable (like you have just found out). The class is instanciated somewhere, this is the point where you pass the details to the constructor. Commented Apr 28, 2015 at 15:40
  • yes I've found this, @chris--- what would you say is a good solution to this predicament? Commented Apr 28, 2015 at 15:41

3 Answers 3

2

There's two ways to do this. The first way is to pass the details in to the constructor, which is probably the best way if your connection is only called in one place.

class database {
    public $link;

    public function __construct($dbHost, $dbUser, $dbPass, $dbName)
    {
        $this->link = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
    }
}

$database = new database('localhost', 'username', 'password', 'database');

Normally you'd keep the connection details in a config file as variables and then pass in the variables.

The second way is, as you mentioned, extend the core class once per connection.

class database {
    public $link;
    protected $host;
    protected $user;
    protected $pass;
    protected $name;

    protected function connect()
    {
        $this->link = new mysqli($this->host, $this->user, $this->pass, $this->name);
    }
}

class my_connection extends database {
    public function __construct($host, $user, $pass, $name)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->name = $name;
        $this->connect();
    }
}

Either way is fine. There's no standard as such, just choose which way works best for you.

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

4 Comments

would there be any notable issues with passing details to the constructor from a file_get_contents routine that would open a file listing the username/dbname/password and applying them to the private variables?
Not at all, normally you'd use an include or require to load a config file that contains the variables.
Ahhh I'd considered using include/require and then forgotten about it as my mind turned towards file_get_contents. I think using the require will probably work for me . cheers
I decided in the end it was most efficient for me to use an include to add the variables directly into the class from an outside PHP file. Cheers for your help
0
  1. Create your shared library as a separate project.
  2. Push that project to a GIT repo somewhere (maybe BitBucket).
  3. Create a private composer package host (maybe using Toran Proxy).
  4. Add your GIT repo to your package host.
  5. Turn all of your projects into composer projects.
  6. Add your shared library to the dependencies of each project.
  7. Before you release your projects to production, run composer install.
  8. Set the database credentials to the environmental variables of each site.
  9. Read the environmental variables into your DB class.

This way you can easily share code between projects, and you keep your DB credentials out of the source code.

Comments

0

You could do;

<?php
require_once(dirname(__FILE__) . 'database.php');

However, a better approach is to set up a config file which includes information you require.

ie.

<?php
require_once(dirname(__FILE__) . '/path-to/config/config.inc.php');

Hope this helps.

UPDATE: added example gist here or checkout Laravel's (GitHub) file structure and configurations

Comments

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.