1
1
<?xml version =" 1.0" encoding =" utf-8" ?>
2
- <!-- EN-Revision: 5fbf0f0cbce2dda6a379d8bda33d5b1869702e47 Maintainer: mowangjuanzi Status: partial -->
2
+ <!-- EN-Revision: 8a9f4bd9d54a809d66f32d35ec60eb2b7353f76e Maintainer: mowangjuanzi Status: partial -->
3
3
<sect1 xml : id =" migration84.new-features" >
4
4
<title >新功能</title >
5
5
9
9
10
10
<!-- RFC: https://wiki.php.net/rfc/property-hooks -->
11
11
<sect3 xml : id =" migration84.new-features.core.property-hooks" >
12
- <title >属性钩子 </title >
12
+ <title >属性挂钩 </title >
13
13
14
14
<simpara >
15
- TODO
15
+ 对象属性现在可以其 <literal >get</literal > 和 <literal >set</literal >
16
+ 操作中关联相关的附加逻辑。根据用法,这可能会也可能不会使属性变为虚拟属性,即该属性根本没有实际的存储值。
16
17
</simpara >
17
18
18
19
<informalexample >
19
20
<programlisting role =" php" >
20
21
<![CDATA[
21
22
<?php
22
- /*
23
- examples
24
- */
23
+ class Person
24
+ {
25
+ // “虚拟”属性,可能无法明确设置。
26
+ public string $fullName {
27
+ get => $this->firstName . ' ' . $this->lastName;
28
+ }
29
+
30
+ // 所有的写入操作都会经过这个挂钩,结果就是写入的内容。
31
+ // 读取访问正常。
32
+ public string $firstName {
33
+ set => ucfirst(strtolower($value));
34
+ }
35
+
36
+ // 所有的写入操作都会经过这个挂钩,它必须写入支持值本身。
37
+ // 读取访问正常。
38
+ public string $lastName {
39
+ set {
40
+ if (strlen($value) < 2) {
41
+ throw new \InvalidArgumentException('Too short');
42
+ }
43
+ $this->lastName = $value;
44
+ }
45
+ }
46
+ }
47
+
48
+ $p = new Person();
25
49
50
+ $p->firstName = 'peter';
51
+ print $p->firstName; // 打印“Peter”
52
+ $p->lastName = 'Peterson';
53
+ print $p->fullName; // 打印“Peter Peterson”
26
54
]]>
27
55
</programlisting >
28
56
</informalexample >
@@ -42,6 +70,8 @@ examples
42
70
<?php
43
71
class Example
44
72
{
73
+ // 第一个可见性修饰符控制 get 可见性,第二个修饰符控制 set 可见性。
74
+ // The get-visibility must not be narrower than set-visibility.
45
75
public protected(set) string $name;
46
76
47
77
public function __construct(string $name)
@@ -56,19 +86,49 @@ class Example
56
86
57
87
<!-- RFC: https://wiki.php.net/rfc/lazy-objects -->
58
88
<sect3 xml : id =" migration84.new-features.core.lazy-objects" >
59
- <title >Lazy Objects</title >
60
-
89
+ <title >惰性对象</title >
61
90
<simpara >
62
- TODO
91
+ 现在可以创建对象,将初始化延迟到访问时。库和框架可以利用这些惰性对象来延迟获取初始化所需的数据或依赖项。
63
92
</simpara >
93
+ <informalexample >
94
+ <programlisting role =" php" >
95
+ <![CDATA[
96
+ <?php
97
+ class Example
98
+ {
99
+ public function __construct(private int $data)
100
+ {
101
+ }
102
+
103
+ // ...
104
+ }
105
+
106
+ $initializer = static function (Example $ghost): void {
107
+ // 获取数据或者依赖项
108
+ $data = ...;
109
+ // 初始化
110
+ $ghost->__construct($data);
111
+ };
112
+
113
+ $reflector = new ReflectionClass(Example::class);
114
+ $object = $reflector->newLazyGhost($initializer);
115
+ ]]>
116
+ </programlisting >
117
+ </informalexample >
64
118
</sect3 >
65
119
66
120
<!-- RFC: https://wiki.php.net/rfc/deprecated_attribute -->
67
121
<sect3 xml : id =" migration84.new-features.core.deprecated-attribute" >
68
122
<title ><code >#[\Deprecated]</code > 注解</title >
69
123
70
124
<simpara >
71
- TODO
125
+ 新的 <classname >Deprecated</classname > 属性可用于将函数、方法和类常量标记为已弃用。此弃用属性的行为与 PHP
126
+ 本身提供的现有弃用机制的行为一致。唯一的例外是发出的错误代码是 <constant >E_USER_DEPRECATED</constant >,而不是
127
+ <constant >E_DEPRECATED</constant >。
128
+ </simpara >
129
+
130
+ <simpara >
131
+ PHP 本身提供的现有弃用已更新为使用该属性,通过包含简短的解释来改进发出的错误消息。
72
132
</simpara >
73
133
</sect3 >
74
134
@@ -190,9 +250,16 @@ class Example
190
250
<sect2 xml : id =" migration84.new-features.dom" >
191
251
<title >DOM</title >
192
252
253
+ <!-- RFC: https://wiki.php.net/rfc/domdocument_html5_parser -->
254
+ <!-- RFC: https://wiki.php.net/rfc/opt_in_dom_spec_compliance -->
255
+ <simpara >
256
+ 添加了 <package >Dom</package > 命名空间,其中包含与现有 DOM 类对应的新类(例如,<classname >Dom\Node</classname > 是新的
257
+ <classname >DOMNode</classname >)。这些类与 HTML 5 兼容,并且符合 WHATWG 规范;解决了 DOM 扩展中长期存在的错误。旧的 DOM
258
+ 类仍然可用,以实现向后兼容。
259
+ </simpara >
260
+
193
261
<para >
194
- Added the <methodname >DOMNode::compareDocumentPosition</methodname >
195
- with its associated constants:
262
+ 新增 <methodname >DOMNode::compareDocumentPosition</methodname > 及其相关常量:
196
263
<simplelist >
197
264
<member ><constant >DOMNode::DOCUMENT_POSITION_DISCONNECTED</constant ></member >
198
265
<member ><constant >DOMNode::DOCUMENT_POSITION_PRECEDING</constant ></member >
@@ -205,23 +272,11 @@ class Example
205
272
206
273
<!-- RFC: https://wiki.php.net/rfc/improve_callbacks_dom_and_xsl -->
207
274
<simpara >
208
- It is now possible to pass any callable to
209
- <methodname >DOMXPath::registerPhpFunctions</methodname >.
210
- <!-- TODO Mention DOMXPath::registerPHPFunctionNS ? -->
211
- </simpara >
212
- </sect2 >
275
+ 现在可以将任何 callable 传递给
276
+ <methodname >DOMXPath::registerPhpFunctions</methodname >。
213
277
214
- <!-- TODO: Should this be moved to "other changes" in the SAPI section? - Girgias -->
215
- <sect2 xml : id =" migration84.new-features.fpm" >
216
- <title >FPM</title >
217
-
218
- <simpara >
219
- Flushing headers without a body will now succeed.
220
- <!-- See GH-12785. -->
221
- </simpara >
222
-
223
- <simpara >
224
- 状态页面新增字段用于展示内存峰值。
278
+ 此外,现在使用 <methodname >DOMXPath::registerPhpFunctionNs</methodname > 可以注册使用原生函数调用语法,而不是使用
279
+ <code >php:function('name')</code >。
225
280
</simpara >
226
281
</sect2 >
227
282
@@ -391,8 +446,8 @@ class Example
391
446
</simpara >
392
447
</sect2 >
393
448
394
- <sect2 xml : id =" migration84.new-features.reflection " >
395
- <title >Reflection </title >
449
+ <sect2 xml : id =" migration84.new-features.readline " >
450
+ <title >Readline </title >
396
451
397
452
<simpara >
398
453
<classname >ReflectionAttribute</classname > now contains a
@@ -407,9 +462,7 @@ class Example
407
462
408
463
<!-- RFC: https://wiki.php.net/rfc/lazy-objects -->
409
464
<para >
410
- Multiple new methods and constants which are related to the lazy objects
411
- feature have been added:
412
-
465
+ 添加了与惰性对象功能相关的多个新方法和常量:
413
466
<simplelist >
414
467
<member >
415
468
<methodname >ReflectionClass::newLazyGhost</methodname >
@@ -436,10 +489,10 @@ class Example
436
489
<methodname >ReflectionClass::getLazyInitializer</methodname >
437
490
</member >
438
491
<member >
439
- <methodname >ReflectionClass ::skipLazyInitialization</methodname >
492
+ <methodname >ReflectionProperty ::skipLazyInitialization</methodname >
440
493
</member >
441
494
<member >
442
- <methodname >ReflectionClass ::setRawValueWithoutLazyInitialization</methodname >
495
+ <methodname >ReflectionProperty ::setRawValueWithoutLazyInitialization</methodname >
443
496
</member >
444
497
<member >
445
498
<constant >ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE</constant >
0 commit comments