无法从var_exported数据重新创建对象(在PHP中)

时间:2011-12-17 14:32:38

标签: php serialization deserialization

在我们的实时服务器上运行的脚本从以下行发送数据:

$log_output .= '<br>'.__LINE__.'<br>recordings_data='.var_export($recordings_data,TRUE);

看起来像这样:

  

recordings_data = stdClass :: __ set_state(array('RecordingLongResponse')   =&GT; array(0 =&gt; stdClass :: __ set_state(...),1 =&gt; stdClass :: __ set_state(),2 =&gt; stdClass :: __ set_state(),3 =&gt;   stdClass :: __ set_state(array('roomStartDate'=&gt;'1321977120000',   'roomEndDate'=&gt; '1321977120000','recordingURL'=&gt; 'serverURL1',   'secureSignOn'=&gt; false,'recordingId'=&gt; '1287268130290',   'creationDate'=&gt; '1321977120000','recordingSize'=&gt; '6765975',   'roomName'=&gt; '利益相关者分析','sessionId'=&gt; '1287268130229',   )),...),))

我不确定如何“重新创建”该对象。我尝试反序列化它:

 $recording_data_ser= file_get_contents('elm-ser-data.txt'); // where I've saved everything after the '='
 $recording_data = unserialize($recording_data_ser);

3 个答案:

答案 0 :(得分:4)

serialize()unserialize()是普遍接受的转储/加载PHP对象的方法。您也可以使用json_encode()json_decode()来完成此操作。您是否有理由使用var_export()

修改:unserialize() - serialize()的结果只有var_dump(),格式完全不同,除非您使用eval(),否则不会导入导入。< / p>

例如:

$arr = array('foo' => 'bar');

var_export($arr);
#=> array (
#=>  'foo' => 'bar',
#=> )

echo serialize($arr);
#=> a:1:{s:3:"foo";s:3:"bar";}

echo json_encode($arr);
#=> {"foo":"bar"}

获得序列化数据后(通过serialize()json_encode()),您可以使用相反的方法(分别为unserialize()json_decode())重新创建对象

答案 1 :(得分:3)

var_export()旨在将数据结构转储到文件中。然后,您必须eval()include()require()该文件。 serialize()格式与var_export完全不同。

var_export生成有效的PHP代码来定义数据结构,就像您自己写出数组/对象定义一样。序列化/反序列化以完全不同的格式写出,并且它们不可互换。

答案 2 :(得分:2)

如果您在var_export()的实例上致电stdClass,则会尝试使用::__set_state()导出stdClass,由于某种原因,improved_var_export()没有实施。{ / p>

但是,将关联数组转换为对象通常会产生相同的效果(至少在我的情况下会这样做)。所以我编写了一个stdClass函数来将(object) array ()的实例转换为::__set_state()个调用。如果您选择导出任何其他类的对象,我建议您在这些类中实现<?php /** * An implementation of var_export() that is compatible with instances * of stdClass. * @param mixed $variable The variable you want to export * @param bool $return If used and set to true, improved_var_export() * will return the variable representation instead of outputting it. * @return mixed|null Returns the variable representation when the * return parameter is used and evaluates to TRUE. Otherwise, this * function will return NULL. */ function improved_var_export ($variable, $return = false) { if ($variable instanceof stdClass) { $result = '(object) '.improved_var_export(get_object_vars($variable), true); } else if (is_array($variable)) { $array = array (); foreach ($variable as $key => $value) { $array[] = var_export($key, true).' => '.improved_var_export($value, true); } $result = 'array ('.implode(', ', $array).')'; } else { $result = var_export($variable, true); } if (!$return) { print $result; return null; } else { return $result; } } // Example usage: $obj = new stdClass; $obj->test = 'abc'; $obj->other = 6.2; $obj->arr = array (1, 2, 3); improved_var_export((object) array ( 'prop1' => true, 'prop2' => $obj, 'assocArray' => array ( 'apple' => 'good', 'orange' => 'great' ) )); /* Output: (object) array ('prop1' => true, 'prop2' => (object) array ('test' => 'abc', 'other' => 6.2, 'arr' => array (0 => 1, 1 => 2, 2 => 3)), 'assocArray' => array ('apple' => 'good', 'orange' => 'great')) */ // Example implementation in context of OP $export = improved_var_export($data, true); file_put_contents(dirname(__FILE__).'/data.php', '<'.'?php return '.$export.'; ?'.'>'); // "Unserialization" (evaluation) $import = include dirname(__FILE__).'/data.php'; ?>

{{1}}