-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathassert.xml
412 lines (402 loc) · 13.6 KB
/
assert.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 27dcb487a7e9c0f04559522a6d2aacf860d622cd Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.assert" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>assert</refname>
<refpurpose>断言检测</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>assert</methodname>
<methodparam><type>mixed</type><parameter>assertion</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>Throwable</type><type>string</type><type>null</type></type><parameter>description</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
<function>assert</function> 允许定义预期(expectations):在开发和测试环境中生效的断言,但在生产环境中会优化掉以达到零成本。
</para>
<para>
断言应该只用于调试功能。其中一个用例是检查健全性的前提条件,应该始终为
&true;,如果不满足这些条件,则表示存在某些编程错误。
另一个用例是确保某些功能的存在,例如扩展函数或某些系统限制和特性。
</para>
<para>
由于断言可以配置为已消除,因此<emphasis>不</emphasis>应该用于普通运行时操作,比如检查输入参数。一般来说,代码应该在禁用断言检查的情况下仍然按预期运行。
</para>
<para>
<function>assert</function> 将检查 <parameter>assertion</parameter> 中指定的预期(expectations)是否成立。如果不成立,也就是结果为
&false;,它将根据 <function>assert</function> 的配置采取适当的操作。
</para>
<para>
<function>assert</function> 的行为由以下 INI 设置决定:
<table>
<title>断言 &ConfigureOptions;</title>
<tgroup cols="4">
<thead>
<row>
<entry>&Name;</entry>
<entry>&Default;</entry>
<entry>&Description;</entry>
<entry>&Changelog;</entry>
</row>
</thead>
<tbody>
<row>
<entry><link linkend="ini.zend.assertions">zend.assertions</link></entry>
<entry><literal>1</literal></entry>
<entry>
<simplelist>
<member>
<literal>1</literal>:生成并执行代码(开发模式)
</member>
<member>
<!-- TODO: look up the RFC to figure out why you'd want this -->
<literal>0</literal>:生成代码但在运行时跳转
</member>
<member>
<literal>-1</literal>:生成代码但在运行时跳转(生产模式)
</member>
</simplelist>
</entry>
<entry/>
</row>
<row>
<entry><link linkend="ini.assert.active">assert.active</link></entry>
<entry>&true;</entry>
<entry>
为 &false; 时,<function>assert</function> 将不会检查预期(expectation)并且无条件返回 &true;。
</entry>
<entry>
自 PHP 8.3.0 起弃用。
</entry>
</row>
<row>
<entry><link linkend="ini.assert.callback">assert.callback</link></entry>
<entry>&null;</entry>
<entry>
<para>
当断言失败时,将调用用户定义的函数,其签名应该是:
<methodsynopsis role="procedural">
<type>void</type><methodname>assert_callback</methodname>
<methodparam><type>string</type><parameter>file</parameter></methodparam>
<methodparam><type>int</type><parameter>line</parameter></methodparam>
<methodparam><type>null</type><parameter>assertion</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>description</parameter></methodparam>
</methodsynopsis>
</para>
</entry>
<entry>
<para>
在 PHP 8.0.0 之前,回调的签名应该是:
<methodsynopsis role="procedural">
<type>void</type><methodname>assert_callback</methodname>
<methodparam><type>string</type><parameter>file</parameter></methodparam>
<methodparam><type>int</type><parameter>line</parameter></methodparam>
<methodparam><type>string</type><parameter>assertion</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>description</parameter></methodparam>
</methodsynopsis>
</para>
<simpara>
自 PHP 8.3.0 起弃用。
</simpara>
</entry>
</row>
<row>
<entry><link linkend="ini.assert.exception">assert.exception</link></entry>
<entry>&true;</entry>
<entry>
如果为 &true;,则如果不能满足预期(expectations),将抛出 <classname>AssertionError</classname> 异常。
</entry>
<entry>
自 PHP 8.3.0 起弃用
</entry>
</row>
<row>
<entry><link linkend="ini.assert.bail">assert.bail</link></entry>
<entry>&false;</entry>
<entry>
为 &true; 时,如果预期(expectation)不支持,将会中止 PHP 脚本的执行。
</entry>
<entry>
自 PHP 8.3.0 起弃用
</entry>
</row>
<row>
<entry><link linkend="ini.assert.warning">assert.warning</link></entry>
<entry>&true;</entry>
<entry>
为 &true; 时,则如果预期(expectation)不支持,将发出 <constant>E_WARNING</constant> 警告。如果启用了
<link linkend="ini.assert.exception">assert.exception</link>,此 INI 设置将无效。
</entry>
<entry>
自 PHP 8.3.0 起弃用
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>assertion</parameter></term>
<listitem>
<para>
可以是任何带返回值的表达式,运行后的结果用于表示断言成功还是失败。
</para>
<warning>
<para>
在 PHP 8.0.0 之前,如果 <parameter>assertion</parameter> 是 <type>string</type>,将解释为
PHP 代码,并通过 <function>eval</function> 执行。这个字符串将作为第三个参数传递给回调函数。这种行为在
PHP 7.2.0 中<emphasis>弃用</emphasis>,并在 PHP 8.0.0 中<emphasis>移除</emphasis>。
</para>
</warning>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>description</parameter></term>
<listitem>
<para>
如果 <parameter>description</parameter> 是 <classname>Throwable</classname>
的实例,只有在 <parameter>assertion</parameter> 执行失败时才会抛出。
<note>
<para>
自 PHP 8.0.0 开始,在调用可能定义的断言回调<emphasis>之前</emphasis>执行此操作。
</para>
</note>
<note>
<para>
自 PHP 8.0.0 开始,无论 <link linkend="ini.assert.exception">assert.exception</link> 的配置如何,都将会抛出该 &object;。
</para>
</note>
<note>
<para>
自 PHP 8.0.0 开始,在这种情况下,<link linkend="ini.assert.bail">assert.bail</link> 设置不起作用。
</para>
</note>
</para>
<para>
如果 <parameter>description</parameter> 是 &string;,当发出异常或警告时,将使用该消息。
如果 <parameter>assertion</parameter> 失败了,可选的 description 将会包括在失败信息里。
</para>
<para>
如果省略 <parameter>description</parameter>,
<!-- This does not happen if &null; is passed ... -->
将在编译时创建默认 description,该 description 等于对 <function>assert</function> 调用的源代码。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>assert</function> 总是返回 &true;,前提是以下条件中至少有一个为真:
</para>
<simplelist>
<member><literal>zend.assertions=0</literal></member>
<member><literal>zend.assertions=-1</literal></member>
<member><literal>assert.exception=1</literal></member>
<member><literal>assert.bail=1</literal></member>
<member>将自定义异常对象传递给 <parameter>description</parameter>。</member>
</simplelist>
<para>
如果所有条件都不满足, <function>assert</function> 将在 <parameter>assertion</parameter> 为真时返回 &true;,否则返回 &false;。urn &true; if
<parameter>assertion</parameter> is truthy and &false; otherwise.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.3.0</entry>
<entry>
弃用所有的 <literal>assert.</literal> INI 设置。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
<function>assert</function> 将不再对字符串参数求值,而是跟其他参数一样对待。应该使用 <code>assert($a == $b)</code>
替代 <code>assert('$a == $b')</code>。已移除 <literal>assert.quiet_eval</literal> &php.ini; 指令和
<constant>ASSERT_QUIET_EVAL</constant> 常量,因为它们不再有任何作用。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
如果 <parameter>description</parameter> 是 <classname>Throwable</classname> 的实例,无论
<link linkend="ini.assert.exception">assert.exception</link> 的值如何,如果断言失败,该对象都会被抛出。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
如果 <parameter>description</parameter> 是 <classname>Throwable</classname> 的实例,即使设置了用户回调,也不会调用该回调。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
不再允许在命名空间中声明叫做 <literal>assert()</literal> 的函数,并发出 <constant>E_COMPILE_ERROR</constant>。
</entry>
</row>
<row>
<entry>7.3.0</entry>
<entry>
弃用在命名空间中声明 <literal>assert()</literal> 函数。这样声明会发出 <constant>E_DEPRECATED</constant>。
</entry>
</row>
<row>
<entry>7.2.0</entry>
<entry>
弃用使用 <type>string</type> 作为 <parameter>assertion</parameter>。当 <link linkend="ini.assert.active">assert.active</link>
和 <link linkend="ini.zend.assertions">zend.assertions</link> 都设为 <literal>1</literal> 时,现在会发出
<constant>E_DEPRECATED</constant> 通知。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>assert</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
assert(1 > 2);
echo 'Hi!';
]]>
</programlisting>
<para>
如果启用断言(<link linkend="ini.zend.assertions"><literal>zend.assertions=1</literal></link>)
以上示例会输出:
</para>
<screen>
<![CDATA[
Fatal error: Uncaught AssertionError: assert(1 > 2) in example.php:2
Stack trace:
#0 example.php(2): assert(false, 'assert(1 > 2)')
#1 {main}
thrown in example.php on line 2
]]>
</screen>
<para>
如果禁用断言(<literal>zend.assertions=0</literal> 或 <literal>zend.assertions=-1</literal>)
以上示例会输出:
</para>
<screen>
<![CDATA[
Hi!
]]>
</screen>
</example>
<example>
<title>使用自定义信息</title>
<programlisting role="php">
<![CDATA[
<?php
assert(1 > 2, "Expected one to be greater than two");
echo 'Hi!';
?>
]]>
</programlisting>
<para>
如果启用断言,以上示例会输出:
</para>
<screen>
<![CDATA[
Fatal error: Uncaught AssertionError: Expected one to be greater than two in example.php:2
Stack trace:
#0 example.php(2): assert(false, 'Expected one to...')
#1 {main}
thrown in example.php on line 2
]]>
</screen>
<para>
如果禁用断言,以上示例会输出:
</para>
<screen>
<![CDATA[
Hi!
]]>
</screen>
</example>
<example>
<title>使用自定义异常类</title>
<programlisting role="php">
<![CDATA[
<?php
class ArithmeticAssertionError extends AssertionError {}
assert(1 > 2, new ArithmeticAssertionError("Expected one to be greater than two"));
echo 'Hi!';
]]>
</programlisting>
<para>
如果启用断言,以上示例会输出:
</para>
<screen>
<![CDATA[
Fatal error: Uncaught ArithmeticAssertionError: Expected one to be greater than two in example.php:4
Stack trace:
#0 {main}
thrown in example.php on line 4
]]>
</screen>
<para>
如果禁用断言,以上示例会输出:
</para>
<screen>
<![CDATA[
Hi!
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>assert_options</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->