您好,欢迎访问一九零五行业门户网

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

我看到有人在批判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 代码:
复制代码 代码如下:
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); 
    } 

?>
html 代码:
{strtitle} {strhead}
{strcell} {stroperation}
共 {intrecordsum} 条记录,{intpage}/{intpages} 页 {intpagenum}
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]
http://www.bkjia.com/phpjc/317998.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/317998.htmltecharticle我看到有人在批判php,什么这地方不好用,那地方不好用的。其实严格地说起来,没有一门语言好用,也没有一门语言有一个严格的标准,...
其它类似信息

推荐信息