language/oop5/magic.xml
5e15a6c3e4d5819102361ae78e73c90a06238c8a
...
...
@@ -58,6 +58,9 @@
58
58
must be identical to the signature described in this document.
59
59
Otherwise, a fatal error is emitted.
60
60
Prior to PHP 8.0.0, no diagnostic was emitted.
61
+
However, <link linkend="object.construct">__construct()</link> and
62
+
<link linkend="object.destruct">__destruct()</link> must not declare a return type;
63
+
otherwise a fatal error is emitted.
61
64
</para>
62
65
</warning>
63
66
...
...
@@ -93,10 +96,15 @@
93
96
Use <link linkend="object.serialize">__serialize()</link> instead.
94
97
</para>
95
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>
96
104
<para>
97
105
The intended use of <link linkend="object.sleep">__sleep()</link> is to commit pending
98
106
data or perform similar cleanup tasks. Also, the function is
99
-
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.
100
108
</para>
101
109
<para>
102
110
Conversely, <function>unserialize</function> checks for the
...
...
@@ -268,6 +276,13 @@ class Connection
268
276
is disabled.
269
277
</para>
270
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>
271
286
As of PHP 8.0.0, any class that contains a <link linkend="object.tostring">__toString()</link>
272
287
method will also implicitly implement the <interfacename>Stringable</interfacename> interface, and will
273
288
thus pass type checks for that interface. Explicitly implementing the interface anyway is
...
...
@@ -361,6 +376,96 @@ bool(true)
361
376
]]>
362
377
</screen>
363
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>
364
469
</sect2>
365
470

366
471
<sect2 xml:id="language.oop5.magic.set-state">
...
...
@@ -428,7 +533,7 @@ object(A)#2 (2) {
428
533
<simpara>
429
534
When exporting an object, <function>var_export</function> does not check
430
535
whether <link linkend="object.set-state">__set_state()</link> is
431
-
implemented by the object's class, so re-importing such objects will fail,
536
+
implemented by the object's class, so re-importing objects will result in an <classname>Error</classname> exception,
432
537
if __set_state() is not implemented. Particularly, this affects some
433
538
internal classes.
434
539
</simpara>
435
540