Php 扩展MySQLi类
我希望能够创建扩展MySQLi类的类来执行其所有SQL查询Php 扩展MySQLi类,php,class,mysqli,Php,Class,Mysqli,我希望能够创建扩展MySQLi类的类来执行其所有SQL查询 $mysql = new mysqli('localhost', 'root', 'password', 'database') or die('error connecting to the database'); 我不知道如果不全球化$mysql对象以用于我的其他方法或类,如何做到这一点 class Blog { public function comment() { global $mysql; //rest
$mysql = new mysqli('localhost', 'root', 'password', 'database') or die('error connecting to the database');
我不知道如果不全球化$mysql对象以用于我的其他方法或类,如何做到这一点
class Blog {
public function comment() {
global $mysql;
//rest here
}
}
任何帮助都将不胜感激
谢谢。我的建议是创建一个单例DataAccess类,在全局配置文件中实例化该类,并在Blog类中调用它,如
$query=DataAccess::query(“SELECT*FROM Blog WHERE id=”.$id)
看看Singleton模式,它是一个非常容易理解的设计模式。非常适合这种情况
DataAccess类可以有几种方法,如query
,fetchAssoc
,numRows
,checkUniqueValue
,transactionStart
,transactionCommit
,transactionRollback
等。这些函数也可以设置为接口,由DataAccess类实现。这样,您就可以轻松地为多个数据库管理系统扩展DataAccess类
上面描述了我的数据访问模型。您可以对任何其他类使用PHP的extends关键字:
class MyCustomSql extends mysqli {
public function __construct($host, $user, $password, $database) {
parent::__construct($host, $user, $password, $database);
}
public function someOtherMethod() {
}
}
$sql = new MyCustomSql('localhost', 'root', 'password', 'database') or die('Cannot connect!');
或者最好使用对象聚合而不是继承:
class MySqlManipulator {
private $db;
public function __construct($host, $user, $password, $database) {
$this->db = new mysqli($host, $user, $password, $database);
}
public function someOtherMethod() {
return $this->db->query("SELECT * FROM blah_blah");
}
}
$mysqlmanipulator = new MySqlManipulator('localhost', 'root', 'password', 'database') or die('Cannot connect!');
我的标准方法是创建一个充当数据库访问器的类,以及一个所有需要此类访问的对象都从中继承的基类 因此: 请看一看,如果查询失败,将抛出哪些异常供您捕获。它被广泛使用和测试,所以在使用它的同时,您不应该在寻找现有解决方案时遇到问题 要将其注入blog类,请执行以下操作:
class Blog {
private $_db;
public function __construct(PDO $db) {
$this->_db = $db
}
public function comment() {
return $this->_db->query(/*something*/);
}
}
我在做类似的事情。我很高兴这个封装数据库登录的单例类
<?php
class db extends mysqli
{
protected static $instance;
protected static $options = array();
private function __construct() {
$o = self::$options;
// turn of error reporting
mysqli_report(MYSQLI_REPORT_OFF);
// connect to database
@parent::__construct(isset($o['host']) ? $o['host'] : 'localhost',
isset($o['user']) ? $o['user'] : 'root',
isset($o['pass']) ? $o['pass'] : '',
isset($o['dbname']) ? $o['dbname'] : 'world',
isset($o['port']) ? $o['port'] : 3306,
isset($o['sock']) ? $o['sock'] : false );
// check if a connection established
if( mysqli_connect_errno() ) {
throw new exception(mysqli_connect_error(), mysqli_connect_errno());
}
}
public static function getInstance() {
if( !self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public static function setOptions( array $opt ) {
self::$options = array_merge(self::$options, $opt);
}
public function query($query) {
if( !$this->real_query($query) ) {
throw new exception( $this->error, $this->errno );
}
$result = new mysqli_result($this);
return $result;
}
public function prepare($query) {
$stmt = new mysqli_stmt($this, $query);
return $stmt;
}
}
这是一个失败的解决方案,因为Blog绝不是mysqli类的扩展。它只是简单地使用它,而不是使用为数据访问提供的新功能来扩展它。很明显,博客本身不能是数据库对象的子类,我用来回答问题,而不是判断应用程序设计。如果我使用Singleton,它会在每次调用时创建到数据库的新连接吗?不,这是Singleton的想法。您创建了一个实例,并通过将其称为static来继续使用该实例。因此,所有类/控制器/视图等都有1db连接。我喜欢将这个类作为Singleton的一个示例,但会质疑是否需要覆盖query()
和prepare()
,因为mysqli
OO API已经提供了这些。
<?php
class db extends mysqli
{
protected static $instance;
protected static $options = array();
private function __construct() {
$o = self::$options;
// turn of error reporting
mysqli_report(MYSQLI_REPORT_OFF);
// connect to database
@parent::__construct(isset($o['host']) ? $o['host'] : 'localhost',
isset($o['user']) ? $o['user'] : 'root',
isset($o['pass']) ? $o['pass'] : '',
isset($o['dbname']) ? $o['dbname'] : 'world',
isset($o['port']) ? $o['port'] : 3306,
isset($o['sock']) ? $o['sock'] : false );
// check if a connection established
if( mysqli_connect_errno() ) {
throw new exception(mysqli_connect_error(), mysqli_connect_errno());
}
}
public static function getInstance() {
if( !self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public static function setOptions( array $opt ) {
self::$options = array_merge(self::$options, $opt);
}
public function query($query) {
if( !$this->real_query($query) ) {
throw new exception( $this->error, $this->errno );
}
$result = new mysqli_result($this);
return $result;
}
public function prepare($query) {
$stmt = new mysqli_stmt($this, $query);
return $stmt;
}
}
<?php
require "db.class.php";
$sql = db::getInstance();
$result = $sql->query("select * from city");
/* Fetch the results of the query */
while( $row = $result->fetch_assoc() ){
printf("%s (%s)\n", $row['Name'], $row['Population']);
}
?>