三个建议:
1.) 在调用unserialize()之前,对序列化的数据字符串进行手动字符串翻译以进行纠正。
O:6:"Person":1:{s:5:"n1ame";N;}
s:5
表示原始序列化中属性 n1ame
的字符长度,您需要将其更改为 s:4
以将其恢复为 name
,其中的 s
表示字符串数据类型,数字是文本值的长度,在此示例中是属性键。
O:6:"Person":1:{s:4:"name";N;}
你可以尝试一下。
unserialize( str_replace( 's:5:"n1ame"', 's:4:"name"', $previousSerializedPersion ) );
2.) 另一个解决方案是使用__wakup()函数来纠正您的数据。此函数在序列化对象后但在分配和使用之前运行,这可能是一种“更好”的解决方案,因为它在代码中清晰地展现出来。
class Person
{
public $name;
public function __wakeup()
{
if( property_exists( $this, 'n1ame' ) )
{
$this->name = $this->n1ame;
unset( $this->n1ame );
}
}
}
unserialize('O:6:"Person":1:{s:5:"n1ame";s:4:"mark";}');
3.) 使用相同的 __wakup() 概念,但是抛出一个异常。
class Person
{
public $name;
public function __wakeup()
{
if( property_exists( $this, 'n1ame' ) )
{
throw new Exception('oh no this data is bad');
}
}
}
unserialize('O:6:"Person":1:{s:5:"n1ame";s:4:"mark";}');
https://3v4l.org/h8pss
$previousSerializedPersion
,它是否已经包含旧的n1ame
属性? - RiggsFollyserialize(new Person())
中获取$previousSerializedPersion
和Person::$n1ame
。假设我将其存储在Memcache中,然后接收它 :) - zored