Serializable::serialize

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Serializable::serialize β€” ΠŸΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΠ΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π² Π²ΠΈΠ΄Π΅ строки

ОписаниС

public function Serializable::serialize(): ?string

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ строковоС прСдставлСниС ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ строковоС прСдставлСниС ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° ΠΈΠ»ΠΈ null.

Ошибки

ВыбрасываСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Exception ΠΏΡ€ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π΅Π½ΠΈΠΈ Ρ‚ΠΈΠΏΠΎΠ², ΠΎΡ‚Π»ΠΈΡ‡Π½Ρ‹Ρ… ΠΎΡ‚ строки ΠΈΠ»ΠΈ null.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 2 notes

up
21
crog at gustavus dot edu ΒΆ
12 years ago
The documentation here is somewhat misleading. Where it says "This method acts as the destructor of the object. The __destruct() method will not be called after this method," I believe the intent is not that the destructor is not run on the object itself, but that the destructor is not called /as part of the serialization process/. 

That is, the object will still be destructed as it goes out of scope as normal, but the destructor is not called as a part of the object's serialization.
up
4
John ΒΆ
8 years ago
I don't like the bad docs here, which are wrongly claiming that "serialize(): This method acts as the destructor of the object. The __destruct() method will not be called after this method.".

They are simply meaning that serialize() will NOT call __destruct(). That is the ONLY thing they mean.

Your object that is being serialized will continue to live as a normal object. So you should NOT treat serialize() as your destructor! Treat your object as a still-living copy that the user may be using!

The destructor will always run as normal, later, when your object goes out of scope (or is forcibly unset() by you)).

Example with proof:

<?php

class A implements Serializable
{
    public $data = [];

    public function __destruct()
    {
        echo "Destruct of A called.\n";
    }

    public function serialize()
    {
        printf("- Serialize of %s called.\n", static::class);
        return serialize($this->data);
    }

    public function unserialize($serialized)
    {
        printf("- Unserialize of %s called.\n", static::class);
        $this->data = unserialize($serialized);
    }
}

class B extends A
{
    public function __destruct()
    {
        echo "Destruct of B called.\n";
    }
}

$a = new A();
$a->data['inner_b'] = new B();
var_dump($a);

echo "-----------------\n";
echo "Calling serialize($a):\n";
$str = serialize($a);
echo "-----------------\n";
echo "End of script shutdown from here on:...\n";

?>

Result:

```
object(A)#1 (1) {
  ["data"]=>
  array(1) {
    ["inner_b"]=>
    object(B)#2 (1) {
      ["data"]=>
      array(0) {
      }
    }
  }
}
-----------------
Calling serialize($a):
- Serialize of A called.
- Serialize of B called.
-----------------
End of script shutdown from here on:...
Destruct of A called.
Destruct of B called.
```