0

I am just creating a simple standalone stock updater and turning my hand to OOP rather than just function based. I have created a simple class that requires my database connection but want to know the best practice for including and using the database connection. The first exert is the class that will be using the database. The second is the contents of 'dbconnector.php'.

class stockUpdater {

    var $data = '';

    function __construct() {
        include('dbconnector.php');
        print_r($mysqli);

        //check and get file
        $fp = fopen('stockfiles/stockfile.csv', 'r');
        if($fp){
            while(! feof($fp)) {
                $prods[] = fgetcsv($fp);
            }
        }

        if(empty($prods)){
            $this->logError(time(),'No data found in file');
        }
    }

    //error logging function
    private function logError($time, $message){

    }
}

$updateStock = new stockUpdater();

I Know what I'm doing here works but dont know if there's a better way of doing it than including the database connection script in every class as this could create multiple db connections when only 1 is needed.

$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}
Steve Smith
  • 654
  • 6
  • 14
  • 29
  • I think you should call the dbconnector when you instantiate the class. The idea of classes is meant to be universal - so everytime you need a different connection, you call different dbconnector and instantiating new object of the same class(es). – Royal Bg Apr 04 '13 at 11:04
  • Can you show me this as an example? Not 100% sure I get what you mean – Steve Smith Apr 04 '13 at 11:07
  • The simpliest example: `public function __construct($conn) { include (' ' . $conn . ' '); ...` ; `$conn = 'dbconnector.php'; $a = new stockUpdater($conn);` ... ; ... `$conn2 = 'dbconn2.php'; $b = new stockUpdater($conn2);` ... so instantiating object from stockUpdater() as $a will connect with dbconnector.php, but the same class instantiated on $b will connect with dbconn2.php – Royal Bg Apr 04 '13 at 11:08
  • You should do it by injecting dependencies. Kinda like [here](http://stackoverflow.com/a/11369679/727208). – tereško Apr 04 '13 at 11:21
  • But it only half solves the problem with different classes, as I read your post again. You can try different way. Make your dbconnector a class, which recieves params (host, user, pass, etc.) by constructor, and then a family tree of classes. The most main class extends your DbConnector Class, and each other extends the previous – Royal Bg Apr 04 '13 at 11:22

1 Answers1

1

The simplest way to fix this is to wrap around the $mysqli instance.

Define the StockUpdater class (it's recommended to uppercase the first letter of a class name)

class StockUpdater {
    private $mysqli;

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

    public function doStuff() {
        // do stuff with $this->mysqli
    }
}

And use it like

require_once('dbconnector.php');

$su = new StockUpdater($mysqli);
$su->doStuff();
Bart
  • 16,076
  • 5
  • 54
  • 79