(PHP 4, PHP 5, PHP 7, PHP 8)
assert — 检查断言是否为 false
PHP 5 和 7
PHP 7
assert() 会检查指定的 assertion
并在结果为 false
时采取适当的行动。
如果 assertion
是字符串,它将会被 assert()
当做 PHP 代码来执行。如果传入了 boolean 的条件作为
assertion
,这个条件将不会显示为断言函数的参数;在调用你定义的
assert_options() 处理函数时,条件会转换为字符串,而布尔值 false
会被转换成空字符串。
断言这个功能应该只被用来调试。应该用于完整性检查时测试条件是否始终应该为
true
,来指示某些程序错误,或者检查具体功能的存在(类似扩展函数或特定的系统限制和功能)。
断言不应该用于普通运行时操作,类似输入参数的检查。作为一个经验法则,在断言禁用时代码也应该能够正确地运行。
assert() 的行为可以通过 assert_options() 来配置,或者手册页面上描述的 .ini 设置。
assert_options() 和/或 ASSERT_CALLBACK
配置指令允许设置回调函数来处理失败的断言。
assert() 回调函数在构建自动测试套件的时候尤其有用,因为它们允许你简易地捕获传入断言的代码,并包含断言的位置信息。 当信息能够被其他方法捕获,使用断言可以让它更快更方便!
回调函数应该接受三个参数。第一个参数包括了断言失败所在的文件。第二个参数包含了断言失败所在的行号,第三个参数包含了失败的表达式(如有任意
— 字面值例如 1 或者 "two" 将不会传递到这个参数)。PHP 5.4.8 及更高版本的用户也可以提供第四个可选参数,如果设置了,用于将
description
指定到 assert()。
assert() 是 PHP 7 中的语言结构,允许定义 expectation:断言在开发和测试环境中生效,经过优化,在生产环境中零成本。
出于向后兼容的原因,assert_options() 仍然可以控制如上所述的行为。但仅限 PHP 7 的代码应使用两个新的配置指令来控制 assert() 的行为,而不是调用 assert_options()。
指令 | 默认值 | 可能值 |
---|---|---|
zend.assertions | 1 |
|
assert.exception | 0 |
|
assertion
断言。In PHP 5, this must be either a string to be evaluated or a bool to be tested. In PHP 7, this may also be any expression that returns a value, which will be executed and the result used to indicate whether the assertion succeeded or failed.
自 PHP 7.2.0 起弃用 string 作为
assertion
,自 PHP 8.0.0 起删除。
description
如果 assertion
失败了,选项 description 将会包括在失败信息里。
From PHP 7, if no
description is provided, a default description equal to the source code
for the invocation of assert() is provided.
exception
In PHP 7, the second parameter can be a Throwable object instead of a descriptive string, in which case this is the object that will be thrown if the assertion fails and the assert.exception configuration directive is enabled.
assertion 是 false 则返回 false
,否则是 true
。
版本 | 说明 |
---|---|
8.0.0 |
assert() will no longer evaluate string arguments, instead they will be
treated like any other argument. assert($a == $b) should be used instead of
assert('$a == $b') . The assert.quiet_eval php.ini directive and
the ASSERT_QUIET_EVAL constant have also been removed, as they would no longer
have any effect.
|
8.0.0 |
Declaring a function called assert() inside a namespace is
no longer allowed, and issues E_COMPILE_ERROR .
|
7.3.0 |
Declaring a function called assert() inside a namespace
became deprecated. Such declaration now emits an E_DEPRECATED .
|
7.2.0 |
Usage of a string as the assertion
became deprecated. It now emits an E_DEPRECATED
notice when both assert.active
and zend.assertions are set
to 1 .
|
7.0.0 |
assert() is now a language construct and not a
function. assertion can now be an expression.
The second parameter is now interpreted either as an
exception (if a
Throwable object is given), or as the
description supported from PHP 5.4.8 onwards.
|
示例 #1 使用自定义处理程序处理失败的断言
<?php
// 激活断言,并设置它为 quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
//创建处理函数
function my_assert_handler($file, $line, $code)
{
echo "<hr>Assertion Failed:
File '$file'<br />
Line '$line'<br />
Code '$code'<br /><hr />";
}
// 设置回调函数
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// 让一则断言失败
assert('mysql_query("")');
?>
示例 #2 使用自定义处理器打印描述信息
<?php
// 激活断言,并设置它为 quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// 创建处理函数
function my_assert_handler($file, $line, $code, $desc = null)
{
echo "Assertion failed at $file:$line: $code";
if ($desc) {
echo ": $desc";
}
echo "\n";
}
// 设置回调
assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// Make an assertion that should fail
assert('2 < 1');
assert('2 < 1', 'Two is less than one');
?>
以上例程会输出:
Assertion failed at test.php:21: 2 < 1 Assertion failed at test.php:22: 2 < 1: Two is less than one
示例 #3 Expectations without a custom exception
<?php
assert(true == false);
echo 'Hi!';
?>
With zend.assertions set to 0, the above example will output:
Hi!
With zend.assertions set to 1 and assert.exception set to 0, the above example will output:
Warning: assert(): assert(true == false) failed in - on line 2 Hi!
With zend.assertions set to 1 and assert.exception set to 1, the above example will output:
Fatal error: Uncaught AssertionError: assert(true == false) in -:2 Stack trace: #0 -(2): assert(false, 'assert(true == ...') #1 {main} thrown in - on line 2
示例 #4 Expectations with a custom exception
<?php
class CustomError extends AssertionError {}
assert(true == false, new CustomError('True is not false!'));
echo 'Hi!';
?>
With zend.assertions set to 0, the above example will output:
Hi!
With zend.assertions set to 1 and assert.exception set to 0, the above example will output:
Warning: assert(): CustomError: True is not false! in -:4 Stack trace: #0 {main} failed in - on line 4 Hi!
With zend.assertions set to 1 and assert.exception set to 1, the above example will output:
Fatal error: Uncaught CustomError: True is not false! in -:4 Stack trace: #0 {main} thrown in - on line 4