PHP 8.3.4 Released!

assert_options

(PHP 4, PHP 5, PHP 7, PHP 8)

assert_options设置/获取各种断言 flag

警告

此函数自 PHP 8.3.0 起弃用。强烈建议不要应用此函数。

说明

assert_options(int $option, mixed $value = ?): mixed

设置 assert() 的各种控制选项,或者是仅仅查询当前的设置。

注意: 不鼓励使用 assert_options(),而是分别使用 ini_set()ini_get() 设置和获取 php.ini 指令 zend.assertionsassert.exception

参数

option

断言选项
选项 INI 设置 默认值 描述
ASSERT_ACTIVE assert.active 1 启用 assert() 断言
ASSERT_EXCEPTION assert.exception 1 每个失败断言,抛出 AssertionError
ASSERT_WARNING assert.warning 1 为每个失败的断言产生一个 PHP 警告(warning)
ASSERT_BAIL assert.bail 0 在断言失败时中止执行
ASSERT_QUIET_EVAL assert.quiet_eval 0 在断言表达式求值时禁用 error_reporting。PHP 8.0.0 起移除。
ASSERT_CALLBACK assert.callback (null) 断言失败时调用回调函数

value

可选的新选项值。

通过 ASSERT_CALLBACKassert.callback 设置的回调函数应该有以下签名:

assert_callback(
    string $file,
    int $line,
    ?string $assertion,
    string $description = ?
): void
file
调用 assert() 的文件名。
line
调用 assert() 的行数。
assertion
在 PHP 8.0.0 之前,传递给 assert() 的断言,仅作为字符串给出。 (如果断言是 boolean 条件,则此参数将为空字符串。)从 PHP 8.0.0 开始,此参数始终为 null
description
传递给 assert() 的描述。

value 传递空字符串会重置断言回调。

返回值

返回任意选项的原始设置。

错误/异常

如果 option 是无效选项,抛出 ValueError

更新日志

版本 说明
8.3.0 assert_option() 现在已弃用。
8.0.0 如果 option 是无效选项,抛出 ValueError。之前返回 false

示例

示例 #1 assert_options() 示例

<?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';
?>

参见

add a note

User Contributed Notes 1 note

up
3
Fr?d?ric Bouchery
20 years ago
Here is an exemple how to use the assertion callback function :

<?php
assert_options
( ASSERT_CALLBACK, 'assert_callback');

function
assert_callback( $script, $line, $message ) {
echo
'You have a design error in your script <b>', $script,'</b> : line <b>', $line,'</b> :<br />';
echo
'<b>', ereg_replace( '^.*//\*', '', $message ), '</b><br /><br />';
echo
'Open the source file and check it, because it\'s not a normal behaviour !';
exit;
}

$x = 3;
assert('is_integer( $x ) && ($x >= 0) && ($x <= 10); //* $x must be an integer value from 0 to 10' );
echo
"0 <= $x <= 10";
?>

assertion is usefull for "design by contract" methodology ...
To Top