Usage

Due to the fact that PHP doesn’t know the concept of lists, lists
are serialized like hash-maps in PHP. As a matter of fact the
reverse value of a serialized list is a dict:

>>> loads(dumps(range(2)))
{0: 0, 1: 1}

If you want to have a list again, you can use the dict_to_list
helper function:

>>> dict_to_list(loads(dumps(range(2))))
[0, 1]

It’s also possible to convert into a tuple by using the dict_to_tuple
function:

>>> dict_to_tuple(loads(dumps((1, 2, 3))))
(1, 2, 3)

Another problem are unicode strings. By default unicode strings are
encoded to ‘utf-8’ but not decoded on unserialize. The reason for
this is that phpserialize can’t guess if you have binary or text data
in the strings:

>>> loads(dumps(u'Hello W\xf6rld'))
'Hello W\xc3\xb6rld'

If you know that you have only text data of a known charset in the result
you can decode strings by setting decode_strings to True when calling
loads:

This feature however is not supported in PHP. PHP will only unserialize
the first object.

Array Serialization

Starting with 1.2 you can provide an array hook to the unserialization
functions that are invoked with a list of pairs to return a real array
object. By default dict is used as array object which however means
that the information about the order is lost for associative arrays.

For example you can pass the ordered dictionary to the unserilization
functions:

Object Serialization

PHP supports serialization of objects. Starting with 1.2 of phpserialize
it is possible to both serialize and unserialize objects. Because class
names in PHP and Python usually do not map, there is a separate
object_hook parameter that is responsible for creating these classes.

For a simple test example the phpserialize.phpobject class can be used:

An object hook is a function that takes the name of the class and a dict
with the instance data as arguments. The instance data keys are in PHP
format which usually is not what you want. To convert it into Python
identifiers you can use the convert_member_dict function. For more
information about that, have a look at the next section. Here an
example for a simple object hook:

PHP’s Object System

The PHP object system is derived from compiled languages such as Java
and C#. Attributes can be protected from external access by setting
them to protected or private. This does not only serve the purpose
to encapsulate internals but also to avoid name clashes.

In PHP each class in the inheritance chain can have a private variable
with the same name, without causing clashes. (This is similar to the
Python __var name mangling system).

As you can see, reassigning attributes on a php object will try
to change a private or protected attribute with the same name.
Setting an unknown one will create a new public attribute:

>>> user.is_admin = True
>>> user.__php_vars__['is_admin']
True

To convert the phpobject into a dict, you can use the _asdict
method:

>>> d = user._asdict()
>>> d['username']
'admin'

Python 3 Notes

Because the unicode support in Python 3 no longer transparently
handles bytes and unicode objects we had to change the way the
decoding works. On Python 3 you most likely want to always
decode strings. Because this would totally fail on binary data
phpserialize uses the “surrogateescape” method to not fail on
invalid data. See the documentation in Python 3 for more
information.