Challenge - 5 Problems
Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of __serialize method in PHP
What is the output of the following PHP code when serialized?
PHP
<?php class User { private string $name; private int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function __serialize(): array { return ['name' => $this->name, 'age' => $this->age]; } } $user = new User('Alice', 30); print_r($user->__serialize());
Attempts:
2 left
💡 Hint
The __serialize method returns an associative array with keys and values representing the object's data.
✗ Incorrect
The __serialize method returns an array with keys 'name' and 'age' holding the respective property values. So the output is an associative array with both keys and values.
❓ Predict Output
intermediate2:00remaining
Output of __unserialize method in PHP
What will be the output of the following PHP code?
PHP
<?php class Product { public string $title; public float $price; public function __unserialize(array $data): void { $this->title = $data['title']; $this->price = $data['price']; } } $data = ['title' => 'Book', 'price' => 12.5]; $product = new Product(); $product->__unserialize($data); echo $product->title . ' costs $' . $product->price;
Attempts:
2 left
💡 Hint
The __unserialize method sets object properties from the given array.
✗ Incorrect
The __unserialize method assigns the 'title' and 'price' from the array to the object's properties. So echoing them prints 'Book costs $12.5'.
🔧 Debug
advanced2:00remaining
Identify the error in __serialize implementation
What error will this PHP code produce when trying to serialize the object?
PHP
<?php class Person { private string $name; private int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } public function __serialize(): array { return json_encode(['name' => $this->name, 'age' => $this->age]); } } $person = new Person('Bob', 25); serialize($person);
Attempts:
2 left
💡 Hint
Check the return type of __serialize method.
✗ Incorrect
The __serialize method must return an array, but here it returns a string from json_encode, causing a TypeError.
📝 Syntax
advanced2:00remaining
Which __unserialize method is syntactically correct?
Which option shows a syntactically correct __unserialize method in PHP?
Attempts:
2 left
💡 Hint
Check the method signature and return type for __unserialize.
✗ Incorrect
The __unserialize method must accept an array and return void. Option A matches this exactly. Option A lacks type hint, C lacks return type, A returns string which is invalid.
🚀 Application
expert2:00remaining
How many items in serialized data after __serialize?
Given this PHP class, how many key-value pairs will the __serialize method return?
PHP
<?php class Session { private string $id; private array $data; private bool $active; public function __construct(string $id, array $data, bool $active) { $this->id = $id; $this->data = $data; $this->active = $active; } public function __serialize(): array { return [ 'id' => $this->id, 'data' => $this->data, 'active' => $this->active ]; } } $session = new Session('abc123', ['user' => 'john'], true); $serialized = $session->__serialize();
Attempts:
2 left
💡 Hint
Count the number of keys returned in the array from __serialize.
✗ Incorrect
The __serialize method returns an array with three keys: 'id', 'data', and 'active'. So the count is 3.