PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




assert_options

Name

assert_options — Changes assert() behavior.

Synopsis

mixed assert(assert_option[, value]);
int assert_option: Single ASSERT_* flag, such as ASSERT_ACTIVE
mixed value (optional): Optional value to assign to the flag

Returns

Original setting; FALSE on error

Description

Changes 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.

Option .ini Parameter Default Description
ASSERT_ACTIVE assert.active 1 Enable evaluation.
ASSERT_WARNING assert.warning 1 Display warning with failure.
ASSERT_BAIL assert.bail 0 Terminate script execution on failure.
ASSERT_QUIET_EVAL assert.quiet_eval 0 Do not report errors in code evaluated inside a call to assert() .
ASSERT_CALLBACK assert.callback NULL Use custom function to display warnings.


Version

Existing since version 4.0

Example

Example 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.