如何读取和解释 PHP 中任何对象的序列化输出?

How to read and interpret the serialization output of any object in PHP?

鉴于此代码:

$array = array('1' => 'one',
               '2' => 'two',
               '3' => 'three');
$arrayObject = new ArrayObject($array);
$iterator = $arrayObject->getIterator();
echo serialize($iterator);

我得到这个字符串输出:

C:13:"ArrayIterator":111:{x:i:16777216;C:11:"ArrayObject":65:{x:i:0;a:3:{i:1;s:3:"one";i:2;s:3:"two";i:3;s:5:"three";};m:a:0:{}};m:a:0:{}}

现在,假设这是我从输出中理解的内容:

我的问题:

C:13:"ArrayIterator":111:{x:i:16777216;C:11 :"ArrayObject":65:{x:i:0;a:3:{i:1;s:3:"one";i:2;s:3:"two";i:3;s:5:"three";};m:a: 0:{}};m:a:0:{}}

是否有完整的参考或比 PHP 手册更深入地涵盖该主题的内容,因为我没有在 PHP 手册页上找到关于此类对象的信息或示例序列化。

感谢关注!

幸运的是,我可以在源代码的一处找到那些 x: 和 m: 以及字符串追加。我可能是错的,但 "x:" 看起来相对于数组中使用的最后一个索引(所以在这种情况下,序列化甚至保存类的状态)"m:" 被成员评论,我不知道可能是什么。

您可以在此link进一步学习: http://lxr.php.net/xref/PHP_5_6/ext/spl/spl_array.c#1703

1676 /* {{{ proto string ArrayObject::serialize()
1677   Serialize the object */
1678 SPL_METHOD(Array, serialize)
1679 {
1680    zval *object = getThis();
1681    spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
1682    HashTable *aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
1683    zval members, *pmembers;
1684    php_serialize_data_t var_hash;
1685    smart_str buf = {0};
1686    zval *flags;
1687
1688    if (zend_parse_parameters_none() == FAILURE) {
1689        return;
1690    }
1691
1692    if (!aht) {
1693        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
1694        return;
1695    }
1696
1697    PHP_VAR_SERIALIZE_INIT(var_hash);
1698
1699    MAKE_STD_ZVAL(flags);
1700    ZVAL_LONG(flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1701
1702    /* storage */
1703    smart_str_appendl(&buf, "x:", 2);
1704    php_var_serialize(&buf, &flags, &var_hash TSRMLS_CC);
1705    zval_ptr_dtor(&flags);
1706
1707    if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1708        php_var_serialize(&buf, &intern->array, &var_hash TSRMLS_CC);
1709        smart_str_appendc(&buf, ';');
1710    }
1711
1712    /* members */
1713    smart_str_appendl(&buf, "m:", 2);
1714    INIT_PZVAL(&members);
1715    if (!intern->std.properties) {
1716        rebuild_object_properties(&intern->std);
1717    }
1718    Z_ARRVAL(members) = intern->std.properties;
1719    Z_TYPE(members) = IS_ARRAY;
1720    pmembers = &members;
1721    php_var_serialize(&buf, &pmembers, &var_hash TSRMLS_CC); /* finishes the string */
1722
1723    /* done */
1724    PHP_VAR_SERIALIZE_DESTROY(var_hash);
1725
1726    if (buf.c) {
1727        RETURN_STRINGL(buf.c, buf.len, 0);
1728    }
1729
1730    RETURN_NULL();
1731 } /* }}} */