喜欢面向对象的进来,看看我这个类,怎样写会更好,更容易让别人看懂,也更容易用于开发

看小雪 2014-03-01 06:31:38
喜欢面向对象的进来,看看我这个类,怎样写会更好,更容易让别人看懂,也更容易用于开发。此类为数据库操作的类,只用于PDO且数据库为sqlite。当然,高手也可以改进下。

前景:以前我怕sqlite数据库出错,所以用了两个数据库,因此类里面有传数据库的地址。也算是我前期设计的一个失误吧。当然,能设计成静态调用更好。
<?php
/*
此版本为utf-8(无BOM)版本,因为GBK编码在SQLITE3下处理不当会出现乱码。
延续旧版本部分功能
*/

class WEB {
private $conn ;
private $stmt ;
public function __construct() {
$this->CheckURL();
}
public function __destruct(){
$this->conn=null;
$this->stmt=null;
}
public function CheckURL() {
$BadUrl = "'|and|create|delete|select|update|union|;|*|(|)";
$url = empty($_SERVER["QUERY_STRING"])?"":$_SERVER["QUERY_STRING"];
if(!empty($url)) {
$arr = explode($BadUrl,"|");
foreach($arr as $v) {
if(stripos($url,$v)>0) {
$this->CatchError("URL含有敏感字符");
exit;
}
}
}
}

public function CatchError($str='') {
echo '<fieldset style="width:350px;padding: 3px;"><legend> 错误描述 </legend><br> 捕捉到错误,程序结束。 <p><font style="color:#ff0000;font-size:12px;">',$str, '</font></p><a href="http://'.$_SERVER['SERVER_NAME'].'" target="_blank" title="迷茫时代技术支持" style="margin: 3px 0px 2px 2px;color:#000;text-decoration: underline;">迷茫时代</a></fieldset>';
exit();
}
//数据库操作区-------------------------------------
public function ConnectDB($str) {
if(!isset($str) || empty($str)) $this->CatchError("连接数据库字符串不能为空");
if(!is_file($str)) $this->CatchError("没有此文件$str");
$this->conn = new PDO('sqlite:'.$str) or $this->CatchError('连接数据库错误');

return $this->conn;
}


public function lastInsertId(){
return $this->conn->lastInsertId();
}

public function query($str1,$db,$str2='',$str3='') {
if(empty($str1)) $this->CatchError('查询数据库错误');
if(!empty($str2)) {
$str2 = $this->SQLParam($str2);
$str1 = $str1.$str2;
}
if(!empty($str3)) $str1 = $str1.$str2.$str3;
if(!$this->conn) $this->conn = $this->ConnectDB($db);

$err = $this->conn->errorInfo();
$result = $this->conn->query($str1) or $this->CatchError("查询出错PDO<br>".$err[2]);
$result->setFetchMode(PDO::FETCH_ASSOC);
return $result;
}
public function querySingle($str1,$db,$str2=1) {
if(empty($str1) or empty($db)) $this->CatchError('SQL is empty!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
$result = $this->conn->query($str1);
if($str2==1) {
$row = $result->fetch(PDO::FETCH_ASSOC);
return $row;
}else{
$row = $result->fetch(PDO::FETCH_NUM);
return $row[0];
}
}
public function exec($str1,$db) {
if(empty($str1)) $this->CatchError('SQL is empty!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
return $this->conn->exec($str1);
}
public function prepare($sql,$db) {
if(empty($sql)) $this->CatchError('prepare need SQL!');
if(!$this->conn) $this->conn = $this->ConnectDB($db);
$this->stmt = $this->conn->prepare($sql);
}
public function bindValue($name,$value,$type) {
if($type == 'TEXT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_STR);
}else if($type == 'INT') {
$this->stmt->bindValue($name,$value,PDO::PARAM_INT);
}
}
public function execute() {
$this->stmt->execute();
$this->stmt->closeCursor();
}
public function errorInfo() {
$arr = $this->stmt->errorInfo();
$this->CatchError($arr[2]);
$arr = NULL;
}
public function fetchColumn($int) {
if(!is_numeric($int)) $this->CatchError('fetchColumn must be a number');
$int = abs(intval($int));
return $this->stmt->fetchColumn($int) or $this->CatchError('the number is not in fetchColumn!');
}
public function SQLParam($str) {
if(is_null($str) || empty($str) || (!isset($str))) return '';
$str = str_replace("'","",$str);
$str = str_replace("\\","",$str);
$str = str_replace("\"","",$str);
$str = str_replace("&","",$str);
$str = str_replace("#","",$str);
$str = str_replace(";","",$str);
$str = str_replace("%","",$str);
$str = str_replace("`","",$str);
$str = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $str);
$str = preg_replace("/&(.)(uml);/", "$1e", $str);

return $str;
}
//数据库操作区结束--------------------------------------------------

function version(){
return array(
//'server' => $this->conn->getAttribute(PDO::ATTR_SERVER_INFO),
'client version' => $this->conn->getAttribute(PDO::ATTR_CLIENT_VERSION),
'driver name' => $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME),
'server version' => $this->conn->getAttribute(PDO::ATTR_SERVER_VERSION),
//'connection' => $this->conn->getAttribute(PDO::ATTR_CONNECTION_STATUS)
);
}


}
?>
...全文
67 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文围绕基于两阶段鲁棒优化算法的微网多电源容量配置问题展开研究,针对微电网中风能、光伏、柴油发电机及储能系统等多元电源的容量规划难题,提出了一种能够有效应对源荷不确定性的优化方法。该方法采用两阶段鲁棒优化框架,第一阶段确定电源的最优配置容量,第二阶段通过调整运行策略应对不确定性扰动,从而提升系统在复杂环境下的适应性与经济性。文中结合Matlab实现了完整的算法建模与仿真验证,提供了清晰的代码实现路径,展示了模型在典型场景下的求解过程与结果分析,具有较强的工程应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力,从事新能源、微电网、优化调度等相关领域的科研人员及研究生。; 使用场景及目标:①解决含高比例可再生能源的微电网系统中电源容量配置的鲁棒性问题;②掌握两阶段鲁棒优化建模方法及其在能源系统中的实际应用;③为应对源荷不确定性提供可复现的技术方案与仿真工具支持; 阅读建议:此资源侧重于算法建模与实际仿真相结合,建议读者在理解鲁棒优化基本理论的基础上,结合Matlab代码逐模块分析,重点掌握不确定集构建、列与约束生成(C&CG)算法实现以及运行结果的经济性与可靠性评估方法,以实现从理论到实践的贯通。

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧