-
Notifications
You must be signed in to change notification settings - Fork 104
/
assert-options.xml
270 lines (256 loc) · 7.71 KB
/
assert-options.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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: ada1d79de35239334b68d0120b011530e31244ff Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xml:id="function.assert-options" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>assert_options</refname>
<refpurpose>设置/获取各种断言 flag</refpurpose>
</refnamediv>
<refsynopsisdiv>
&warn.deprecated.function-8-3-0;
</refsynopsisdiv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>assert_options</methodname>
<methodparam><type>int</type><parameter>option</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>value</parameter></methodparam>
</methodsynopsis>
<para>
设置 <function>assert</function> 的各种控制选项,或者是仅仅查询当前的设置。
</para>
<note>
<simpara>
不鼓励使用 <function>assert_options</function>,而是分别使用
<function>ini_set</function> 和 <function>ini_get</function> 设置和获取
&php.ini; 指令 <link linkend="ini.zend.assertions">zend.assertions</link> 和
<link linkend="ini.assert.exception">assert.exception</link>。
</simpara>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>option</parameter></term>
<listitem>
<para>
<table>
<title>断言选项</title>
<tgroup cols="4">
<thead>
<row>
<entry>选项</entry>
<entry>INI 设置</entry>
<entry>默认值</entry>
<entry>描述</entry>
</row>
</thead>
<tbody>
<row>
<entry>ASSERT_ACTIVE</entry>
<entry>assert.active</entry>
<entry>1</entry>
<entry>启用 <function>assert</function> 断言</entry>
</row>
<row>
<entry>ASSERT_EXCEPTION</entry>
<entry>assert.exception</entry>
<entry>1</entry>
<entry>每个失败断言,抛出 <classname>AssertionError</classname></entry>
</row>
<row>
<entry>ASSERT_WARNING</entry>
<entry>assert.warning</entry>
<entry>1</entry>
<entry>为每个失败的断言产生一个 PHP 警告(warning)</entry>
</row>
<row>
<entry>ASSERT_BAIL</entry>
<entry>assert.bail</entry>
<entry>0</entry>
<entry>在断言失败时中止执行</entry>
</row>
<row>
<entry>ASSERT_QUIET_EVAL</entry>
<entry>assert.quiet_eval</entry>
<entry>0</entry>
<entry>
在断言表达式求值时禁用 error_reporting。PHP 8.0.0 起移除。
</entry>
</row>
<row>
<entry>ASSERT_CALLBACK</entry>
<entry>assert.callback</entry>
<entry>(&null;)</entry>
<entry>断言失败时调用回调函数</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
可选的新选项值。
</para>
<para>
通过 <constant>ASSERT_CALLBACK</constant> 或 <link linkend="ini.assert.callback">assert.callback</link> 设置的回调函数应该有以下签名:
<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 class="union"><type>string</type><type>null</type></type><parameter>assertion</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>description</parameter></methodparam>
</methodsynopsis>
<variablelist>
<varlistentry>
<term><parameter>file</parameter></term>
<listitem>
<simpara>
调用 <function>assert</function> 的文件名。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>line</parameter></term>
<listitem>
<simpara>
调用 <function>assert</function> 的行数。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>assertion</parameter></term>
<listitem>
<simpara>
在 PHP 8.0.0 之前,传递给 <function>assert</function> 的断言,仅作为字符串给出。
(如果断言是 boolean 条件,则此参数将为空字符串。)从 PHP 8.0.0 开始,此参数始终为 &null;。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>description</parameter></term>
<listitem>
<simpara>
传递给 <function>assert</function> 的描述。
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
向 <parameter>value</parameter> 传递空字符串会重置断言回调。
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回任意选项的原始设置。
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
如果 <parameter>option</parameter> 是无效选项,抛出 <classname>ValueError</classname>。
</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>
<function>assert_option</function> 现在已弃用。
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
如果 <parameter>option</parameter> 是无效选项,抛出
<classname>ValueError</classname>。之前返回 &false;。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>assert_options</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
// 处理断言失败时的函数
function assert_failure($file, $line, $assertion, $message)
{
echo "The assertion $assertion in $file on line $line has failed: $message";
}
// 我们的测试函数
function test_assert($parameter)
{
assert(is_bool($parameter));
}
// 设置断言选项
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_BAIL, true);
assert_options(ASSERT_WARNING, false);
assert_options(ASSERT_CALLBACK, 'assert_failure');
// 让一个断言会失败
test_assert(1);
// 由于 ASSERT_BAIL 是 true,这里永远也到不了
echo 'Never reached';
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>assert</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
-->