assert_options
(PHP 4, PHP 5, PHP 7, PHP 8)
assert_options — 设置/获取各种断言 flag
警告
此函数自 PHP 8.3.0 起弃用。强烈建议不要应用此函数。
说明
设置 assert() 的各种控制选项,或者是仅仅查询当前的设置。
注意: 不鼓励使用 assert_options(),而是分别使用 ini_set() 和 ini_get() 设置和获取 php.ini 指令 zend.assertions 和 assert.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_CALLBACK
或 assert.callback 设置的回调函数应该有以下签名:
value
传递空字符串会重置断言回调。
返回值
返回任意选项的原始设置。
错误/异常
如果 option
是无效选项,抛出 ValueError。
示例
示例 #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';
?>
+添加备注
用户贡献的备注 1 note
Fr?d?ric Bouchery ¶
21 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 ...
备份地址:http://www.lvesu.com/blog/php/function.assert-options.php