[英]How to create/improve database PHP OOP class with all/full functions
I am new to PHP OOP. 我是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.
我想为此代码添加更多灵活性 ,添加函数以便我可以运行查询 ,甚至将结果(fetch_assoc / fetch_array)分配给数组(或var等)供以后使用。
The problem I am having with running queries, is that I am not able to put together both classes (nesting?): $db->Query->Select('myTable');
我在运行查询时遇到的问题是,我无法将两个类放在一起(嵌套?):
$db->Query->Select('myTable');
OR $db->Query->Select('myTable')->Where($pageID);
OR
$db->Query->Select('myTable')->Where($pageID);
OR $db->Query->Select('myTable')->Where($pageID)->OrderBy('name');
OR
$db->Query->Select('myTable')->Where($pageID)->OrderBy('name');
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类=)。
<?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();
?>
Two significant improvements: 两项重大改进:
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.
这允许您将db对象实例化到其他数据库,而不必破解全局。
Stop using the native [depreacted] mysql_*
library and switch to a modern API such as PDO or MySQLi . 停止使用本机[depreacted]
mysql_*
库并切换到现代API,如PDO或MySQLi 。 This is a good PDO tutorial , especially in your case because you are coming from the native mysql_*
perspective. 这是一个很好的PDO教程 ,特别是在你的情况下,因为你来自本机
mysql_*
透视图。
I also noticed this: 我也注意到了这一点:
$db = new db(admin);
PHP will look for a constant admin
here, it will fail to find it and assume the string admin
was intended. PHP将在这里寻找一个常量
admin
,它将无法找到它并假设字符串admin
是有意的。 If you ever had a constant defined as admin
then you would observe strange behavior there (or what you might think is string anyway). 如果你有一个常量定义为
admin
那么你会在那里观察到奇怪的行为(或者你可能认为是字符串无论如何)。 Change to a string: 更改为字符串:
$db = new db('admin');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.