class file{
var $size = 0;
var $name;
function file(){};
function setSize($size){$this->size=$size;}
function setName($name){$this->name=$name;}
function setSize($size){$this->size=$size;}
function getName(){return $this->name;}
}
var $size = 0;
var $name;
function file(){};
function setSize($size){$this->size=$size;}
function setName($name){$this->name=$name;}
function setSize($size){$this->size=$size;}
function getName(){return $this->name;}
}
/* 文件名 news.inc.php
** 说明 新闻操作类
*/class news
{
var $id; //新闻编号
var $title; //新闻标题
var $content; //新闻内容
var $author; //新闻作者
var $postime; //发布时间
var $cat; //新闻类别
var $audit; //是否审核 0:未审核 1:已审核
var $tpl; //使用的模板路径
var $recmd; //是否推荐 0:不推荐 1:推荐
var $top; //是否设为首页新闻 0:不设 1:设置
var $num = array(); //新闻总数及各分类总数的数组
var $db; //mysql 类的实例
/* 函数 news(&$db)
** 功能 构造函数
** 参数:$db mysql类的实例
*/
function news(&$db)
{
$this->db = &$db;
}
/* 函数 add($title, $content, $author, $cat, $img)
** 功能 添加一篇新闻
** 参数 参见类的变量声明
*/
function add($title, $content, $author, $postime, $cat, $audit, $tpl, $recmd, $top)
{
$sql = "INSERT INTO news VALUES ('',
'$title',
'$content',
'$author',
'$postime',
'$cat',
'$audit',
'$tpl',
'$recmd',
'$top')";
$this->db->query($sql);
if ($this->db->success())
return 1;
else
return 0;
}
/* 函数 update($id, $title, $content, $author, $cat, $img, $view_num)
** 功能 修改一篇新闻
** 参数 参见类的变量声明
*/
function update($id, $title, $content, $author, $postime, $cat, $audit, $tpl, $recmd, $top)
{
$sql = "UPDATE news SET title = '$title',
content = '$content',
author = '$author',
postime = '$postime',
cat = '$cat',
audit = '$audit',
tpl = '$tpl',
recmd = '$recmd',
top = '$top'
WHERE id = '$id'";
if ($this->db->query($sql))
return 1;
else
return 0;
}
/* 函数 del($id)
** 功能 删除一篇新闻
** 参数 $id 新闻编号
*/
function del($id)
{
$sql = "DELETE FROM news WHERE id = $id ";
$this->db->query($sql);
if ($this->db->success())
return 1;
else
return 0;
}
/* 函数 list_news($id)
** 功能 根据新闻编号从库中取得一条记录
** 参数 $id 新闻编号
*/
function list_one($id)
{
$sql = "SELECT * FROM news WHERE id = $id ";
//echo $sql;
if ($this->db->query($sql))
return 1;
else
return 0;
}
/* 函数 list_news($start=0, $rows=10)
** 功能 从数据库中取得新闻列表
** 参数 condition array()条件数组
** 参数 $start 每次取得的起始记录,默认为第一条记录
** 参数 $rows 每次取得的记录条数 默认为10条
*/
function list_news($condition, $start=0, $rows=10)
{
$sql = "SELECT id, title, author, postime, cat, audit FROM news ";
$tmp = ""; //用来保存条件SQL
if (isset($condition['cat']))
$tmp .= "WHERE cat = '".$condition['cat']."' ";
if (isset($condition['audit']))
$tmp .= (empty($tmp)?"WHERE":"AND")." audit = '".$condition['audit']."' ";
if (isset($condition['recmd']))
$tmp .= (empty($tmp)?"WHERE":"AND")." recmd = '".$condition['recmd']."' ";
if (isset($condition['top']))
$tmp .= (empty($tmp)?"WHERE":"AND")." top = '".$condition['top']."' ";
$sql .= $tmp."ORDER BY postime DESC LIMIT $start, $rows";
//echo $sql;
if ($this->db->query($sql))
return 1;
else
return 0;
}
/* 函数 get_news()
/* 功能 取得一条新闻记录并保存在变量中
** 参数 无
*/
function get_news()
{
if ($record = $this->db->get_record())
{
foreach ($record as $key => $value)
{
$this->$key = $value;
}
return 1;
}
else
return 0;
}
/* 函数 total_news()
/* 功能 返回查询新闻结果的总数
** 参数 无
*/
function total_news()
{
return $this->db->total_record();
}
/* 函数 news_num()
** 功能 返回所有新闻的总数及各类新闻的总数
** 参数 无
** 返回值 num 数组
** num[0] 所有新闻
** num[1] 第一类新闻总数
** num[2] 第二类新闻总数
** .................
*/
function news_num()
{
$sql = "SELECT count(*) as num, cat FROM news GROUP BY cat";
if ($this->db->query($sql))
{
$this->num[0] = 0;
while($record = $this->db->get_record())
{
$this->num[$record['cat']] = $record['num'];
$this->num[0] += $record['num'];
}
return $this->num;
}
else
return 0; //错误
}
}
?>
好了,我假设你的session已经没问题了,我对下面的例子作个说明:b.php和a.php各定义了一个类,其中a.php中的类a稍微复杂,因为它是由b.php中的类doc组成的,这叫类的组成关系.我举的例子大概是最简单的类组成关系的例子啦.
c.php中产生了一个类a的对象叫test,并把它注册成session变量,c.php还显示了一个超文本的表单界面,供你输入一个字串.点击按钮后,对象test被传递到d.php,在下面的程序中,我具体介绍了传递对象时要注意的事项.这个程序是可以实际运行的,它是我学习用session传递对象的一个小总结,大家可以回去试试看,祝大家愉快.
<?
//a.php
include("b.php");//包含b.php是因为doc类的定义在b.php中,而类a中的doc属性是doc类的对象
/**
* Short description.
* 一个带有组成关系的类
* Detail description
* @author
* @version 1.0
* @copyright
* @access public
*/
class a
{
/**
* Description 这是一个简单变量作为类a的属性
* @var
* @since 1.0
* @access private
*/
var $docid;
/**
* Description 这是一个对象变量作为类a的属性
* @var
* @since 1.0
* @access private
*/
var $doc;
/**
* Short description.
* 构造函数
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function a()//类a的构造函数,它给自己的doc属性赋了初值
{
$adoc=new doc;
$this->doc=$adoc;
} // end func
/**
* Short description.
* 给docid赋值
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function getdocid()//这个函数给自己的docid属性赋了值
{
$this->docid=$this->doc->id;
} // end func
/**
* Short description.
* 这个函数极简单,就不解释啦
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function printdocid()//显示doc对象属性的id属性
{
echo "doc->id=".$this->doc->id."<br>";
} // end func
} // end class
?>
<?
//b.php
/**
* Short description.
* 这是一个简单的类
* Detail description
* @author
* @version 1.0
* @copyright
* @access public
*/
class doc
{
/**
* Description 简单变量作为类doc的属性
* @var
* @since 1.0
* @access private
*/
var $id;
/**
* Short description.
* 这个函数显示一个录入界面
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function scr()
{
screen();
} // end func
/**
* Short description.
* 这个函数把上面录入的值入库
* Detail description
* @param none
* @global none
* @since 1.0
* @access private
* @return void
* @update date time
*/
function save($conn,$i)
{
$sql="INSERT INTO `test` (`id`, `name`) VALUES ('', '$i')";
$result=mysql_query($sql,$conn);
$this->id = mysql_insert_id($conn);
} // end func
} // end class
?><?
//c.php
include("a.php");//要产生类a的对象须包含定义类a的文件(注意,a.php中不能有超文本,一个空格都不行,因为session_start前面有这样的要求;也不能包含带有超文本的文件,如果功能要求中必须包含超文本,请做成象下面screen.php那样的函数)
$test=new a();//产生一个类a的对象,取名test
session_start();
session_register('test');//把对象变量注册成session变量以便传递
include("screen.php");//这时再包含带有超文本的文件
$test->doc->scr();//调用界面显示
?><?
//d.php
include("a.php");//要想用session接受前面传递的对象变量,必须在session_start()前包含定义该类的文件
session_start();
include("conn.php");//此文件中定义了下句用到的connect()函数的定义
$conn=connect();
echo "i=$i<br>";
$test->doc->save($conn,$i);//这个test对象是在c.php中建立,并通过session传递过来的,它是这段程序显示的目的所在, 请注意体会;test对象的属性和方法被session传递后,依然有效
$test->getdocid();
$test->printdocid();
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<a href="c.php">return</a>
</body>
</html><!--到此为止都是d.php文件中的-->
<?
//conn.php
/**
* Short description.
* 这就是一个普通的mysql连接函数,没什么特别的
* Detail description
* @param none
* @global none
我只是刚开始准备学PHP~
那请问一下如果function有返回值呢~
在调用这个方法时我怎么接收呢~