Complete the code to define the __serialize method that returns the object's data as an array.
<?php class User { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function __serialize(): array { return [1]; } } ?>
The __serialize method must return an array of the object's data. Using array() syntax is correct here.
Complete the code to define the __unserialize method that restores the object's properties from the given data array.
<?php class User { private $name; private $age; public function __unserialize(array $data): void { $this->name = $data[[1]]; $this->age = $data["age"]; } } ?>
The __unserialize method receives an array with keys matching the property names. So to restore $name, use the key "name".
Fix the error in the __serialize method to correctly return the object's data as an array.
<?php class Product { private $id; private $price; public function __serialize(): array { return [1]; } } ?>
The __serialize method must return an associative array with keys and values, not a serialized string or a numeric array.
Fill both blanks to complete the __unserialize method that restores the object's properties from the data array.
<?php class Product { private $id; private $price; public function __unserialize(array $data): void { $this->id = $data[[1]]; $this->price = $data[[2]]; } } ?>
The __unserialize method uses the keys "id" and "price" to restore the respective properties from the data array.
Fill all three blanks to complete the class with __serialize and __unserialize methods that handle the properties correctly.
<?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"]; } } ?>
The __serialize method returns an associative array with keys matching the property names: "title", "author", and "pages".