PHP - serialize
Trieda
Metóda - serialize
(PHP 4, PHP 5, PHP 7)
The function generates a textual representation of a value which is suitable to be stored. It handles all data types except the resource type. It's possible to recreate the original value from the serialized value later.
Procedurálne
- function serialize (mixed $value) : string
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $value | mixed | The value to be serialized. |
Mávratovej hodnoty
Vracia: string
Returns a string representation of a value which is suitable to be stored.
Note: When saving this value into the database , it's better
to use the BLOB type rather than CHAR or
TEXT.
Príklady
Serialization and deserialization of class instance:
<?php
class Dog
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function sayHello()
{
echo 'Woof woof! I am ' . $this->name . '!';
}
}
$dog = new Dog('Bad');
$packed = serialize($dog);
echo $packed . "\n";
$unpacked = unserialize($packed);
$unpacked->sayHello();
Serialization of array:
$animals = ['Dog', 'Cat', 'Rabbit', 'Mouse'];
echo serialize($animals);
Súvisiace manuály
- function json_encode (mixed $value, int $depth = 512) : string
