关 键 词:
这里建议对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();
?>
上面的代码很简单: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的值,function run()
{
$action = $this->getAction();
$page_class_file = $this->root . $action . '.php';
include_once($page_class_file);
$page = & new CPage($this);
$page->execute();
exit ();
}
?>
比如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开发资源论坛讨论。
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
?>
相关文章
图文推荐
主 站 资 源
论 坛 资 源
·SQLite结合PHP的开发实践
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·PDO函数库使用入门
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·PDO函数库使用入门
热门技术文档
·SQLite结合PHP的开发实践
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·关于Servlet及JSP中遇到的多线程同步问题
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
·简介一种PHP设计模式:DPT
·在PHP中使用XML-RPC来构造Web Service简介
·PHP网站注入方法之深度分析
·教你使用Apache的rewrite技术来实现URL重写
·Wordpress 2.5 Tags 标签功能
·关于Servlet及JSP中遇到的多线程同步问题
·详细解析:Apache服务器实现的用户验证
·set_include_path在win和linux下的区别
·自定义SESSION(一)——文件
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
- · 3分钟快速了解 Delphi for PHP 特色 (中文), PDF档
- · 购买Delphi for PHP的五大理由, PDF档
- · Delphi for PHP 使用规格介绍, PDF档
- · Delphi for PHP 問答集 (From CodeGear)
- · Delphi for PHP 产品价格表
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容




