Created
August 2, 2020 23:37
-
-
Save Eyad-Bereh/30b3524413027337b38fe34307771d5b to your computer and use it in GitHub Desktop.
An example demonstrating how to implement Serializable and JsonSerializable interfaces in PHP - Exclusive for the group: https://www.facebook.com/groups/HTML.CSSandJavaScript/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //php 7.2.24 | |
declare(strict_types = 1); | |
class User implements Serializable, JsonSerializable { | |
private $id; /** @var string user id */ | |
private $firstname; /** @var string user's firstname */ | |
private $lastname; /** @var string user's lastname */ | |
private $email; /** @var string user's email */ | |
/** | |
* *********************************************************************************** | |
* *********************************************************************************** | |
* Just adding the basic functionality | |
* *********************************************************************************** | |
* *********************************************************************************** | |
*/ | |
/** | |
* Initialize class fields | |
* | |
* @param string $id set user id | |
* @param string $firstname set user firstname | |
* @param string $lastname set user lastname | |
* @param string $email set user email | |
* | |
* @return void | |
* | |
*/ | |
public function __construct(string $id, string $firstname, string $lastname, string $email) { | |
$this->id = $id; | |
$this->firstname = $firstname; | |
$this->lastname = $lastname; | |
$this->email = $email; | |
} | |
/** | |
* *********************************************************************************** | |
* *********************************************************************************** | |
* Implementing the Serializable interface | |
* *********************************************************************************** | |
* *********************************************************************************** | |
*/ | |
/** | |
* Returns a string representation of the object | |
* | |
* @return string | |
* | |
*/ | |
public function serialize() : string { | |
// NOTICE THAT WE'RE SEPARATING VALUES USING SEMICOLON | |
$str = ""; | |
$str .= "$this->id;"; | |
$str .= "$this->firstname;"; | |
$str .= "$this->lastname;"; | |
$str .= "$this->email"; | |
return $str; | |
} | |
/** | |
* Constructs an object from string representation | |
* | |
* @param string $serialized the string representation of the object | |
* | |
* @throws UnexpectedValueException if the given $serialized isn't a string | |
* | |
* @return void | |
*/ | |
public function unserialize($serialized) : void { | |
if (!is_string($serialized)) { | |
$type = gettype($serialized); | |
throw new UnexpectedValueException("User::unserialize(serialized = $serialized) failed because serialized parameter is expected to be a string but $type was given"); | |
} | |
$info = explode(";", $serialized); // Remember how we separated values using semicolons ! | |
// By Convention from serialization process: | |
$this->id = $info[0]; // First field is the user id | |
$this->firstname = $info[1]; // Second field is the user firstname | |
$this->lastname = $info[2]; // Third field is the user lastname | |
$this->email = $info[3]; // Fourth field is the user email | |
} | |
/** | |
* *********************************************************************************** | |
* *********************************************************************************** | |
* Implementing the JsonSerializable interface | |
* *********************************************************************************** | |
* *********************************************************************************** | |
*/ | |
/** | |
* Specify what data to serialize as JSON | |
* | |
* | |
* @return array the fields to serialize | |
* | |
*/ | |
public function jsonSerialize() { | |
$info = [ | |
"id" => $this->id, | |
"firstname" => $this->firstname, | |
"lastname" => $this->lastname, | |
"email" => $this->email | |
]; | |
return $info; | |
} | |
} | |
// Let's try this ... | |
$user = new User("984ed1d7-a342-48f6-89de-fb0e2e4b394a", "John", "Doe", "john.doe@example.com"); | |
// Let's get the custom serialized string | |
$serialized = serialize($user); | |
echo "Serialization using serialize():\n"; | |
echo $serialized . "\n"; | |
echo "--------------------------------------------------\n"; | |
// Now let's unserialize it | |
echo "Deserialization using unserialize():\n"; | |
$object = unserialize($serialized); | |
var_dump($object); | |
echo "--------------------------------------------------\n"; | |
// Now let's serialize the object using json_encode() | |
echo "Serialization using json_encode():\n"; | |
$serialized = json_encode($user); | |
echo $serialized . "\n"; | |
echo "--------------------------------------------------\n"; | |
// Now let's unserialize the object using json_decode() | |
echo "Deserialization using json_decode():\n"; | |
$object = json_decode($serialized); | |
var_dump($object); | |
echo "--------------------------------------------------\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment