反序列化对象数组

时间:2014-03-20 18:28:17

标签: php

我有一个这样的场景,可以动态地将对象转储到会话中并将所有对象恢复回来,但看起来好像我做错了。

以下是将对象序列化为会话

的代码段

$CMO是一个对象。

$SerializedCmo = serialize($CMO);
if(isset($_SESSION['count']))
{
    $count = $_SESSION['count'] ;
    $count++;
}
else
    $count = 0;

$_SESSION['count'] = $count;

$_SESSION[$CaseAssignID.$count] = $SerializedCmo;

以下是从会话

反序列化对象的代码段
$count =  $_SESSION['count'];
$CaseAssignId = $_SESSION['assignmentvalue'];

$CMO = new Condition_Management_Information();

for ($i =0 ; $i <= $count ; $i++)
{
  $UnserializedCmo = $_SESSION[$CaseAssignId.$i];
  $CMO = unserialize($UnserializedCmo);

}

count和CaseassignId的值是正确的,但$ UnserializedCmo和$ CMO的var_dump分别为NULL和布尔值(false)。

3 个答案:

答案 0 :(得分:0)

这个怎么样:

function SerializeTest()
{
    /* Condition_Management_Information */ $CMO = GetObjectFromSomewhere();

    $CaseAssignID = GetIDPrefixFromSomeWhere();

    $SerializedCmo = serialize($CMO);

    // get current qty of items
    if(isset($_SESSION['howmany']))
    {
        $howmany = (int)$_SESSION['howmany'] ;
    }
    else
        $howmany = (int)0;

    // increment qty
    $howmany++;    
    $_SESSION['howmany'] = $howmany;

    $SessionID = $CaseAssignID . "_" . $howmany;

    $_SESSION[$SessionID] = $SerializedCmo;
} // function SerializeTest(....)

另一种方法:

function DeserializeTest()
{
    // ALWAYS use a cast for integer objects, otherwise you'll get a NULL value,
    // instead of a ZERO value
    $howmany = (int)$_SESSION['howmany'];

    // $CaseAssignId = $_SESSION['assignmentvalue'];    
    $CaseAssignID = GetIDPrefixFromSomeWhere();

    // do not need to do this:
    // $CMO = new Condition_Management_Information();

    /* Condition_Management_Information */ $CMO = null;

    for ($i =0 ; $i <= $howmany ; $i++)
    {
      $SessionID = $CaseAssignID . "_" . $i;

      // verify objects from session
      // maybe the error is here
        if(isset($_SESSION[$SessionID]))
        {
            $UnserializedCmo = $_SESSION[$SessionID];

                    // do not assume something is stored
                    if ($UnserializedCmo != null)
                    {
              $CMO = unserialize($UnserializedCmo);
              DoSomethingWithEachObject($CMO);
                    }
        }
        else
            $CMO = null;
    } // for
} // function DeserializeTest(....)

干杯

答案 1 :(得分:0)

在序列化和反序列化之前,您需要为要序列化的对象创建一个类并包含它:

//script.php
include "objectClass.php"
$obj= new objectClass(a,b,c);
$_SESSION['something'] = serialize($obj);

//other_script.php
include "objectClass.php"
$obj= unserialize($_SESSION['something']);
echo $obj->getA();

的问候。

答案 2 :(得分:0)

未经测试的代码:

我建议您将'$ SerializedCmo'附加到名为'SerializedCmo'的数组中。

这样你就不必知道它的索引了。

以下是将对象序列化为会话

的代码段
$SerializedCmo = serialize($CMO);
if (!isset($_SESSION['SerializedCmo']))
{
  $_SESSION['SerializedCmo'] = array();
}


$_SESSION['SerializedCmo'][] = $SerializedCmo; // append to array...

以下是从会话

反序列化对象的代码段
$CMO = new Condition_Management_Information();

$allCMO = array(); // unserialized objects in here

if (isset($_SESSION['SerializedCmo']))
{
   foreach ($_SESSION['SerializedCmo'] as $SerializedCmo)
   {
     $AllCMO[] = unserialize($SerializedCmo); // append to CMO object array


   }
}