<?
直接生成一个新类:
$car = new cart(11);
echo is_object($car); //Object 正确!
----------------------------------------------------------
$cart = new cart(20);
通过SESSION传递后,在另一个页面里:
echo $cart."
";
print_r($cart)."
";
echo is_object($cart)."
";
-输出::
Object
__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => cart [cartid] => 20 [cartpro] => Array ( [5] => 1 ) )
""//判断返回值为空 这里有问题[/COLOR]
现在想不明白的就是为什么用SESSION传过来的对象好像不是对象 的类型了,出错信息是:
----
Fatal error: Unknown(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition cart of the object you are trying to operate on was loaded _before_ the session was started in showcart.php on line 32
----
对比了一下,发现出SESSION传递过来的对象中加上了“[__PHP_Incomplete_Class_Name]”,是干什么的呢?
这是我的类文件,大家方便的帮帮看看了!
真的想不出是什么错,类里面的东西好像也是对着的,结构好像也是想要的,到底是怎么了呢!?
?>
[PHP]
<?php
/*
//购物车类
//购物车ID,商品信息CARTPRO
//CARTPRO结构:一维数组
//CARTPRO([商品ID]=>商品数量,[商品ID]=>商品数量,...)
//时间:4.22/05
*/
class cart
{
var $cartid;
var $cartpro;
//////////////////////////////////////////////////////构造函数,参数USERID,用户的SESSION号
function cart($userid)
{
if(!empty($userid))
{
$this->cartid = $userid;
$this->cartpro= array();
}
else
{
$this->error(0);
exit();
}
}
////////////////////////////////////////////////////////增加商品,参数PROID,商品的ID
function additem($proid)
{
if(array_key_exists($proid, $this->cartpro))
{
$this->cartpro[$proid]++;
}
else
{
$this->cartpro[$proid]=1;
}
//$this->error(2);
}
///////////////////////////////////////////////////////减少商品,参数PROID,商品ID,一次只减一个,如果没有,删除该ID
function removeitem($proid)
{
if(array_key_exists($proid, $this->cartpro) == true)
{
if($this->cartpro[$proid]>1)
$this->cartpro[$proid]--;
else
{
unset($this->cartpro[$proid]);
}
}
else
{
$this->error(1);
}
}
////////////////////////////////////////////////////////删除商品,一次删除一个索引
function delitem($proid)
{
if(array_key_exists($proid, $this->cartpro))
{
unset($this->cartpro[$proid]);
}
else
{
$this->error(1);
}
}
//////////////////////////////////////////////////////显示购物车的内容
function showcart()
{
foreach ($this->cartpro as $key => $value)
print "$key*$value|\n";
}
function datestr()
{
$date = "";
foreach($this->cartpro as $key => $value)
$date.="$key*$value|";
return $date;
}
////////////////////////////////////////////////////////出错提示,参数ERROR!
function error($error)
{
switch ($error)
{
case 0:
echo "<script language='javascript'>alert('你还没有登录,请先登录!');history.go(-1);</script>";exit();
break;
case 1:
echo "<script language='javascript'>alert('你没有该商品了!');history.go(-1);</script>";exit();
break;
case 2:
echo "<script language='javascript'>alert('购物成功,请返回继续购物!');window.location='".$_SERVER['HTTP_REFERER']."';</script>";exit();
break;
default:
echo "<script language='javascript'>alert('程序出错,返回重试!');</script>";exit();
}
}
}
?>
[/PHP]