0
0
PHPprogramming~20 mins

__serialize and __unserialize in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
AArray ( [name] => Alice [age] => 30 )
BArray ( [0] => Alice [1] => 30 )
CArray ( [name] => Alice )
DArray ( [age] => 30 )
Attempts:
2 left
💡 Hint
The __serialize method returns an associative array with keys and values representing the object's data.
Predict Output
intermediate
2: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;
AUndefined property: Product::$title
BBook costs $0
CBook costs $12.5
DFatal error: Unserialize failed
Attempts:
2 left
💡 Hint
The __unserialize method sets object properties from the given array.
🔧 Debug
advanced
2: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);
ANo error, serialization works fine
BTypeError: Return value of __serialize() must be of the type array, string returned
CWarning: json_encode() expects parameter 1 to be array, string given
DFatal error: Cannot serialize private properties
Attempts:
2 left
💡 Hint
Check the return type of __serialize method.
📝 Syntax
advanced
2:00remaining
Which __unserialize method is syntactically correct?
Which option shows a syntactically correct __unserialize method in PHP?
A
public function __unserialize(array $data): void {
    $this-&gt;name = $data['name'];
    $this-&gt;age = $data['age'];
}
B
public function __unserialize($data) {
    $this-&gt;name = $data['name'];
    $this-&gt;age = $data['age'];
}
C
public function __unserialize(array $data) {
    $this-&gt;name = $data['name'];
    $this-&gt;age = $data['age'];
}
D
public function __unserialize(array $data): string {
    $this-&gt;name = $data['name'];
    $this-&gt;age = $data['age'];
    return 'done';
}
Attempts:
2 left
💡 Hint
Check the method signature and return type for __unserialize.
🚀 Application
expert
2: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();
A2
B0
C1
D3
Attempts:
2 left
💡 Hint
Count the number of keys returned in the array from __serialize.