0

What is best way to achieve following:

class abc
{

 function xyz()
    {
      $PDO->Query(); //run any query
    }

}

Don't want to make connection on every PDO query/operation.

How can we make DB connection to achieve above, using public function, class or something else.

Thanks.

I need to update it.

Don't want to make connection in same class. Connection need to be imported from other class/function. This class abc must not make connection. DB connection come from other class or public function or any other good approach, thats exactly what I am looking for.

fmask
  • 481
  • 7
  • 18

3 Answers3

2
class abc
{
    private $PDO;

    function __construct($pdo)
    {
        $this->PDO = $pdo;
    }
    function xyz()
    {
        $this->PDO->Query(); //run any query
    }

}
$pdo = new PDO(...);
$foo = new abc($pdo);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I knew you'll answer this one, and was waiting to learn from it :) – Hanky Panky Oct 31 '13 at 05:57
  • What's the point in creating a class to simply connect to a database? Why not create the connection and store the reference in a global variable? :P – Pastor Bones Oct 31 '13 at 06:06
  • 1
    I think you misread this answer, There is no class being created here just to `connect to a database`. That class `abc` is not for database connection. In fact the database connection is being made outside it and is being passed to its constructor. – Hanky Panky Oct 31 '13 at 06:08
  • thanks for your answer, I want to know how to use external connection, don't want to make same class connection. – fmask Oct 31 '13 at 06:12
-1
// OOP Database Connection
class db{
  private $host;
  private $username;
  private $passwoord;
  private $dbname;

  protected function connect(){
   $this->host="localhost";
   $this->username="root";
   $this->password="";
   $this->dbname="database";
   $conn = new mysqli($this->host,$this->username,$this->password,$this->dbname);
   return $conn;
 }
}
-2

You can use a singleton pattern to persist the connection...

class DB {
    private $_conn = null;

    // Make private to prevent direct object creation
    private function __construct($config) {
        $this->_conn = new PDO("mysql:host=".$config["host"].";port=".$config["port"].";dbname=".$config["dbname"], $config["user"], $config["password"]);
    }

    public static function connect($config) {
        static $_instance = null;
        if($_instance == null){
            $_instance = new DB($config);
        }
        return $_instance;
    }

    public function query ($res) {
        $query = $this->_conn->prepare($res);
        $query->execute();
        return $query;
    }
}

It can be used like this to either create a PDO connection or grab an existing PDO connection:

$db = DB::connect($config);
$res = $db->query("SELECT * FROM table");
Pastor Bones
  • 7,183
  • 3
  • 36
  • 56
  • What's the point in calling connect every time you want a database? Why not already use it? $stmt = $DB::prepare()? And it seems you have little more too much configs here – Your Common Sense Oct 31 '13 at 06:02
  • Why use a registry pattern or global variable to create and hold a PDO connection object when it's not always necessary? – Pastor Bones Oct 31 '13 at 06:04
  • You don't need all this stuff with singleton. There is one powerful operator in PHP, namely `IF`. It can let you connect on demand, even without calling connect() excpicitly – Your Common Sense Oct 31 '13 at 07:00
  • Well, you can use it once more, to test if connection already opened and call connect() if not yet. you may wish also add another parameter to query() function, with data to bind. It will make this function usable. And still too much configs. Where $config is supposed to be taken from inside of abc class? – Your Common Sense Oct 31 '13 at 07:08
  • It was just an example I typed from memory...on the fly. Wanna critique my entire ORM library? (thx for the interest in instruction) – Pastor Bones Oct 31 '13 at 07:17
  • If it's really an *ORM*, not DB adstraction layer (which you posted here), a critique is simple: just try to run a query like `DELETE FROM cart LEFT JOIN users ON user_id WHERE user.status = ?` – Your Common Sense Oct 31 '13 at 07:28