{"id":6029,"library":"phpserialize","title":"PHP Serialize for Python","description":"phpserialize is a Python library that ports the `serialize` and `unserialize` functions of PHP, allowing Python applications to encode and decode data in PHP's native serialization format. It implements the standard Python serialization interface, providing `dumps` and `loads` functions. The current version is 1.3, with the last update in 2012, making it in a maintenance phase rather than actively developed, though it officially supports Python 3.","status":"maintenance","version":"1.3","language":"en","source_language":"en","source_url":"http://github.com/mitsuhiko/phpserialize","tags":["serialization","php","interoperability","python2","python3"],"install":[{"cmd":"pip install phpserialize","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"dumps","correct":"from phpserialize import dumps"},{"symbol":"loads","correct":"from phpserialize import loads"},{"note":"Used as an `object_hook` for simple PHP object deserialization.","symbol":"phpobject","correct":"from phpserialize import phpobject"}],"quickstart":{"code":"from phpserialize import dumps, loads\n\n# Basic serialization of a string\npython_string = 'Hello World'\nphp_serialized_string = dumps(python_string)\nprint(f\"Serialized string: {php_serialized_string}\")\nassert loads(php_serialized_string) == b'Hello World'\n\n# Deserialization of a simple PHP array string (as bytes for Python 3)\nphp_array_data = b'a:1:{s:3:\"key\";s:5:\"value\";}'\npython_dict = loads(php_array_data)\nprint(f\"Deserialized array: {python_dict}\")\n# Note: Keys and string values are bytes in Python 3, require decoding\nassert python_dict[b'key'] == b'value'\n\n# Deserialization with string decoding\npython_dict_decoded = loads(php_array_data, decode_strings=True)\nprint(f\"Deserialized array (decoded): {python_dict_decoded}\")\nassert python_dict_decoded['key'] == 'value'\n\n# Object serialization example (requires special handling)\n# Using a simple PHP object string from a stdClass\nphp_object_data = b'O:8:\"stdClass\":1:{s:3:\"baz\";s:3:\"qux\";}'\nfrom phpserialize import phpobject\npython_obj = loads(php_object_data, object_hook=phpobject, decode_strings=True)\nprint(f\"Deserialized object: {python_obj}\")\nassert python_obj['baz'] == 'qux'","lang":"python","description":"Demonstrates basic serialization and deserialization of strings and PHP arrays. It highlights the necessity of providing byte strings for input to `loads` in Python 3 and handling byte strings for output, or using `decode_strings=True` for automatic string decoding. Also includes an example of deserializing a simple PHP object using `phpobject` as an `object_hook`."},"warnings":[{"fix":"Ensure input to `loads` is `bytes`. For output, either manually decode `bytes` results (e.g., `value.decode('utf-8')`) or pass `decode_strings=True` to `loads` to get Python unicode strings directly.","message":"In Python 3, `loads` (unserialize) expects byte strings as input. All string-like values (keys, string contents) in the deserialized output will also be byte strings by default. You must explicitly decode them or use the `decode_strings=True` parameter during `loads`.","severity":"gotcha","affected_versions":"1.3 (Python 3 support) onwards"},{"fix":"For PHP objects, use the `object_hook` parameter with `loads`. For simple cases, `phpserialize.phpobject` can convert PHP objects to Python dictionaries. For complex type mapping, implement a custom `object_hook` function. Be aware that Python lists/tuples will be represented as PHP arrays, which are dictionaries in Python.","message":"PHP's serialization format has specific ways of handling objects and arrays. When deserializing PHP objects into Python, a custom `object_hook` is often required to map PHP class names to Python classes or dictionaries. Similarly, Python lists and tuples are not distinct PHP types and are typically serialized as PHP associative arrays.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Test thoroughly on your target Python version. Consider inspecting the `phpserialize3` fork if facing issues, or evaluate alternative, more actively maintained serialization libraries if strict PHP serialization compatibility is not paramount.","message":"The original `phpserialize` library has not seen active development since version 1.3 in 2012. While it states Python 3 support, newer Python versions might encounter subtle compatibility issues not addressed by new releases. A community fork, `phpserialize3`, exists specifically for Python 3, which might offer more up-to-date compatibility, although it also appears to be infrequently updated.","severity":"deprecated","affected_versions":"Post-1.3, specifically with newer Python 3.x versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}