Files
doc-en/reference/info/functions/assert-options.xml
2023-10-08 17:57:35 +01:00

280 lines
7.9 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.assert-options" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>assert_options</refname>
<refpurpose>Set/get the various assert flags</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>
Set the various <function>assert</function> control options or just query
their current settings.
</para>
<note>
<simpara>
The use of <function>assert_options</function> is discouraged
in favor of setting and getting the &php.ini; directives
<link linkend="ini.zend.assertions">zend.assertions</link> and
<link linkend="ini.assert.exception">assert.exception</link> with
<function>ini_set</function> and <function>ini_get</function>, respectively.
</simpara>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>option</parameter></term>
<listitem>
<para>
<table>
<title>Assert Options</title>
<tgroup cols="4">
<thead>
<row>
<entry>Option</entry>
<entry>INI Setting</entry>
<entry>Default value</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>ASSERT_ACTIVE</entry>
<entry>assert.active</entry>
<entry>1</entry>
<entry>enable <function>assert</function> evaluation</entry>
</row>
<row>
<entry>ASSERT_EXCEPTION</entry>
<entry>assert.exception</entry>
<entry>1</entry>
<entry>throws an <classname>AssertionError</classname> for each failed assertions</entry>
</row>
<row>
<entry>ASSERT_WARNING</entry>
<entry>assert.warning</entry>
<entry>1</entry>
<entry>issue a PHP warning for each failed assertion</entry>
</row>
<row>
<entry>ASSERT_BAIL</entry>
<entry>assert.bail</entry>
<entry>0</entry>
<entry>terminate execution on failed assertions</entry>
</row>
<row>
<entry>ASSERT_QUIET_EVAL</entry>
<entry>assert.quiet_eval</entry>
<entry>0</entry>
<entry>
disable error_reporting during assertion expression
evaluation. Removed as of PHP 8.0.0.
</entry>
</row>
<row>
<entry>ASSERT_CALLBACK</entry>
<entry>assert.callback</entry>
<entry>(&null;)</entry>
<entry>Callback to call on failed assertions</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
An optional new value for the option.
</para>
<para>
The callback function set via <constant>ASSERT_CALLBACK</constant> or
<link linkend="ini.assert.callback">assert.callback</link>
should have the following signature:
<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>
The file where <function>assert</function> has been called.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>line</parameter></term>
<listitem>
<simpara>
The line where <function>assert</function> has been called.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>assertion</parameter></term>
<listitem>
<simpara>
Prior to PHP 8.0.0, the assertion which has been passed to <function>assert</function>,
but only when the assertion is given as a string.
(If the assertion is a boolean condition, this parameter will be an empty string.)
As of PHP 8.0.0, this parameter is always &null;.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>description</parameter></term>
<listitem>
<simpara>
The description that has been passed to <function>assert</function>.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
Passing an empty string as <parameter>value</parameter> resets the assert callback.
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the original setting of any option.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
If <parameter>option</parameter> is not a valid option a
<classname>ValueError</classname> is thrown.
</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> is now deprecated.
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
If <parameter>option</parameter> is not a valid option,
a <classname>ValueError</classname> is now thrown.
Previously &false; was returned.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>assert_options</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
// This is our function to handle
// assert failures
function assert_failure($file, $line, $assertion, $message)
{
echo "The assertion $assertion in $file on line $line has failed: $message";
}
// This is our test function
function test_assert($parameter)
{
assert(is_bool($parameter));
}
// Set our assert options
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_BAIL, true);
assert_options(ASSERT_WARNING, false);
assert_options(ASSERT_CALLBACK, 'assert_failure');
// Make an assert that would fail
test_assert(1);
// This is never reached due to ASSERT_BAIL
// being 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
-->