百科狗-知识改变命运!
--

assert() - php 选项信息函数

梵高1年前 (2023-11-21)阅读数 22#技术干货
文章标签断言

assert()

assert() - php 选项信息函数

(PHP 4, PHP 5, PHP 7)

检查一个断言是否为FALSE

说明

PHP 5

assert(mixed $assertion[,string $description]): bool

PHP 7

assert(mixed $assertion[,Throwable$exception]): bool

assert()会检查指定的$assertion并在结果为FALSE时采取适当的行动。

Traditional assertions (PHP 5 and 7)

如果$assertion是字符串,它将会被assert()当做 PHP 代码来执行。$assertion是字符串的优势是当禁用断言时它的开销会更小,并且在断言失败时消息会包含$assertion表达式。这意味着如果你传入了 boolean 的条件作为$assertion,这个条件将不会显示为断言函数的参数;在调用你定义的assert_options()处理函数时,条件会转换为字符串,而布尔值FALSE会被转换成空字符串。

断言这个功能应该只被用来调试。你应该用于完整性检查时测试条件是否始终应该为TRUE,来指示某些程序错误,或者检查具体功能的存在(类似扩展函数或特定的系统限制和功能)。

断言不应该用于普通运行时操作,类似输入参数的检查。作为一个经验法则,在断言禁用时你的代码也应该能够正确地运行。

assert()的行为可以通过assert_options()来配置,或者手册页面上描述的.ini 设置。

assert_options()ASSERT_CALLBACK配置指令允许设置回调函数来处理失败的断言。

assert()回调函数在构建自动测试套件的时候尤其有用,因为它们允许你简易地捕获传入断言的代码,并包含断言的位置信息。当信息能够被其他方法捕获,使用断言可以让它更快更方便!

回调函数应该接受三个参数。第一个参数包括了断言失败所在的文件。第二个参数包含了断言失败所在的行号,第三个参数包含了失败的表达式(如有任意—字面值例如 1 或者"two"将不会传递到这个参数)。 PHP 5.4.8 及更高版本的用户也可以提供第四个可选参数,如果设置了,用于将$description指定到assert()

Expectations (PHP 7 only)

assert()is a language construct in PHP 7, allowing for the definition of expectations: assertions that take effect in development and testing environments, but are optimised away to have zero cost in production.

Whileassert_options()can still be used to control behaviour as described above for backward compatibility reasons, PHP 7 only code should use the two new configuration directives to control the behaviour ofassert()and not callassert_options().

PHP 7 configuration directives forassert()
DirectiveDefault valuePossible values
zend.assertions1
  • 1: generate and execute code (development mode)
  • 0: generate code but jump around it at runtime
  • -1: do not generate code (production mode)
assert.exception0
  • 1: throw when the assertion fails, either by throwing the object provided as the$exceptionor by throwing a newAssertionErrorobject if$exceptionwasn't provided
  • 0: use or generate aThrowableas described above, but only generate a warning based on that object rather than throwing it (compatible with PHP 5 behaviour)

参数

$assertion

断言。In PHP 5, this must be either astringto be evaluated or abooleanto 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.

$description

如果$assertion失败了,选项 description 将会包括在失败信息里。

$exception

In PHP 7, the second parameter can be aThrowableobject instead of a descriptivestring, in which case this is the object that will be thrown if the assertion fails and theassert.exceptionconfiguration directive is enabled.

返回值

assertion 是 false 则返回FALSE,否则是TRUE

更新日志

版本说明
7.0.0assert()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 aThrowableobject is given), or as the$descriptionsupported from PHP 5.4.8 onwards.
5.4.8增加了参数$description。$description现在也作为第四个参数提供给ASSERT_CALLBACK模式里的回调函数。

范例

Traditional assertions (PHP 5 and 7)

使用自定义处理程序处理失败的断言

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)