Skip to content

Commit 25c0026

Browse files
committed
Update language
1 parent f89c71b commit 25c0026

13 files changed

+454
-74
lines changed

language-snippets.ent

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: 9a5b92a30888d6423db112f07a9b344cf6fc4891 Maintainer: Gregory Status: ready -->
3+
<!-- EN-Revision: c75f19c74fa3b64abfafd7a35aaa652b07834a5a Maintainer: Gregory Status: ready -->
44
<!-- CREDITS: dallas, mowangjuanzi, Luffy -->
55
<!-- 请保持此文件与英文文件中相应的每个 ENTITY 行号一一对应以便于对照,修改与更新! -->
66

@@ -2182,7 +2182,12 @@ while <constant>PGSQL_BOTH</constant> will return both numerical and associative
21822182

21832183
<!-- GMP Notes -->
21842184
<!ENTITY gmp.return '<classname xmlns="http://docbook.org/ns/docbook">GMP</classname> 对象。'>
2185-
<!ENTITY gmp.parameter '<para xmlns="http://docbook.org/ns/docbook"><classname>GMP</classname> 对象或 &integer; ,或数字&string;。</para>'>
2185+
<!ENTITY gmp.parameter '<para xmlns="http://docbook.org/ns/docbook">
2186+
<classname>GMP</classname> 对象、&integer;&string;,可以按照跟在 <function>gmp_init</function>
2187+
中使用字符串并自动检测 base(即当 base 等于 0 时)相同的逻辑将其解释为数字。
2188+
</para>'>
2189+
2190+
21862191

21872192
<!-- MySQLi Notes -->
21882193
<!ENTITY mysqli.result.description '<varlistentry xmlns="http://docbook.org/ns/docbook"><term>

language/oop5/abstract.xml

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- $Author$ -->
4-
<!-- EN-Revision: 9ee9eccf455188ab6eb352194eb6f9eb99e15606 Maintainer: Haohappy Status: ready -->
3+
<!-- EN-Revision: 984188eb6941dc419f49eed6c1a55a6b749a823d Maintainer: Haohappy Status: ready -->
4+
<!-- CREDITS: mowangjuanzi -->
55
<sect1 xml:id="language.oop5.abstract" xmlns="http://docbook.org/ns/docbook">
66
<title>抽象类</title>
77

88
<para>
9-
PHP 有抽象类和抽象方法。定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。
9+
PHP 有抽象类、抽象方法和抽象属性。定义为抽象的类无法实例化。任何一个类,如果它里面有一个方法或者属性是声明为抽象,那么这个类就必须被声明为抽象。定义为抽象的方法仅声明方法的签名以及它是
10+
public 还是 protected;但无法定义实现。定义为抽象的属性可以声明 <literal>get</literal> 或
11+
<literal>set</literal> 行为的要求,并且可以为一个(但不是全部)操作提供实现。
1012
</para>
1113

1214
<para>
@@ -15,52 +17,67 @@
1517
<link linkend="language.oop.lsp">签名兼容性</link> 规则。
1618
</para>
1719

20+
<simpara>
21+
自 PHP 8.4 起,抽象类可以声明抽象属性,可以是 public,也可以是 protected。protected
22+
抽象属性可以从 protected 或 public 作用域读取/写入的属性满足。
23+
</simpara>
24+
<simpara>
25+
抽象属性可以由标准属性或<link linkend="language.oop5.property-hooks">挂钩</link>属性来满足,与所需的操作相对应。
26+
</simpara>
27+
1828
<example>
19-
<title>抽象类示例</title>
20-
<programlisting role="php">
29+
<title>抽象方法示例</title>
30+
<programlisting role="php">
2131
<![CDATA[
2232
<?php
33+
2334
abstract class AbstractClass
2435
{
25-
// 强制要求子类定义这些方法
36+
// 强制要求子类定义这些方法
2637
abstract protected function getValue();
2738
abstract protected function prefixValue($prefix);
2839
2940
// 普通方法(非抽象方法)
30-
public function printOut() {
41+
public function printOut()
42+
{
3143
print $this->getValue() . "\n";
3244
}
3345
}
3446
3547
class ConcreteClass1 extends AbstractClass
3648
{
37-
protected function getValue() {
49+
protected function getValue()
50+
{
3851
return "ConcreteClass1";
3952
}
4053
41-
public function prefixValue($prefix) {
54+
public function prefixValue($prefix)
55+
{
4256
return "{$prefix}ConcreteClass1";
4357
}
4458
}
4559
4660
class ConcreteClass2 extends AbstractClass
4761
{
48-
public function getValue() {
62+
public function getValue()
63+
{
4964
return "ConcreteClass2";
5065
}
5166
52-
public function prefixValue($prefix) {
67+
public function prefixValue($prefix)
68+
{
5369
return "{$prefix}ConcreteClass2";
5470
}
5571
}
5672
57-
$class1 = new ConcreteClass1;
73+
$class1 = new ConcreteClass1();
5874
$class1->printOut();
59-
echo $class1->prefixValue('FOO_') ."\n";
75+
echo $class1->prefixValue('FOO_'), "\n";
6076
61-
$class2 = new ConcreteClass2;
77+
$class2 = new ConcreteClass2();
6278
$class2->printOut();
63-
echo $class2->prefixValue('FOO_') ."\n";
79+
echo $class2->prefixValue('FOO_'), "\n";
80+
6481
?>
6582
]]>
6683
</programlisting>
@@ -76,36 +93,38 @@ FOO_ConcreteClass2
7693
</example>
7794

7895
<example>
79-
<title>抽象类示例</title>
96+
<title>抽象方法示例</title>
8097
<programlisting role="php">
8198
<![CDATA[
8299
<?php
100+
83101
abstract class AbstractClass
84102
{
85-
// 我们的抽象方法仅需要定义需要的参数
103+
// 抽象方法仅需要定义需要的参数
86104
abstract protected function prefixName($name);
87-
88105
}
89106
90107
class ConcreteClass extends AbstractClass
91108
{
92-
93-
// 我们的子类可以定义父类签名中不存在的可选参数
94-
public function prefixName($name, $separator = ".") {
109+
// 子类可以定义父类签名中不存在的可选参数
110+
public function prefixName($name, $separator = ".")
111+
{
95112
if ($name == "Pacman") {
96113
$prefix = "Mr";
97114
} elseif ($name == "Pacwoman") {
98115
$prefix = "Mrs";
99116
} else {
100117
$prefix = "";
101118
}
119+
102120
return "{$prefix}{$separator} {$name}";
103121
}
104122
}
105123
106-
$class = new ConcreteClass;
124+
$class = new ConcreteClass();
107125
echo $class->prefixName("Pacman"), "\n";
108126
echo $class->prefixName("Pacwoman"), "\n";
127+
109128
?>
110129
]]>
111130
</programlisting>
@@ -117,8 +136,80 @@ Mrs. Pacwoman
117136
]]>
118137
</screen>
119138
</example>
120-
</sect1>
139+
<example>
140+
<title>抽象属性示例</title>
141+
<programlisting role="php">
142+
<![CDATA[
143+
<?php
144+
145+
abstract class A
146+
{
147+
// 继承类必须具有可 public get 的属性
148+
abstract public string $readable {
149+
get;
150+
}
151+
152+
// 继承类必须具有 protected 或 public set 的属性
153+
abstract protected string $writeable {
154+
set;
155+
}
156+
157+
// 继承类必须具有 protected 或 public 的对称属性。
158+
abstract protected string $both {
159+
get;
160+
set;
161+
}
162+
}
163+
164+
class C extends A
165+
{
166+
// 这满足了要求,也使其可 set,这是有效的
167+
public string $readable;
121168
169+
// 这不能满足要求,因为它不能 public 可读
170+
protected string $readable;
171+
172+
// 这正好满足要求,所以足够了。
173+
// 它只能被写入,并且只能从 protected 作用域进行写入。
174+
protected string $writeable {
175+
set => $value;
176+
}
177+
178+
// 这将访问控制从 protected 继承为 public,这很好
179+
public string $both;
180+
}
181+
182+
?>
183+
]]>
184+
</programlisting>
185+
</example>
186+
<simpara>
187+
抽象类上的抽象属性可以为任何挂钩提供实现,必须声明 <literal>get</literal> 或 <literal>set</literal> 但是没有定义(如上例所示)。
188+
</simpara>
189+
<example>
190+
<title>抽象属性示例</title>
191+
<programlisting role="php">
192+
<![CDATA[
193+
<?php
194+
195+
abstract class A
196+
{
197+
// 这提供了默认(但可覆盖)set 实现,
198+
// 并要求子类提供 get 实现
199+
abstract public string $foo {
200+
get;
201+
202+
set {
203+
$this->foo = $value;
204+
}
205+
}
206+
}
207+
208+
?>
209+
]]>
210+
</programlisting>
211+
</example>
212+
</sect1>
122213
<!-- Keep this comment at the end of the file
123214
Local variables:
124215
mode: sgml

language/oop5/anonymous.xml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: a9edd62d087ab1eb6292c795b7256e14ff9f1234 Maintainer: daijie Status: ready -->
4-
<!-- Reviewed: no -->
3+
<!-- EN-Revision: a058684525563674d49fcfce7ce22d1183ee0a31 Maintainer: daijie Status: ready -->
4+
<!-- CREDITS: mowangjuanzi -->
55
<sect1 xml:id="language.oop5.anonymous" xmlns="http://docbook.org/ns/docbook">
66
<title>匿名类</title>
77

@@ -37,7 +37,7 @@ $util->setLogger(new class {
3737
</informalexample>
3838

3939
<para>
40-
可以传递参数到匿名类的构造器,也可以扩展(extend)其他类、实现接口(implement interface),以及像其他普通的类一样使用 trait:
40+
可以传递参数到匿名类的构造器,也可以继承其他类、实现接口(implement interface),以及像其他普通的类一样使用 trait:
4141
</para>
4242

4343
<informalexample>
@@ -73,7 +73,7 @@ object(class@anonymous)#1 (1) {
7373
</informalexample>
7474

7575
<para>
76-
匿名类被嵌套进普通 Class 后,不能访问这个外部类(Outer class)的 private(私有)、protected(受保护)方法或者属性。
76+
匿名类被嵌套进普通 Class 后,不能访问这个外部类(Outer class)的 private(私有)、protected(受保护)方法或者属性。
7777
为了访问外部类(Outer class)protected 属性或方法,匿名类可以 extend(扩展)此外部类。
7878
为了使用外部类(Outer class)的 private 属性,必须通过构造器传进来:
7979
</para>
@@ -121,6 +121,7 @@ echo (new Outer)->func2()->func3();
121121
]]>
122122
</screen>
123123
</informalexample>
124+
124125
<para>
125126
声明的同一个匿名类,所创建的对象都是这个类的实例。
126127
</para>
@@ -168,8 +169,34 @@ class@anonymous/in/oNi1A0x7f8636ad2021
168169
</screen>
169170
</informalexample>
170171
</note>
172+
173+
<sect2 xml:id="language.oop5.anonymous.readonly">
174+
<title>只读匿名类</title>
175+
<simpara>
176+
自 PHP 8.3.0 起,<literal>readonly</literal> 修饰符可应用于匿名类。
177+
</simpara>
178+
<example xml:id="language.oop5.anonymous.readonly.example">
179+
<title>定义 readonly 匿名类</title>
180+
<programlisting role="php">
181+
<![CDATA[
182+
<?php
183+
184+
// 使用匿名类
185+
$util->setLogger(new readonly class('[DEBUG]') {
186+
public function __construct(private string $prefix)
187+
{
188+
}
189+
190+
public function log($msg)
191+
{
192+
echo $this->prefix . ' ' . $msg;
193+
}
194+
});
195+
]]>
196+
</programlisting>
197+
</example>
198+
</sect2>
171199
</sect1>
172-
173200
<!-- Keep this comment at the end of the file
174201
Local variables:
175202
mode: sgml

language/oop5/autoload.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: ce3a2d381693ccbc10cc4a808c3eb853f3c85c9e Maintainer: dallas Status: ready -->
3+
<!-- EN-Revision: 7befd9af043386158ef9080723eb18e03f0625c7 Maintainer: dallas Status: ready -->
44
<!-- CREDITS: mowangjuanzi -->
55
<sect1 xml:id="language.oop5.autoload" xmlns="http://docbook.org/ns/docbook">
66
<title>类的自动加载</title>
@@ -72,6 +72,24 @@ string(5) "ITest"
7272
Fatal error: Interface 'ITest' not found in ...
7373
*/
7474
?>
75+
]]>
76+
</programlisting>
77+
</example>
78+
<example>
79+
<title>使用 Composer 的自动加载器</title>
80+
<simpara>
81+
Composer 会生成 <literal>vendor/autoload.php</literal> 文件,用于自动加载
82+
Composer 管理的软件包。通过 include 此文件,无需任何额外工作即可使用这些软件包。
83+
</simpara>
84+
<programlisting role="php">
85+
<![CDATA[
86+
<?php
87+
require __DIR__ . '/vendor/autoload.php';
88+
89+
$uuid = new Ramsey\Uuid\Uuid::uuid7();
90+
91+
echo "Generated new UUID -> ", $uuid->toString(), "\n";
92+
?>
7593
]]>
7694
</programlisting>
7795
</example>

language/oop5/basic.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- $Revision$ -->
3-
<!-- EN-Revision: 1d92bcca7524c50fc41056ea4c04e1032eb9e055 Maintainer: avenger Status: ready -->
3+
<!-- EN-Revision: 2f85d57a0b5a01b875cbca2b291f04389cf61ac2 Maintainer: avenger Status: ready -->
44
<!-- CREDITS: mowangjuanzi, Luffy -->
55
<sect1 xml:id="language.oop5.basic" xmlns="http://docbook.org/ns/docbook">
66
<title>基本概念</title>
@@ -12,10 +12,10 @@
1212
开头,后面跟着类名,后面跟着一对花括号,里面包含有类的属性与方法的定义。
1313
</para>
1414
<para>
15-
类名可以是任何非 PHP
16-
<link linkend="reserved">保留字</link>
17-
的合法标签。一个合法类名以字母或下划线开头,后面跟着若干字母数字或下划线。以正则表达式表示为
18-
<code>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>。
15+
类名可以是任何不是 PHP <link linkend="reserved">保留字</link>
16+
的有效标签。自 PHP 8.4.0 起,弃用使用单个下划线 <literal>_</literal>
17+
作为类名。有效类名以字母或下划线开头,后面跟着若干字母数字或下划线。以正则表达式表示为
18+
<code>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>。
1919
</para>
2020
<para>
2121
一个类可以包含有属于自己的 <link

0 commit comments

Comments
 (0)