Skip to content

DateTime::__(un)serialize Improve the examples, add an Error warning, amend method names in the Example section #4661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 30 additions & 3 deletions reference/datetime/datetimeinterface/serialize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,51 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>DateTime::serialize</function> example</title>
<title><methodname>DateTime::__serialize</methodname> example</title>
<programlisting role="php">
<![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>
&examples.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>
<para>
An <classname>Error</classname> is thrown when attempting to unserialize a custom
<classname>DateTime</classname> object if the <link linkend="object.serialize">__serialize()</link> is
defined but the <link linkend="object.unserialize">__unserialize()</link> is missing.
</para>
</warning>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
Expand Down
33 changes: 29 additions & 4 deletions reference/datetime/datetimeinterface/unserialize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,33 @@
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
The <classname>DateTime</classname> object.
&return.void;
</para>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>DateTime::unserialize</function> example</title>
<title><methodname>DateTime::__unserialize</methodname> example</title>
<programlisting role="php">
<![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";

$this->__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>
Expand All @@ -76,6 +90,17 @@ object(DateTime)#1 (3) {
</example>
</refsect1>

<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
An <classname>Error</classname> is thrown when attempting to unserialize a custom
<classname>DateTime</classname> object if the <link linkend="object.serialize">__serialize()</link> is
defined but the <link linkend="object.unserialize">__unserialize()</link> is missing.
</para>
</warning>
</refsect1>

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