在类上下文中使用新PDO的正确方法是什么? [英] What's the correct way to use a new PDO in a class context?
问题描述
过去,我一直创建一个数据库类,并在该类中为$ connection属性分配了__construct方法中通过mysql_connect进行的连接.在同一文件中,我将实例化该类,以使其准备就绪.然后,每当我需要该连接时,我只需要该数据库文件,并在需要$ connection的方法中添加一个全局$ connection.使用php的PDO实现相似或更好的最佳方法是什么?
In the past, I've always created a database class and in that class assigned a $connection attribute the connection via mysql_connect in a __construct method. In the same file, I would instantiate the class so that it was ready to go. Then whenever I needed that connection I would simply require that database file and add a global $connection in the method that need the $connection. What is the best way to achieve something similar or better using php's PDO?
推荐答案
我相信没有最佳"方法来实现您的要求,但这是我使用的方法. execute函数是根据我的需要设计的,您可以根据需要进行更改.
I believe that there's no "Best" way to achieve what you ask but here is what I use. The function execute was designed according to my needs, you may change it however you want.
**编辑**
顺便说一句,由于我从不同文件中多次调用该类,因此我在此类中使用了单例方法.因此,您也可以更改它.
By the way, I am using a singleton method in this class since I call the class several times from different files. Therefore, you may change that as well.
class DB
{
/* Connection settings */
private static $host = 'localhost';
private static $user = 'root';
private static $pass = 'your_pass';
private static $base = 'your_db';
private static $ins; // pdo instance
private static $class; // class object for singleton
public static $counter; // counts how many times execute is called
public function __construct()
{
}
public static function connect($errMode = PDO::ERRMODE_SILENT)
{
if (!isset(self::$ins))
{
try
{
self::$ins = new PDO("mysql:host=" . self::$host . ";" ."dbname=" . self::$base . ";", self::$user, self::$pass);
self::$ins->setAttribute(PDO::ATTR_ERRMODE, $errMode);
// PDO::ERRMODE_EXCEPTION
}
catch (Excpetion $ex)
{
self::raiseError($ex);
}
$className = __CLASS__;
self::$class = new $className;
}
return self::$class;
}
/**
* Function to execute a given query
* @param string $query : query string
* @param string | array $param : parameter (either array of parameters or string)
* @param bool $useBind : if true bindParam method will be used, else execute method will be called
* @return PDOStatement
*/
public function execute($query, $param, $useBind=true)
{
self::$counter++;
$stmt = self::$ins->prepare($query);
if (!$useBind)
$stmt->execute($param);
else
{
if (is_array($param))
{
$size = sizeof($param);
// if items within param param are array (e.g. array(array(value, name, type, length),
// array(value, name, type, length))
if ($size >= 1 && is_array($param[0]))
{
$i = 1; // ? placeholder counter
foreach ($param as $arr)
{
$size = sizeof($arr);
if ($size == 1) // e.g. array('red')
$stmt->bindParam($i, $arr[0]);
else if ($size == 2) // e.g. array(':color', 'red')
$stmt->bindParam($arr[0], $arr[1]);
else if ($size == 3) // e.g. array(':color', 'red', PDO::PARAM_STR)
$stmt->bindParam($arr[0], $arr[1], $arr[2]);
else // e.g. array(':color', 'red', PDO::PARAM_STR, 12)
$stmt->bindParam($arr[0], $arr[1], $arr[2], $arr[3]);
$i++;
}
}
else if ($size == 1) // e.g. array(15)
$stmt->bindParam(1, $param[0]);
else if ($size == 2) // e.g. array(':color', 'red')
$stmt->bindParam($param[0], $param[1]);
else if ($size == 3) // e.g. array(':color', 'red', PDO::PARAM_STR)
$stmt->bindParam($param[0], $param[1], $param[2]);
else if ($size == 4) // e.g. array(':color', 'red', PDO::PARAM_STR, 12)
$stmt->bindParam($param[0], $param[1], $param[2], $param[3]);
}
else // e.g. $db::execute($query, 15)
$stmt->bindParam(1, $param);
$stmt->execute();
}
return $stmt;
}
public function query($query)
{
self::$counter++;
$result = self::$ins->query($query);
return $result;
}
public function close()
{
self::$ins = null;
}
public function lastId()
{
$id = self::$ins->lastInsertId();
return $id;
}
public function trans()
{
self::$ins->beginTransaction();
}
public function commit()
{
self::$ins->commit();
}
public function rollBack()
{
self::$ins->rollBack();
}
private function raiseError($er)
{
throw new Exception($er);
}
public function counter()
{
return self::$counter;
}
}
这篇关于在类上下文中使用新PDO的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!