Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions reference/datetime/datetimeinterface/serialize.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: malferov Status: ready -->
<!-- EN-Revision: d0a271b7ed3149b465a364fadd3f1212d01efd22 Maintainer: malferov Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="datetime.serialize" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
Expand Down Expand Up @@ -50,19 +50,45 @@
<![CDATA[
<?php

$date = new DateTime('2025-03-27');
class CustomDateTimeImmutable extends DateTimeImmutable
{
#[\Override]
public function __serialize(): array
{
return [
'date' => $this->format(DateTimeInterface::W3C),
'timestamp' => $this->getTimestamp(),
'timezone' => $this->getTimeZone()->getName(),
'to' => 'Drink a cup of coffee',
];
}
}

$date = new CustomDateTimeImmutable('2025-03-27');
var_dump(serialize($date));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(114) "O:8:"DateTime":3:{s:4:"date";s:26:"2025-03-27 00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}"
string(171) "O:23:"CustomDateTimeImmutable":4:{s:4:"date";s:25:"2025-03-27T00:00:00+00:00";s:9:"timestamp";i:1743033600;s:8:"timezone";s:3:"UTC";s:2:"to";s:21:"Drink a cup of coffee";}"
]]>
</screen>
</example>
</refsect1>

<refsect1 role="notes">
&reftitle.notes;
<warning>
<simpara>
При попытке десериализовать пользовательский объект <classname>DateTime</classname>
выбрасывается ошибка <exceptionname>Error</exceptionname>,
если метод <link linkend="object.serialize">__serialize()</link> определили,
а определение метода <link linkend="object.unserialize">__unserialize()</link> не добавили.
</simpara>
</warning>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
Expand Down
45 changes: 35 additions & 10 deletions reference/datetime/datetimeinterface/unserialize.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: malferov Status: ready -->
<!-- EN-Revision: d0a271b7ed3149b465a364fadd3f1212d01efd22 Maintainer: malferov Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="datetime.unserialize" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
Expand Down Expand Up @@ -47,10 +47,9 @@

<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Метод не возвращает значений, а только обрабатывает промежуточное представление объекта <classname>DateTime</classname>
или другого объекта даты и времени, совместимого с интерфейсом.
</para>
<simpara>
&return.void;
</simpara>
</refsect1>

<refsect1 role="examples">
Expand All @@ -61,26 +60,52 @@
<![CDATA[
<?php

$serializedDate = 'O:8:"DateTime":3:{s:4:"date";s:26:"2025-03-27 00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}';
var_dump(unserialize($serializedDate));
class CustomDateTimeImmutable extends DateTimeImmutable
{
#[\Override]
public function __unserialize(array $data): void
{
echo "Time to `{$data['to']}`:\n\n";

parent::__construct($data['date'], new DateTimeZone($data['timezone']));
}
}

$serializedData = 'O:23:"CustomDateTimeImmutable":4:{s:4:"date";s:25:"2025-03-27T00:00:00+00:00";s:9:"timestamp";i:1743033600;s:8:"timezone";s:3:"UTC";s:2:"to";s:21:"Drink a cup of coffee";}';

var_dump(unserialize($serializedData));
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(DateTime)#1 (3) {
Time to `Drink a cup of coffee`:

object(CustomDateTimeImmutable)#1 (3) {
["date"]=>
string(26) "2025-03-27 00:00:00.000000"
["timezone_type"]=>
int(3)
int(1)
["timezone"]=>
string(3) "UTC"
string(6) "+00:00"
}
]]>
</screen>
</example>
</refsect1>

<refsect1 role="notes">
&reftitle.notes;
<warning>
<simpara>
При попытке десериализовать пользовательский объект <classname>DateTime</classname>
выбрасывается ошибка <exceptionname>Error</exceptionname>,
если метод <link linkend="object.serialize">__serialize()</link> определили,
а определение метода <link linkend="object.unserialize">__unserialize()</link> не добавили.
</simpara>
</warning>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
Expand Down
Loading