Martin
Martin

Reputation: 22760

PHP importing data into a class

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:

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.

Upvotes: 0

Views: 729

Answers (3)

daxeh
daxeh

Reputation: 1085

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

Upvotes: 0

Styphon
Styphon

Reputation: 10447

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.

Upvotes: 2

Flosculus
Flosculus

Reputation: 6946

  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.

Upvotes: 0

Related Questions