dongyang1518 2012-04-18 10:41
浏览 86

PHP反序列化数组

So I am dealing with customer shopping basket. The best way I found suitable for my purpose was to store objects (with setting properties for each item attribute) in an array. So I ended up with an array of objects ItemObject type.


I read the best way to store this array of objects in MySQL table, was to serialize it hence converting it to string and then storing it. Then when I need it I can unserialize() it with the php function.

I did just that...and now that I unserialize it, I get an error....the code follows for the ItemObject, and the unserialization part. How can I unserialize that string back to array of objects of type ItemObjects so that I could iterate and print out the details of each item?

<?php
$dateSelected = $_GET['date'];
echo "Date: ".$dateSelected . "<br>";
mysql_connect();
mysql_select_db("dashjkd");
$result = mysql_query("SELECT order_object FROM orders WHERE orderDate = '".$dateSelected."'");
if(mysql_num_rows($result) > 0){
    $temp;
    while($row = mysql_fetch_assoc($result)){
        $temp = $row['order_object'];
        //print_r($temp);
    }
    echo "Array: <br>";

    $basketObjArray = array();
    $basketObjArray = unserialize($temp);
    print_r($basketObjArray);
    foreach($basketObjArray as $key => $value)
    {
        echo "Key: " . $key;
        echo "Value: ". $value;
    }

}else{
    echo "Something went horribly wrong..try again!";
}
?>

And this unserilization code leads to this error and strange output:

Date: 2012-04-18
Array:
Array ( [0] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ItemObject [itemNameVar] => whitethoab [itemQtyVar] => 12 [itemPriceVar] => 10 ) [1] => stdClass Object ( [itemNameVar] => woolthoab [itemPriceVar] => 10 [itemQtyVar] => 2 ) [2] => stdClass Object ( [itemNameVar] => shemag [itemPriceVar] => 4 [itemQtyVar] => 1 ) [3] => stdClass Object ( [itemNameVar] => jacket [itemPriceVar] => 12 [itemQtyVar] => 2 ) ) Key: 0
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in /home/sdsds/public_html/laundry/customerorder.php on line 21

the itemObject I designed was very simple and worked before when I attempted to display the array before serializing it:

class ItemObject{
                            public $itemNameVar;
                            public $itemQtyVar;
                            public $itemPriceVar;

                        }

Thanks for the help :)

  • 写回答

2条回答 默认 最新

  • doumu5023 2012-04-18 10:51
    关注

    Make sure the class you try to serialize/unserialize has got both __sleep and __wakeup magic functions.

    Inside those functions you can define how this class or instances thereof need to be serialized and how to reinitalise them with the serialized data. For example __wakeup can be used to create a new database connection for the object as the connection from before the serialization is likely not available anymore.

    http://www.php.net/manual/en/language.oop5.magic.php#object.sleep

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行