不错的一篇面向对象的PHP开发模式(简写版)


Posted in PHP onMarch 15, 2007

我看到有人在批判PHP,什么这地方不好用,那地方不好用的。其实严格地说起来,没有一门语言好用,也没有一门语言有一个严格的标准,凡事都有一个发展的过程,我们总不能等这些标准呀什么的都很完善了再用吧?我觉得不管用什么语言,写程序都要靠自己,一个程序员要有好的风格,思路等等。最近我在整理一些资料,现在发出一些,希望大家多提意见,多多扶持啊哈

======================================
面向对象的PHP开发模式(待完善中。。。)
======================================

一、环境

服务器:Linux (Apache 2.x, MySQL4.1.x, PHP4, Perl, SHELL, CVS, Sambar)

客户端:Windows (Ie6, UltraEdit, 其它辅助工具)

测试机:windows98/2K/xp/Linux (Ie5, Ie6, mozilla, firefox)

二、网页、程序、数据库的三层

所谓的网页并不是一般的静态网页,这里的网页是根据项目分析的具体情况进行拆分
后用html做的模板;这里的数据库包括数据库和与其它部分的接口程序,通常程序和数据库
程序可能会混合在一个文件里,但应该用函数的方式把它们尽量分开,其它程序如果要用数
据库直接调用这些函数即可,不能直接接触SQL语句。

三、项目分析--数据分析

一个项目在得到需求分析后,实际开发前第一步应该做的就是数据分析。数据分析就是
把项目过程中要用到的各式各样的数据堆在一块,根据它们的特点进行分类再分别组织,当
然它们之间还可能存在着各式各样的关联关系。做好这一步就使项目分析工作得到了一个良
好的开端,为下面的项目结构分析及数据处理的流程分析也提供了极大的方便。

四、项目分析--数据抽象

由数据分析后我们的脑子中应该能出现一些大致的数据模型及一些基本数据小模型组合
而成的大模型,一般情况下,我们把一些需要变化的数据建立数据库来进行维护,不需要变
化的数据做成一些常量,并针对这些数据类型抽象出相关的类,并建立进行数据库操作的相
关接口(函数形式,即方法),数据与数据的相关联的操作也可以抽象出一些基本的方法,
我们只需要在程序设计中进行调用即可。

五、项目分析--界面分析

我们分析好了数据,目的是组合出一个或者几个产品,而既然要做产品就要给别人看。
所以我们还要进行界面设计,当各种界面尽量考虑全面后,就将设计的界面制作成模板,并
写出相应的处理接口程序(所以,在程序眼里,界面也是一种数据),在写程序时进行使用。

六、项目分析--流程设计

网站式程序非常简单,按照流程调用我们设计好的各种数据即可。

七、案例分析

用户系统,现在我们分析一个最简单的例子,一个用户系统。

1. 数据分析,我们分析一个最简单的用户系统,所以这里只有两个数据,那就是用户名
和密码,继续分析还会想到我们应该给每条记录加一个编号(id),现在有了三个数据,实在没
有再可以添加的了。

2. 数据抽象,只有三个数据的数据模型,想到它可能出现的操作方法,我们做如下安排,
数据库接口(savetodb(), getfromdb(), delete()),分别为数据入库及出库还有删除;更改密
码(password())。另外考虑到对用户系统的管理及查看,所以会有一个集合类型的数据(list)。

3. 界面分析,登陆,验证成功,验证出错,修改密码,修改密码成功,修改密码出错,用
户注册,注册成功,注册出错;管理--用户列表,管理--用户信息查看,管理--修改用户
密码,管理--删除用户。

4. 示例代码
PHP 代码:

<?php  
include_once "include.php";  
/*  
** 用途:用户系统数据抽象  
** 作者:岳信明  
** 时间:2005-8-30 10:05  
*/  
class User {  
    var $id       = 0;  
    var $Name     = "";  
    var $Password = "";  
    var $db       = "";  
    var $tpl      = "";  
    /*  
    ** 函数功能:构造函数,指定类使用的数据库连接  
    ** 参数说明:$tpl,显示模板处事句柄;$userdb,数据库连接  
    ** 返 回 值:无  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 10:37  
    */  
    function User($vtpl = "", $userdb = "") {  
        if ($vtpl == "") {  
            global $tpl;    // 外部定义数据库连接  
            $this->tpl =& $tpl;  
        } else {  
            $this->tpl = $vtpl;  
        }  
        if ($userdb == "") {  
            global $db;    // 外部定义数据库连接  
            $this->db =& $db;  
        } else {  
            $this->db = $userdb;  
        }  
    }  
    /*  
    ** 函数功能:将数据存入数据库  
    ** 参数说明:无参数  
    ** 返 回 值:true/false,成功/失败  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 10:24  
    */  
    function savetodb() {  
        if ($this->Name == "") {  
            return false;  
        }  
        if ($this->id) {  
            $strSQL = sprintf("UPDATE user SET Name='%s', Password='%s' "  
                            . "WHERE id='%s'",  
                              $this->Name,  
                              $this->Password,  
                              $this->id  
                             );  
        } else {  
            $strSQL = sprintf("INSERT user (Name, Password) "  
                            . "VALUES ('%s', '%s')",  
                              $this->Name,  
                              $this->Password  
                             );  
        }  
        if ($this->db->query($strSQL)) {  
            return true;  
        } else {  
            return false;  
        }  
    }  
    /*  
    ** 函数功能:从数据库中获取记录  
    ** 参数说明:$id,记录编号  
    ** 返 回 值:true/false,成功/失败  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 10:32  
    */  
    function getfromdb($id = 0) {  
        if ($id) {  
            $strSQL = sprintf("SELECT * FROM user WHERE id='%s'", $id);  
        } else if ($this->id) {  
            $strSQL = sprintf("SELECT * FROM user WHERE id='%s'",  
                              $this->id  
                             );  
        } else if ($this->Name != "") {  
            $strSQL = sprintf("SELECT * FROM user WHERE Name='%s'",  
                              $this->Name  
                             );  
        } else {  
            return false;  
        }  
        $this->db->query($strSQL);  
        if ($this->db->next_record()) {  
            $this->id       = $this->db->f("id");  
            $this->Name     = $this->db->f("Name");  
            $this->Password = $this->db->f("Password");  
            return true;  
        } else {  
            return false;  
        }  
    }  
    /*  
    ** 函数功能:从数据库中删除记录  
    ** 参数说明:$id,记录编号  
    ** 返 回 值:true/false,成功/失败  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 10:47  
    */  
    function delete($id = 0) {  
        if (is_array($id)) {    // 同时删除多条记录  
            foreach($id as $i) {  
                $strSQL = sprintf("DELETE FROM user WHERE id='%s'", $i);  
                $this->db->query($strSQL);  
            }  
            return true;  
        } else if ($id) {  
            $strSQL = sprintf("DELETE FROM user WHERE id='%s'", $id);  
        } else if ($this->id) {  
            $strSQL = sprintf("DELETE FROM user WHERE id='%s'", $this->id);  
        } else {  
            return false;  
        }  
        $this->db->query($strSQL);  
        return true;  
    }  
    /*  
    ** 函数功能:显示登陆界面  
    ** 参数说明:$placeholder,显示位置  
    ** 返 回 值:无  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 11:00  
    */  
    function showLogin($placeholder) {  
        $this->tpl->addBlockfile($placeholder, "user_showLogin",  
                                 "tpl.user_showLogin.html"  
                                );  
        $this->tpl->setCurrentBlock("user_showLogin");  
        $this->tpl->setVariable(array("user_Logintitle" => "用户登陆",  
                                      "strUsername"     => "用户名",  
                                      "strPassword"     => "密 码"  
                                     )  
                               );  
        $this->tpl->parseCurrentBlock("user_showLogin");  
    }  
    /*  
    ** 函数功能:处理登陆信息  
    ** 参数说明:$placeholder,显示位置  
    ** 返 回 值:true/false,成功/失败  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 11:12  
    */  
    function getLogin($placeholder = "") {  
        if (isset($_POST["login"])) {  
            if ($_POST["username"] == "") {  
                if ($placeholder != "") {  
                    $this->tpl->setVarable($placeholder, "用户名不能为空!");  
                }  
                return false;  
            }  
            $this->Name = $_POST["username"];  
            $this->getfromdb();  
            if ($this->Password() == $_POST["password"]) {  
                return true;  
            }  
        } else {  
            if ($placeholder != "") {  
                $this->tpl->setVarable($placeholder, "登陆失败!");  
            }  
            return false;  
        }  
    }  
    /*  
    ** 函数功能:显示注册界面  
    ** 参数说明:$placeholder,显示位置  
    ** 返 回 值:无  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 13:33  
    */  
    function showRegister($placeholder) {  
        $this->tpl->addBlockfile($placeholder, "user_showRegister",  
                                 "tpl.user_showRegister.html"  
                                );  
        $this->setCurrentBlock("user_shoRegister");  
        // 在这里完成处理模板的代码  
        ...  
        $this->parseCurrentBlock("user_shoRegister");  
    }  
    /*  
    ** 函数功能:处理注册信息  
    ** 参数说明:$placeholder,显示位置  
    ** 返 回 值:true/false,注册成功/注册失败  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 15:49  
    */  
    function getRegister($placeholder = "") {  
        if (isset($_POST["register")) {  
            if ($_POST["username"] == "") {    // 用户名合法性检查,可改成其它检查方式  
                if ($placeholder != "") { // 错误提示  
                    $this->tpl->setVariable($placeholder, "用户名不合法!");  
                }  
                return false;  
            }  
            if ($_POST["password"] != $_POST["repassword"]) {    // 密码合法性检查  
                if ($placeholder != "") { // 错误提示  
                    $this->tpl->setVariable($placeholder, "两次输入密码不一致!");  
                }  
                return false;  
            }  
            $strSQL = sprintf("SELECT COUNT(*) FROM user "  
                            . "WHERE Name='%s'",  
                              $this->Name  
                             );  
            $this->db->query($strSQL);  
            $this->db->next_record();  
            if ($this->db->f("COUNT(*)") > 0) {  
                return false;  
            } else {  
                $strSQL = sprintf("INSERT INTO user (Name, Password) "  
                                . "VALUES('%s', '%s')",  
                                  $this->Name,  
                                  $this->Password  
                                 );  
                $this->db->query($strSQL);  
                return true;  
            }  
        } else {  
            return false;  
        }  
    }  
} // 类User定义结束  
/*  
** 用途:用户系统数据列表抽象  
** 作者:岳信明  
** 时间:2005-8-30 17:21  
*/  
class UserList {  
    var $page      = 0;  
    var $pages     = 0;  
    var $pagesize  = 9;  
    var $recordsum = 0;  
    var $Users     = array();  
    var $c;  
    var $db        = "";  
    var $tpl       = "";  
    /*  
    ** 函数功能:构造函数,新建一个类时对一些变量进行初始化  
    ** 参数说明:无参数  
    ** 返 回 值:无  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-30 15:49  
    */  
    function UserList($page = 1, $pagesize = 10,  
                      $c, $vtpl = "", $vdb = "") {  
        $this->page = $page;  
        $this->pagesize = $pagesize;  
        $this->condition = $condition;  
        if ($vdb != "") {  
            $this->db = $vdb;  
        } else {  
            global $db;  
            $this->db = $db;  
        }  
        if ($vtpl != "") {  
            $this->tpl = $vtpl;  
        } else {  
            $this->tpl = $tpl;  
        }  
        $strSQL = sprintf("SELECT COUNT(*) FROM user WHERE '%s'",  
                          $this->condition  
                         );  
        $this->db->query($strSQL);  
        $this->db->next_record();  
        $this->recordsum = $this->db->f("COUNT(*)");  
        $this->pages = ceil($this->recordsum / $this->pagesize);  
        $strSQL = sprintf("SELECT * FROM user WHERE '%s' LIMIT '%s', '%s'",  
                          $this->condition,  
                          $this->page * $this->pagesize,  
                          $this->pagesize + 1  
                         );  
        $this->db->query($strSQL);  
        for ($i = 0; $this->db->next_record(); $i ++) {  
            $this->Users[$i] = new User($this->tpl, $this->db);  
            $this->Users[$i]->id       = $this->db->f("id");  
            $this->Users[$i]->Name     = $this->db->f("Name");  
            $this->Users[$i]->Password = $this->db->f("Password");  
        }  
    }  

    /*  
    ** 函数功能:显示列表  
    ** 参数说明:$placeholder,显示位置  
    ** 返 回 值:无  
    ** 作
者:岳信明  
    ** 创建时间:2005-8-31 9:16  
    */  
    function showUserList($placeholder) {  
        $this->tpl->addBlockfile($placeholder, "showUserList", "tpl.showUserList.html");  
        $this->tpl->setCurrentBlock("showUserList");  
        //在这里添加相应的处理代码  
        $this->tpl->setVariable("strTitle", "用户列表");  
        $strTitles = array("用户名", "操作");  
        $RecordOperations = array("重设密码" => "operate=passwd&id=",  
                                  "删除"     => "operate=delete&id="  
                                 );  
        // 显示表头  
        foreach ($strTitles as $title) {  
            $this->tpl->setCurrentBlock("showRecordsTitle");  
            $this->tpl->setVariable("strHead", $title);  
            $this->tpl->parseCurrentBlock("showRecordsTitle");  
        }  
        // 显示记录及相关操作  
        if (is_array($this->Users)) {    // 有记录  
            foreach ($this->Users as $user) {  
                $this->tpl->setCurrentBlock("showRecords");  
                $this->tpl->setCurrentBlock("showCell");  
                $this->tpl->setVariable("strCell", $user);  
                $this->tpl->parseCurrentBlock("showCell");  
                $this->tpl->setCurrentBlock("showCell");  
                foreach ($RecordOperations as $operation => $linkOperation) {  
                    $this->tpl->setCurrentBlock("showOperations");  
                    $this->tpl->setVariable("strOperation", $operation);  
                    $this->tpl->setVariable("strLink", $_SERVER["REQUEST_URI"] . $linkOperation . $user->id);  
                    $this->tpl->parseCurrentBlock("showOperations");  
                }  
                $this->tpl->parseCurrentBlock("showCell");  
                $this->tpl->parseCurrentBlock("showRecords");  
            }  
        } else {    // 无记录  
            $this->tpl->setCurrentBlock("showRecords");  
            $this->tpl->setCurrentBlock("showCell");  
            $this->tpl->setVariable("strCell", "无记录");  
            $this->tpl->parseCurrentBlock("showCell");  
            $this->tpl->setCurrentBlock("showCell");  
            $this->tpl->setVariable("strCell", " ");  
            $this->tpl->parseCurrentBlock("showCell");  
            $this->tpl->parseCurrentBlock("showRecords");  
        }  
        $this->tpl->setCurrentBlock("showPageInfo");  
        $this->tpl->setVariable(array("intColspan" => "2",  
                                      "intRecordSum" => $this->recordsum,  
                                      "intPage"      => $this->page,  
                                      "intPages"     => $this->pages  
                                     )  
                               );  
        $this->tpl->parseCurrentBlock("showPageInfo");  
        $this->tpl->parseCurrentBlock("showUserList");  
    }  
}  
?> <!-- php buffer end --> 

HTML 代码:
PHP 相关文章推荐
用文本文件制作留言板提示(下)
Oct 09 PHP
PHP与MySQL开发中页面乱码的产生与解决
Mar 27 PHP
Ajax PHP简单入门教程代码
Apr 25 PHP
用mysql内存表来代替php session的类
Feb 01 PHP
php简单对象与数组的转换函数代码(php多层数组和对象的转换)
May 18 PHP
解析:php调用MsSQL存储过程使用内置RETVAL获取过程中的return值
Jul 03 PHP
php calender(日历)二个版本代码示例(解决2038问题)
Dec 24 PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
Oct 01 PHP
php+mysql删除指定编号员工信息的方法
Jan 14 PHP
php正则判断是否为合法身份证号的方法
Mar 16 PHP
使用ThinkPHP生成缩略图及显示
Apr 27 PHP
laravel自定义分页效果
Jul 23 PHP
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
Mar 15 #PHP
Linux下进行MYSQL编程时插入中文乱码的解决方案
Mar 15 #PHP
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
Mar 15 #PHP
一个简单的PHP&amp;MYSQL留言板源码
Jul 19 #PHP
利用PHP和AJAX创建RSS聚合器的代码
Mar 13 #PHP
手把手教你使用DedeCms的采集的图文教程
Mar 11 #PHP
PHP中的CMS的涵义
Mar 11 #PHP
You might like
Apache2 httpd.conf 中文版
2006/12/06 PHP
PHP 强制下载文件代码
2010/10/24 PHP
常见的PHP五种设计模式小结
2011/03/23 PHP
php实现屏蔽掉黑帽SEO的搜索关键字
2015/04/15 PHP
php通过执行CutyCapt命令实现网页截图的方法
2016/09/30 PHP
PHP利用curl发送HTTP请求的实例代码
2020/07/09 PHP
海量经典的jQuery插件集合
2010/01/12 Javascript
基于JQuery的密码强度验证代码
2010/03/01 Javascript
类似php的js数组的in_array函数自定义方法
2013/12/27 Javascript
js在输入框屏蔽按键,只能键入数字的示例代码
2014/01/03 Javascript
JavaScript中使用typeof运算符需要注意的几个坑
2014/11/08 Javascript
轻松创建nodejs服务器(3):代码模块化
2014/12/18 NodeJs
js在指定位置增加节点函数insertBefore()用法实例
2015/01/12 Javascript
使用AngularJS创建单页应用的编程指引
2015/06/19 Javascript
jQuery实现鼠标滑向当前图片高亮显示并且其它图片变灰的方法
2015/07/27 Javascript
jquery与ajax获取特殊字符实例详解
2017/01/08 Javascript
使用MUI框架模拟手机端的下拉刷新和上拉加载功能
2017/09/04 Javascript
jQuery实现的淡入淡出图片轮播效果示例
2018/08/29 jQuery
JavaScript实现图片的放大缩小及拖拽功能示例
2019/05/14 Javascript
[02:38]DOTA2亚洲邀请赛 IG战队巡礼
2015/02/03 DOTA
[00:43]魔廷新尊——痛苦女王至宝捆绑包
2020/06/12 DOTA
python解析发往本机的数据包示例 (解析数据包)
2014/01/16 Python
Python lxml模块安装教程
2015/06/02 Python
Python中函数参数调用方式分析
2018/08/09 Python
python try 异常处理(史上最全)
2019/03/07 Python
pandas 空数据处理方法详解
2019/11/02 Python
python mysql中in参数化说明
2020/06/05 Python
快速创建 HTML5 Canvas 电信网络拓扑图的示例代码
2018/03/21 HTML / CSS
纽约的奢华内衣店:Journelle
2016/07/29 全球购物
马来西亚最大的电器网站:Senheng
2017/10/13 全球购物
Nice Kicks网上商店:ShopNiceKicks.com
2018/12/25 全球购物
Groupon西班牙官方网站:在线优惠券和交易,节省高达70%
2021/03/13 全球购物
党建工作目标管理责任书
2015/01/29 职场文书
五四青年节活动总结
2015/02/10 职场文书
怎样写好工作计划
2019/04/10 职场文书
2019奶茶店创业计划书范本,值得你借鉴
2019/08/14 职场文书