博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP程序的一次重构记录
阅读量:6853 次
发布时间:2019-06-26

本文共 3716 字,大约阅读时间需要 12 分钟。

项目和新需求:

我们有一个PHP写的webmail系统,有一个mail_list.php用于展现用户的邮件列表这个页面支持folderId参数(因为邮件是存在不同的文件夹下的)由于邮件太多所以支持翻页。现在需要在系统里增加标签。细化下来:需要在原有的邮件列表里增加标签列,和支持tagid检索出一个邮件列表(列表里也增加标签列)

代码和新需求
mail_list.php调用GetMailList函数,这个函数传入文件夹ID,页码,页大小,得到HTML TABLE和导航链接
GetMailList用T_Page类得到请求的一页数据再处理为HTML表格
虽然T_Page只支持单表查询,但是在整个系统里还是被广泛使用

因为动T_Page还是有风险的,所以最好继承它,然后让需要多表查询的地方直接实例化子类


重构前

/*功能:查询指定文件夹下的特定页的邮件列表(HTML Table)*/function GetMailList($folderId, $pageNo, $pageSize){    global $db_obj, $pageLink, $html; // db_obj是从外部导入使用,后两个似乎要导出去        $query = " where folderId = $folderId ..."; //省略很多行    $pageclass = new T_Page;    $tableName = "mail";    $condition = $query . "order by tm desc";    $pageclass->BaseSet($tableName, "id,sender,subject,content,...", $pageNo, $pageSize, $db_obj);    $pageclass->SetCondition($condition);        $record = $pageclass->ReadList(); // 查出来的记录集,被放到二维数组里    $pageLink=$pageclass->Page(); //存放翻页按钮的数组    $html = "
"; for($ipage=0;$ipage<$pageSize;$ipage++) { $i=$ipage; $id = $record[$i]["id"]; $sender = $record[$i]["sender"]; $subject = $record[$i]["subject"]; $content = $record[$i]["content"]; $html = $html . "
"; $html = $html . "
"; ... } $html = $html . "
$subject
";}/*功能: 查询"一页"记录,且输出导航链接用于翻页*/class T_Page{ var $TableName; ... function BaseSet($TableName, $Fields, $PageNo, $PageSize, $Obj_DB) { $this->TableName = $TableName; ... $this->MaxLine = $this->PageSize; $this->Offset = $this->PageNo * $this->PageSize; } function SetCondition($Condition) { $this->Condition = $Condition; } function ReadList() { $SQL = "select count(*) as recordtotal from $this->tableName $this->Condition"; $result = $this->Obj_DB->simpleQuery($SQL); $row = $this->Obj_DB->fetchRow($result, DB_FETCHMODE_ASSOC); $this->Total = $row["recordtotal"]; if($this->Total > 0) { $SQL = "select $this->Fields fom $this->tableName $this->Condition limit $this->Offset, $this->MaxLine;"; $result = $this->Obj_DB->simpleQuery($SQL); $this->Number = $this->Obj_DB->numRows($result); while($row = $this->Obj_DB->fetchRow($result,DB_FETCHMODE_ASSOC)) { $this->Result[]=$row; } $this->Obj_DB->freeResult($result); } return($this->Result) } function Page() { if($this->PageNo > 0) $this->PageLink[0] = "上一页"; else $this->PageLink[0] = "上一页"; ... return $this->PageLink; } // more function}

 


 重构后

class MailPage extends T_Page{    function setTotal()    {        ...        $this->Total = $row["recordtotal"];    }        function getMailList()    {        $Fields = $this->Field . " ,'' as taglist ";        $SQL.="select $Fields from $this->Table $this->Condition";        $SQL.=" LIMIT $this->Offset , $this->MaxLine";                 ...    }        function addTagList()    {
     ... $tagList = Array(); for($i=0; $i < $this->Number; $i++) { $key = $this->Result[$i]["id"]; $this->Result[$i]["taglist"] = $tagList[$key]; } } //覆盖父类的接口 function ReadList() { $this->setTotal(); $this->getMailList(); $this->addTagList(); return $this->Result; } }class TagPage extends MailPage{ function SetCondition($taglist) { ... $this->Condition = sprintf(" where id in (%s) order by tm desc ", $mailList); }}

 

转载于:https://www.cnblogs.com/code-style/p/4184589.html

你可能感兴趣的文章
Redis清理
查看>>
读书笔记—CLR via C#章节8-10
查看>>
洛谷 3804 【模板】后缀自动机
查看>>
LOJ 2736 「JOISC 2016 Day 3」回转寿司 ——堆+分块思路
查看>>
IE8爆出0day,影响所有版本Windows
查看>>
php: Cannot send session cache limiter
查看>>
子类复制父类的值
查看>>
例题10-1 UVa11582 Colossal Fibonacci Numbers!(同余与模算术)
查看>>
hdu 1385 题意 测试数据
查看>>
Java 炫舞按键功能 DancingPlay (整理)
查看>>
rtems网络移植-rtems系统初始化过程分析
查看>>
re正则表达式:import re ;re.search()
查看>>
介绍下Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果。
查看>>
math.net 拟合
查看>>
找出数组中两数之和为指定值的所有整数对
查看>>
ubuntu IP 扫描
查看>>
项目架构图,mvc架构图
查看>>
Hadoop开发者第四期
查看>>
资料分享:淘宝主备数据库自动切换机制
查看>>
centos 学习总结
查看>>