PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous.
The examples below begin with the core behavior, then move to an application-oriented use.
How Object Serialization Risks and Safe Alternatives Works
PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$settings = ["mode" => "safe"];
$serialized = serialize($settings);
echo strlen($serialized);
Output:
28
PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$data = json_decode('{"role":"viewer"}', true, 512, JSON_THROW_ON_ERROR);
if (!is_string($data["role"] ?? null)) { throw new InvalidArgumentException("Invalid role"); }
echo $data["role"];
Output:
viewer
Never use unrestricted unserialize() on user-controlled data; prefer validated JSON arrays or another non-object format.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$payload = json_encode(["id" => 42, "status" => "ready"], JSON_THROW_ON_ERROR);
$data = json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
if (!is_int($data["id"] ?? null)) { throw new InvalidArgumentException("Invalid ID"); }
echo $data["status"];
Output:
ready
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Never use unrestricted unserialize() on user-controlled data; prefer validated JSON arrays or another non-object format.
- PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Object Serialization Risks and Safe Alternatives | PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous. |
| Alternative approach | JSON supports interoperable data records without PHP object rehydration. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Object Serialization Risks and Safe Alternatives | Never use unrestricted unserialize() on user-controlled data; prefer validated JSON arrays or another non-object format. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | JSON supports interoperable data records without PHP object rehydration. |
Key Takeaways
- PHP object deserialization can instantiate objects and trigger magic methods, making untrusted serialized payloads dangerous.
- Never use unrestricted unserialize() on user-controlled data; prefer validated JSON arrays or another non-object format.
- Use the example that matches the values, runtime version and dependencies in your application.
Object Serialization Risks and Safe Alternatives is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.