0

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 :)

sys_debug
  • 3,883
  • 17
  • 67
  • 98
  • 1
    Just confirming, is your ItemObject definition visible to your script? – Nitesh Apr 18 '12 at 10:45
  • This question may be helpful: http://stackoverflow.com/questions/965611/forcing-access-to-php-incomplete-class-object-properties – F21 Apr 18 '12 at 10:48
  • mate thanks an eye opener...I was doing the wrong foreach cnodition...I had to let it be $basketObjArray as $item then in body do $item->attribute(s) – sys_debug Apr 18 '12 at 10:51

2 Answers2

0

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

bardiir
  • 14,556
  • 9
  • 41
  • 66
0

I read the best way to store this array of objects in MySQL table

Where did you read that? There are a lot of arguments why this is probably the worst way to store data.

BTW, typically this error occurs when you try to unserialize an object which doesn't have a class definition in scope.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • Thanks mate but I did read it somewhere...if I come across it again over the net, I will post it here. It is working now. – sys_debug Apr 18 '12 at 12:34