language/oop5/magic.xml
5e15a6c3e4d5819102361ae78e73c90a06238c8a
...
...
@@ -96,10 +96,15 @@
96
96
Use <link linkend="object.serialize">__serialize()</link> instead.
97
97
</para>
98
98
</note>
99
+
<note>
100
+
<para>
101
+
As of PHP 8.0.0, returning a value which is not an array from <link linkend="object.sleep">__sleep()</link> generates a warning. Previously, it generated a notice.
102
+
</para>
103
+
</note>
99
104
<para>
100
105
The intended use of <link linkend="object.sleep">__sleep()</link> is to commit pending
101
106
data or perform similar cleanup tasks. Also, the function is
102
-
useful if a very large objects doesn't need to be saved completely.
107
+
useful if a very large object doesn't need to be saved completely.
103
108
</para>
104
109
<para>
105
110
Conversely, <function>unserialize</function> checks for the
...
...
@@ -271,6 +276,13 @@ class Connection
271
276
is disabled.
272
277
</para>
273
278
<para>
279
+
A <interfacename>Stringable</interfacename> object will
280
+
<emphasis>not</emphasis> be accepted by a <type>string</type> type declaration if
281
+
<link linkend="language.types.declarations.strict">strict typing</link>
282
+
is enabled. If such behaviour is wanted the type declaration must accept
283
+
<interfacename>Stringable</interfacename> and <type>string</type> via a union type.
284
+
</para>
285
+
<para>
274
286
As of PHP 8.0.0, any class that contains a <link linkend="object.tostring">__toString()</link>
275
287
method will also implicitly implement the <interfacename>Stringable</interfacename> interface, and will
276
288
thus pass type checks for that interface. Explicitly implementing the interface anyway is
...
...
@@ -364,6 +376,96 @@ bool(true)
364
376
]]>
365
377
</screen>
366
378
</example>
379
+
<example>
380
+
<title>Using <link linkend="object.invoke">__invoke()</link></title>
381
+
<programlisting role="php">
382
+
<![CDATA[
383
+
<?php
384
+
class Sort
385
+
{
386
+
private $key;
387
+

388
+
public function __construct(string $key)
389
+
{
390
+
$this->key = $key;
391
+
}
392
+

393
+
public function __invoke(array $a, array $b): int
394
+
{
395
+
return $a[$this->key] <=> $b[$this->key];
396
+
}
397
+
}
398
+

399
+
$customers = [
400
+
['id' => 1, 'first_name' => 'John', 'last_name' => 'Do'],
401
+
['id' => 3, 'first_name' => 'Alice', 'last_name' => 'Gustav'],
402
+
['id' => 2, 'first_name' => 'Bob', 'last_name' => 'Filipe']
403
+
];
404
+

405
+
// sort customers by first name
406
+
usort($customers, new Sort('first_name'));
407
+
print_r($customers);
408
+

409
+
// sort customers by last name
410
+
usort($customers, new Sort('last_name'));
411
+
print_r($customers);
412
+
?>
413
+
]]>
414
+
</programlisting>
415
+
&example.outputs;
416
+
<screen>
417
+
<![CDATA[
418
+
Array
419
+
(
420
+
[0] => Array
421
+
(
422
+
[id] => 3
423
+
[first_name] => Alice
424
+
[last_name] => Gustav
425
+
)
426
+

427
+
[1] => Array
428
+
(
429
+
[id] => 2
430
+
[first_name] => Bob
431
+
[last_name] => Filipe
432
+
)
433
+

434
+
[2] => Array
435
+
(
436
+
[id] => 1
437
+
[first_name] => John
438
+
[last_name] => Do
439
+
)
440
+

441
+
)
442
+
Array
443
+
(
444
+
[0] => Array
445
+
(
446
+
[id] => 1
447
+
[first_name] => John
448
+
[last_name] => Do
449
+
)
450
+

451
+
[1] => Array
452
+
(
453
+
[id] => 2
454
+
[first_name] => Bob
455
+
[last_name] => Filipe
456
+
)
457
+

458
+
[2] => Array
459
+
(
460
+
[id] => 3
461
+
[first_name] => Alice
462
+
[last_name] => Gustav
463
+
)
464
+

465
+
)
466
+
]]>
467
+
</screen>
468
+
</example>
367
469
</sect2>
368
470

369
471
<sect2 xml:id="language.oop5.magic.set-state">
370
472