频道直达 - 专题 - 新闻 - 基础 - 高级 - 安装 - 技巧 - 数据库 - 手册 - PHP - Linux - Java - MySQL - Apache - 麻辣堂 - 狼盟 - Rails社群 - 搜索 - 下载 - 开源 - 论坛
PHP开发资源网 主页>>开发技巧>> 收藏此文 | 收藏本站 | 设为首页

PHP Oop+模板编程示例程序(2)

来源:www.phpx.com 作者:lwg888 出处:www.phpx.com 2007-4-27 20:37:50 进入讨论组
关 键 词: 这里建议对PHP基础编程了解或者掌握了的人,如果你们想进一步

程序设计:
首先,我们有一个统一的应用程序接口sources/index.php。我们看一下index.php的内容:

CODE:
<?
include (dirname(__FILE__)."/../include/lib/Application.inc.php");
$app = new Application();
$app->setRoot(dirname(__FILE__)) ;
$app->setDefaultAction("main");
$app->run();
?>
上面的代码很简单:
第1行:载入应用程序接口类Application.inc.php。有很多朋友使用include和require时,在多级调用后经常出现找不到文件的问题,如果象上面那样:绝对路径+相对路径,包你永远正确。扯远了。。。^_^
第4行:$app->setRoot(dirname(__FILE__)) ;设置子功能模块根目录。由于index.php与子功能模块文件在同一目录,因此设为dirname(__FILE__)。
第5行:$app->setDefaultAction("main");设置默认子功能模块,即没有$_GET['action']和$_POST['action']时执行main.php。
很重要的在第六行$app->run(),开始执行子功能模块。

下面就来看下Application.inc.php中run()是如何运作:
代码如下:

CODE:
<?
function run()
{
    
$action $this->getAction();
    
$page_class_file $this->root $action '.php';
    include_once(
$page_class_file);
    
$page = & new CPage($this);
    
$page->execute();
    exit ();
}
?>
首先我们通过getAction()得到参数action的值,
比如list,当前路径+list+.php就是将要执行的子模块
include_once($page_class_file); 载入list.php
很显然,在list.php中有一个类CPage ,并有一个方法函数execute()作为接口来处理数据并显示该页。
在初始化CPage类时,我们将$app对象用$this作为参数传给了CPage,那么在CPage中就可以使用$app对象中封装的所有方法。因此我们可以在$app中封装多一些的常用方法。比如,可以将数据库的初始化,模板的初始化封装进去。
完整代码如下:
Application.inc.php

CODE:
<?php
if(!defined("__CLASS_APPLICATION__"))
{
    
define("__CLASS_APPLICATION__",1);

    require(
dirname(__FILE__)."/PhpClass.inc.php");


    class 
Application extends PhpClass
    
{
        var 
$db  null;
        var 
$defaultAction ;
        var 
$root;

        
//构造函数
        
function Application($root='')
        {
            if (!
defined(ACTIONSTR))define("ACTIONSTR","action");
            if (!empty(
$root))$this->setRoot($root);
        }

        
//设置子功能模块所在的根目录
        
function setRoot($root='')
        {
            if (empty(
$root))$root dirname($_SERVER['SCRIPT_FILENAME']);
            if (!
is_dir($root)) return $this->catchErr("无效的路径");
            if ( 
substr($root,-1) != DIRECTORY_SEPARATOR$root .= DIRECTORY_SEPARATOR;
            
$this->root $root;
        }

        
//设置默认要执行的功能页
        
function setDefaultAction($name)
        {
            
$this->defaultAction $name;
        }

        
//执行子功能页
        
function run()
        {
            
$action $this->getAction();
            
$page_class_file $this->root $action '.php';
            include_once(
$page_class_file);
            
$page = & new CPage($this);
            
$page->execute();
            exit ();
        }

        
//获取参数action的值
        
function getAction()
        {
            
$_name = (isset($_POST[ACTIONSTR])) ? $_POST[ACTIONSTR] : $_GET[ACTIONSTR];
            if (empty(
$_name)) $_name $this->defaultAction;
            return 
$_name;
        }

        
//模板
        
function tpl($templateDir='') {
            
$this->includeClass('Vant');
            if (empty(
$templateDir)) $templateDir $this->root 'templates/'
            
$tpl = & new Vant($templateDir);
            return 
$tpl;
        }

        
//载入类文件
        
function includeClass ($fileName$path ''$lastName '.inc.php')
        {
            if ( 
substr($path,0,1) != DIRECTORY_SEPARATOR$path DIRECTORY_SEPARATOR $path;
            if ( 
substr($path,0,-1) != DIRECTORY_SEPARATOR$path .= DIRECTORY_SEPARATOR;
            if (empty(
$fileName) )  return $this->catchErr("无效的class名");

            include(
dirname(__FILE__) . $path $fileName $lastName);
        }

        
//数据库连接
        
function connectDb($host='',$username='',$password='',$database='')
        {
            
$this->includeClass('DB');

            
$db = new DB($host,$username,password,database);
            
$db->connect();
            
$this->db = & $db;
            return 
$db;
        }

    } 
//end class

}//end if defined

?>
欢迎进入PHP开发资源论坛讨论。
收藏此文】【 】【打印】【关闭
相关文章
图文推荐
论 坛 资 源
PHP开发资源网奋斗目标
阅读排行:
热门技术文档
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容
 
   网站首页 -  网站地图 -  网站合作 -  手册中心 -  通用网址 -  网站论坛 -  网站投稿 -  友情链接 -  帮助中心
版权所有:PHP开发资源网 © 2003-2008 通用网址:PHP资源网 合作媒体: 赛迪网IT技术
互联网违法和不良信息举报中心 | 不良信息举报信箱