assert_optionsDescriptionChanges the behavior of the assert() function. This function can be used to set options such as the warning message, terminate script processing, or add a callback function when the assertion fails. assert() callbacks are particularly useful for building automated test suites because they allow you to easily capture the code passed to the assertion, along with information on where the assertion was made. While this information can be captured via other methods, using assertions makes it much faster and easier. The callback function should accept three arguments. The first argument contains the file in which the assertion failed. The second argument contains the line on which the assertion failed. The third argument contains the expression that failed (if any); literal values such as 1 or "two" are not passed via this argument. A simple callback function can be found in the example. If you're using a callback function or using the assert() function, PHP still displays an error due to the error-reporting setting in the PHP initialization file. You can alter the value in the initialization file or use the error_reporting() function but a better option is to use ASSERT_WARNING and ASSERT_QUIET_EVAL to control whether errors are output.
ExampleExample 990. Custom callback function <?php // create the callback function function assert_handler($file, $line, $evalcode) { echo "Oops! Assertion problem.<BR />\n"; echo "File: $file <BR />\n Line: $line <BR />\n Code Snippet: $evalcode <BR />\n"; } // set assertion options error_reporting(0); assert_options(ASSERT_ACTIVE,1); assert_options(ASSERT_CALLBACK, "assert_handler"); function test() { return FALSE; } assert('test()'); ?>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|