如何创建/改进数据库PHP OOP类与所有/全部功能 [英] How to create/improve database PHP OOP class with all/full functions
问题描述
我是新的PHP OOP。下面是我的VERY FIRST类文件。我想为此代码添加更多灵活性,添加函数以便可以运行查询,甚至将结果(fetch_assoc / fetch_array)分配给数组(Or var,等)。
I am new to PHP OOP. Below is my VERY FIRST class file. I would like to add more flexibility to this code, add functions so I can run queries and even assign the results (either fetch_assoc/fetch_array) to an array (Or var, etc) for latter use.
我遇到的运行查询的问题是,我不能把这两个类(嵌套?): $ db-> Query-> Select('myTable');
OR $ db-& > Where($ pageID);
OR $ db-> Query-> Select('myTable') - >其中($ pageID) - > OrderBy 'name');
另外,如果你让我知道在这段代码中做错了,并建议改进,所以我可以写更好的php类在未来=)。
ALSO, I would appreciate if you let me know if there's something I am doing wrong in this code AND suggestions for improvements, so I can write better php classes in the future =).
<?php
require( $_SERVER['DOCUMENT_ROOT'] . '/database/database-connection-info.php');
//This file (database-connection-info.php) contains $config[];
class db {
private $db;
private $type = 'read-only';
//$config['db']['dbh'] = Database Host
//$config['db']['dbn'] = Database Name
//$config['db'][$type]['dbu'] = [User type][user name]
//$config['db'][$type]['dbp'] = [User type][password]
public function __construct($type = null) {
global $config;
$this->config = $config;
$this->Connect($type);
}
public function Connect($type) {
global $config;
switch( $type ) {
case 'admin':
$this->type = 'admin';
$this->db = mysql_connect( $this->config['db']['dbh'] , $this->config['db'][$type]['dbu'] , $this->config['db'][$type]['dbp'] );
return $this->db;
default:
$this->type = 'read-only';
$type = 'read';
$this->db = mysql_connect( $this->config['db']['dbh'] , $this->config['db'][$type]['dbu'] , $this->config['db'][$type]['dbp'] );
return $this->db;
}
}
public function Close() {
mysql_close();
mysql_close($this->db);
$this->type = 'Closed';
}
public function Type() {
return "<b>Database connection type</b>: " . $this->type . "<br/>";
}
public function Status() {
if( !@mysql_stat($this->db) )
{ return "<b>Database connection status</b>: There is no database connection open at this time.<br/>"; }
else { return "<b>Database connection status</b>: " . mysql_stat($this->db) . "<br/>"; }
}
}
//For testing purposes
$db = new db(admin);
echo $db->Type();
echo $db->Status();
echo $db->Close();
echo $db->Status();
echo $db->Type();
?>
推荐答案
两个重大改进:
-
不要使用全局配置,而是在构造函数中传递配置。
Don't use a global for the config, instead pass the config in the constructor. This allows you to instantiate db objects to other databases without having to hack a global.
停止使用原生的[depreacted] mysql_ *
库并切换到现代API,例如 PDO < a>或 MySQLi 。 这是一个很好的PDO教程,特别是在您的情况下,因为您来自本机 mysql _ *
透视。
Stop using the native [depreacted] mysql_*
library and switch to a modern API such as PDO or MySQLi. This is a good PDO tutorial, especially in your case because you are coming from the native mysql_*
perspective.
我也注意到了:
$db = new db(admin);
PHP会在这里寻找一个常数 admin
,它将无法找到它,并假设字符串 admin
是打算。如果你有一个定义为 admin
的常量,你会看到那里的奇怪的行为(或者你可能认为是字符串)。更改为字符串:
PHP will look for a constant admin
here, it will fail to find it and assume the string admin
was intended. If you ever had a constant defined as admin
then you would observe strange behavior there (or what you might think is string anyway). Change to a string:
$db = new db('admin');
这篇关于如何创建/改进数据库PHP OOP类与所有/全部功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!