Capy-Bearer Documentation

php_decode

lib call

Capy

function php_decode(value : string) any

Description

php_decode parses PHP data without running PHP, loading classes, or invoking callbacks. It supports null, booleans, integers, floating-point numbers, byte strings, and arrays.

PHP arrays retain entry order. Integer keys become decimal strings, and string keys remain byte strings. Equal keys use the last value without moving the first position. A nonempty array becomes a list only when all keys are PHP integers in sequential order from zero. String keys such as "0" remain map keys. An empty PHP array decodes as a map because PHP has no separate empty-list representation. Re-encoding a map uses string keys, so sparse integer key types do not survive.

PHP integers decode as exact s64 values across the full signed 64-bit range. PHP floating-point values decode as f64. Strings preserve NUL bytes and invalid UTF-8.

The codec limits input to 8 MiB and bounds nesting and entries. It requires complete input consumption. Object, custom-object, enum, and reference tokens are unsupported. The decoder does not instantiate objects or call PHP.

This function accepts standalone serialized values, including the complete array used by PHP's php_serialize session handler. Legacy php and php_binary session formats need separate framing code. Do not split serialized strings at delimiters.

Parameters

value : one complete PHP serialization byte string

Return Values

{ok: true, value: ...} on success, or {ok: false, error: "...", error_code: "..."} on failure. The value field contains the decoded Capy value, including none for PHP null.

Errors

Malformed lengths, truncated or trailing data, unsupported tokens, limits, and out-of-range integers return ok: false with error_code and error. Failure does not throw, trap, or stop the request. Check ok, not the truth value of value. PHP false and null are valid values.

Example

Capy

try decoded := php_decode("a:1:{s:4:\"name\";s:3:\"Ada\";}")
{
	print(decoded.name, "\n")
}
else
{
	print(decoded.error, "\n")
}

Output

Ada