无法从var_导出的数据(在PHP中)重新创建对象
在live server上运行的脚本从以下行发送数据:无法从var_导出的数据(在PHP中)重新创建对象,php,serialization,deserialization,Php,Serialization,Deserialization,在live server上运行的脚本从以下行发送数据: $log_output .= '<br>'.__LINE__.'<br>recordings_data='.var_export($recordings_data,TRUE); serialize()和unserialize()是转储/加载PHP对象的公认方法。您还可以使用json\u encode()和json\u decode()来实现。您想使用var\u export()有什么原因吗 编辑:只能unseria
$log_output .= '<br>'.__LINE__.'<br>recordings_data='.var_export($recordings_data,TRUE);
serialize()
和unserialize()
是转储/加载PHP对象的公认方法。您还可以使用json\u encode()
和json\u decode()
来实现。您想使用var\u export()
有什么原因吗
编辑:只能unserialize()
序列化()-var\u dump()
的结果采用完全不同的格式,除非使用eval()
,否则不是为导入而设计的
例如:
$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()
)再次创建对象。serialize()
和unserialize()
是普遍接受的转储/加载PHP对象的方法。您还可以使用json\u encode()
和json\u decode()
来实现。您想使用var\u export()
有什么原因吗
编辑:只能unserialize()
序列化()-var\u dump()
的结果采用完全不同的格式,除非使用eval()
,否则不是为导入而设计的
例如:
$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\u encode()
),可以使用相反的方法(unserialize()
或json\u decode()
)再次创建对象。var\u export()用于将数据结构转储到文件中。然后您必须eval()
或include()
或require()
该文件。serialize()格式与var_导出完全不同
var_export生成有效的PHP代码来定义数据结构,就像您自己编写了数组/对象定义一样。以完全不同的格式序列化/取消序列化写出来,它们是不可互换的。var\u export()用于将数据结构转储到文件中。然后您必须eval()
或include()
或require()
该文件。serialize()格式与var_导出完全不同
var_export生成有效的PHP代码来定义数据结构,就像您自己编写了数组/对象定义一样。以完全不同的格式序列化/取消序列化写出来,它们是不可互换的。如果在
stdClass
的实例上调用var\u export()
,它会尝试使用:\u set\u state()
导出它,但由于某些原因,stdClass
中没有实现它
但是,将关联数组投射到对象通常会产生相同的效果(至少在我的例子中是这样)。因此,我编写了一个改进的
函数,将stdClass
的实例转换为(对象)数组()
调用。如果您选择导出任何其他类的对象,我建议您在这些类中实现::\uuu set\u 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';
?>
如果在
stdClass
的实例上调用var\u export()
,它会尝试使用:\u set\u state()
导出它,但由于某些原因,stdClass
中未实现该功能
但是,将关联数组投射到对象通常会产生相同的效果(至少在我的例子中是这样)。因此,我编写了一个改进的
函数,将stdClass
的实例转换为(对象)数组()
调用。如果您选择导出任何其他类的对象,我建议您在这些类中实现::\uuu set\u 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。好了。我之前发表了评论,因为我认为这只是另一个BOM问题,但我只是看到了OP在做什么。:)+1.好了。我之前发表了评论,因为我认为这只是另一个BOM问题,但我只是看到了OP在做什么。:)