0
0
PHPprogramming~10 mins

__serialize and __unserialize in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the __serialize method that returns the object's data as an array.

PHP
<?php
class User {
    private $name;
    private $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function __serialize(): array {
        return [1];
    }
}
?>
Drag options to blanks, or click blank then click option'
Aarray("name" => $this->name, "age" => $this->age)
B["name" => $this->name, "age" => $this->age]
C[$this->name, $this->age]
Dserialize($this)
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a serialized string instead of an array.
Returning a numeric array without keys.
2fill in blank
medium

Complete the code to define the __unserialize method that restores the object's properties from the given data array.

PHP
<?php
class User {
    private $name;
    private $age;

    public function __unserialize(array $data): void {
        $this->name = $data[[1]];
        $this->age = $data["age"];
    }
}
?>
Drag options to blanks, or click blank then click option'
A1
B"name"
C0
D"age"
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric indexes instead of string keys.
Mixing up keys for name and age.
3fill in blank
hard

Fix the error in the __serialize method to correctly return the object's data as an array.

PHP
<?php
class Product {
    private $id;
    private $price;

    public function __serialize(): array {
        return [1];
    }
}
?>
Drag options to blanks, or click blank then click option'
Aarray("id", $this->id, "price", $this->price)
Bserialize(["id" => $this->id, "price" => $this->price])
C["id" => $this->id, "price" => $this->price]
D[$this->id, $this->price]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a serialized string instead of an array.
Returning a numeric array without keys.
4fill in blank
hard

Fill both blanks to complete the __unserialize method that restores the object's properties from the data array.

PHP
<?php
class Product {
    private $id;
    private $price;

    public function __unserialize(array $data): void {
        $this->id = $data[[1]];
        $this->price = $data[[2]];
    }
}
?>
Drag options to blanks, or click blank then click option'
A"id"
B"price"
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric indexes instead of string keys.
Swapping the keys for id and price.
5fill in blank
hard

Fill all three blanks to complete the class with __serialize and __unserialize methods that handle the properties correctly.

PHP
<?php
class Book {
    private $title;
    private $author;
    private $pages;

    public function __serialize(): array {
        return [[1] => $this->title, [2] => $this->author, [3] => $this->pages];
    }

    public function __unserialize(array $data): void {
        $this->title = $data["title"];
        $this->author = $data["author"];
        $this->pages = $data["pages"];
    }
}
?>
Drag options to blanks, or click blank then click option'
A"title"
B"author"
C"pages"
D"book"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect or inconsistent keys.
Using a single key for all properties.