对于系统的n层结构:(PHP)一般来说比较常见的是三层和四层结构,说一下我对四层结构的理解吧,因为我从来都是这样用的~~最底层,一般来说是数据库,我称之为数据层或信息层,这层没什么好说的吧上一层,放置的是描述类,在EJB里称为EntityBean(实体Bean),这是直接跟数据库打交道的一层,这就不像EJB了,不过也可以做一个数据库操作类(以下简称db.inc),这一层只跟db.inc打交道,也就是不写SQL语句,所有的SQL语句,也就是跟数据库直接打交道的地方都在db.inc中。再上层,放置的是控制类,在EJB里称为SessionBean(此Session非彼Session哈),感觉并不完全一样,但是差不太多。控制类的作用顾名思义了,即是用特定的方式(除了add/update/delete/SetData)来操作某个描述类,所以实际上它很有可能就只是一个函数库。比如我有个用户的描述类user.inc(不用解释这个是什么吧),我又有个用户的控制类user_control.inc,在该控制类中,我很有可能就有这么一个function:GetUserIDByAge($Age),这个函数的功能一看也就明白,把年龄是$Age的用户的ID查询出来,一般是作为一个数组返回,里面包含的就是年龄是$Age的所有用户的UserID,当然得到了UserID自然也就得到了该用户的一切数据(SetData())。最上层,也就是浏览者看见的一层,我称之为表现层,这层自然就有很多的HTML代码了,当然也内嵌了不少php代码,不用说也应该想到大多都是new一个实例,然后调用它的某个方法来达到某个目的了。“数据库主动关闭”?你是指什么?自动关闭数据库连接?这个要看你是用什么数据库了。失败处理?PHP不是JAVA,没有try catch finally的,最多就是靠函数的返回值来判断某个操作是否成功完成。
http://www.csdn.net/expert/topic/413/413967.shtm
可能我没有描述清楚
因为任何一个数据操作都有可能不成功?
在php里也应该有try catch finally之类的东西吧用吗?
数据库主动关比如是mysql
我在数据执行完后mysql_close上面的四层结构还是不懂
描述好象都是java数据层就是数据库(可能理解错了)
上一层:数据操作类(不写sql,sql是再上一层传递过来的吗?)
再上一层:控制类(除了add/update/delete/SetData)( sql语句从哪里传递过来?)
有没有个例子简单的留言本也可以(分层)
因为有时候得保证程序相对的稳定可能是没的看明白(上面说了所有还不能一目了然)数据操作类就是用方法来传递sql是这样吧(不知道用不传递来描述是否正确)在哪有程序层次分的比较明显的php程序
想看一下刚才看了一下这是帖子题目是编码规范(可能题目没有想清楚)
//数据库操作类 网上到处有下
class DBOperate {
var $Host = "localhost";
var $Database = "glasscity";
var $User = "root";
var $Password = ""; var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row;
var $obj;var $Errno = 0;
var $Error = ""; var $Auto_free = 0; ## Set this to 1 for automatic mysql_free_result()
var $Auto_commit = 0; ## set this to 1 to automatically commit results var $debugmode = 0;function DBOperate()
{
$this->connect();
}function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mysql_pconnect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) {
$this->halt("Link-ID == false, pconnect failed");
}
if (!mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
}
}
} function query($Query_String) {
$this->connect(); if ($this->debugmode)
printf("Debug: query = %s<br>\n", $Query_String); $this->Query_ID = mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
} return $this->Query_ID;
} function FetchObject($result='$this->Query_ID'){
$this->obj = mysql_fetch_object($result);
}function next_record() {
$this->Record = mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error(); $stat = is_array($this->Record);
if (!$stat && $this->Auto_free) {
mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $this->Record;
} function seek($pos) {
$status = mysql_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
return;
} function metadata($table) {
$count = 0;
$id = 0;
$res = array(); $this->connect();
$id = @mysql_list_fields($this->Database, $table);
if ($id < 0) {
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$this->halt("Metadata query failed.");
}
$count = mysql_num_fields($id); for ($i=0; $i<$count; $i++) {
$res[$i]["table"] = mysql_field_table ($id, $i);
$res[$i]["name"] = mysql_field_name ($id, $i);
$res[$i]["type"] = mysql_field_type ($id, $i);
$res[$i]["len"] = mysql_field_len ($id, $i);
$res[$i]["flags"] = mysql_field_flags ($id, $i);
$res["meta"][$res[$i]["name"]] = $i;
$res["num_fields"]= $count;
} mysql_free_result($id);
return $res;
} function affected_rows() {
return mysql_affected_rows($this->Link_ID);
} function num_rows() {
return mysql_num_rows($this->Query_ID);
} function num_fields() {
return mysql_num_fields($this->Query_ID);
} function nf() {
return $this->num_rows();
} function np() {
print $this->num_rows();
} function f($Name) {
return $this->Record[$Name];
} function p($Name) {
print $this->Record[$Name];
} function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);
die("Session halted.");
}
} class SQL extends DBOperate {
var $Host = "localhost";
var $Database = "";
var $User = "";
var $Password = ""; function free_result() {
return @mysql_free_result($this->Query_ID);
} function rollback() {
return 1;
} function commit() {
return 1;
} function autocommit($onezero) {
return 1;
} function insert_id($col="",$tbl="",$qual="") {
return mysql_insert_id($this->Query_ID);
}
}
?> <?
//用户描述类 可以看出库结构吧
class user
{
var $UserID;
var $Password;
var $PassQue;
var $PassAns;
var $NickName;
var $Sex;
var $Email;
var $MarriageState;
var $Birthday;
var $Country;
var $Province;
var $Phone;
var $Mobile;
var $Address;
var $PostCode;
var $Work;
var $Craft;
var $IncomingLevel;
var $Knowledge;
var $NetPlace;
var $HowToKnow;
var $Interesting;
var $Identity;
var $HopeBlaze;
var $TableName; function user()
{
$this->TableName = "user";
} function Add()
{
$link = new DBOperate;
$sql = "insert into ".$this->TableName." values (";
$sql .="'',";
$sql .="'".$this->Password."',";
$sql .="'".$this->PassQue."',";
$sql .="'".$this->PassAns."',";
$sql .="'".$this->NickName."',";
$sql .="'".$this->Sex."',";
$sql .="'".$this->Email."',";
$sql .="'".$this->MarriageState."',";
$sql .="'".$this->Birthday."',";
$sql .="'".$this->Country."',";
$sql .="'".$this->Province."',";
$sql .="'".$this->Phone."',";
$sql .="'".$this->Mobile."',";
$sql .="'".$this->Address."',";
$sql .="'".$this->PostCode."',";
$sql .="'".$this->Work."',";
$sql .="'".$this->Craft."',";
$sql .="'".$this->IncomingLevel."',";
$sql .="'".$this->Knowledge."',";
$sql .="'".$this->NetPlace."',";
$sql .="'".$this->HowToKnow."',";
$sql .="'".$this->Interesting."',";
$sql .="'".$this->Identity."',";
$sql .="'".$this->HopeBlaze."')";
$link->query($sql);
} function SetData($UserID)
{
$link = new DBOperate;
$sql = "select * from user where UserID = '".$UserID."'";
$link->query($sql);
$User = $link->next_record(); $this->UserID = $User["UserID"];
$this->Password = $User["Password"];
$this->PassQue = $User["PassQue"];
$this->PassAns = $User["PassAns"];
$this->NickName = $User["NickName"];
$this->Sex = $User["Sex"];
$this->Email = $User["Email"];
$this->MarriageState = $User["MarriageState"];
$this->Birthday = $User["Birthday"];
$this->Country = $User["Country"];
$this->Province = $User["Province"];
$this->Phone = $User["Phone"];
$this->Mobile = $User["Mobile"];
$this->Address = $User["Address"];
$this->PostCode = $User["PostCode"];
$this->Work = $User["Work"];
$this->Craft = $User["Craft"];
$this->IncomingLevel = $User["IncomingLevel"];
$this->Knowledge = $User["Knowledge"];
$this->NetPlace = $User["NetPlace"];
$this->HowToKnow = $User["HowToKnow"];
$this->Interesting = $User["Interesting"];
$this->Identity = $User["Identity"];
$this->HopeBlaze = $User["HopeBlaze"];
}
?><?
//用户控制类 省略了不少 不过一个你就应该看得明白是怎么用的
function GetUserIDByProvince($Province)
{
$link = new DBOperate;
$result = array(); $sql = "select ID from ". $this->TableName ." where Province = '".$Province."'";
$link->query($sql);
for ($i=0;$i<$link->num_rows();$i++)
{
$Goods = $link->next_record();
$result[] = $Goods["ID"];
}
return $result;
}
?>
php中分的太不明显了
java中好象明显点
加分了