PHP with a unit function that can be used instead of ECHO debugging

Source: Internet
Author: User
Tags assert error handling execution ini

Today I found a function assert and assert_options, they can combine to complete a simple phpunit function, but it is too simple, so the usefulness is not too much, but the record is good.

The main problem is that you don't have the flexibility to define your own error message, only the number of files and rows that are problematic.

Specific methods of use can be seen <> or <>

At the same time can be combined with <> "XXVII." Error Handling and Logging functions "the things in the chapter, common use.

Here is a test file that I wrote, containing all the features of the test, but Assert_quiet_eval has not been very clear, did not test out what the specific role

<?php
function assert_failed ($file, $line, $expr) {
Print "Assertion failed in $file [$line]: $expr <br/>";
}
Error_reporting is set to 0, which is equivalent to calling Assert_options (assert_warning, 0);
error_reporting (0);
Whether to enable support for assert_active
Assert_options (assert_active, 1);
Do you want to stop the execution of the script when you send the first wanning
Assert_options (assert_bail, 0);
I don't know how to use it, I can't test it.
Assert_options (assert_quiet_eval, 0);
echo "Step 1 <br/>";
ASSERT (1==1);
echo "Step 2 <br/>";
ASSERT (2==1);
echo "Step 3 <br/>";
Set assert callback style, you can define the style of wanning information display
Assert_options (Assert_callback, ' assert_failed ');
The assert () does not display its own warnning information, and if Assert_callback is set, the Assert_callback function's information is still displayed, but the $expr parameters passed in the function do not work.
Assert_options (assert_warning, 1);
ASSERT (1==1);
ASSERT ((1/0) >2);
echo "Step 4 <br/>";
?>

The following passage is directly from the copy.

The Assert () function is a clever one this works along the same lines as we print statements, but it only works if a Certain condition is not matched. Essentially, assert () is used to say "this statement must be Trueif it isn ' t, please tell me." For example:
    print "Stage 1\n";
    assert (1 =  = 1);
    print "Stage 2\n";
    assert (1 =  = 2);
    print "Stage 3\n";

Here we are have two assert () s, with the the I call asserting that one must is equal to one, and the second call asserting That one must is equal to two. As it is impossible to redefine constants like 1 and 2, the I assert () would always evaluate to true, and the second W Ill always evaluate to false. Here's the output from script:
    Stage 1
    Stage 2
    W Arning:assert () [Http://www.php.net/function.assert]: Assertion failed
             in/home/paul/sandbox/php/assert.php on line 5
    Stage 3

The ' is ' not ' seen in the ' output at all because it evaluated to TRue, whereas the second assert () evaluated To false we have a warning about an assertion failure. However, script execution carries on so we Are "Stage 3" after the assertion failure. As long as assertions evaluate to true, they have no effect on the running of the script, which means you can insert them For debugging purposes and don't have to worry about taking them out once you are finished.
If you are are worried about your assertions slowing execution down, which, although the speed hit-would be minimal, are still a Valid concern, can disable execution of assert () by using the "assert_options () function or by setting assert.active To out in your php.ini file. If you are want to use assert_options (), it takes two parameters:the option to set and the value you wish to set it to.
Table 22-1 shows the list of options you can use for the "the" the "the" the "a" assert_options ():
Table 22-1. Parameter a assert_options ()
Parameter Default Description

Assert_active on enables evaluation of assert () calls

Assert_warning on makes PHP output a WARNING for each failed assertion

Assert_bail off forces PHP to end script execution on a failed assertion

Assert_quiet_eval off ignores errors in assert () calls

Assert_callback off Names user function to call on a failed assertion

To disable assert () calls, use this line of code:
Assert_options (assert_active, 0);

And to make the PHP end script execution rather than just issue a warning, we can use this line of code:
Assert_options (Assert_bail, 1);

This all of the options can be set in your php.ini file so they are always in effect. The options to change there are assert.active, assert.warning, Assert.bail, Assert.quiet_eval, and Assert_callback.
Assert_callback is a useful option, as it allows you to write a error handler for when your code fails a assertion. It takes the string name of a function to execute assertions fail, and the function for you define must take three Ters:one to hold the file where the assertion occurred, one to hold the line, and one to hold the expression. Using the three together in your callback the function allows you to generate meaningful error messages, you can debug. For example:
function assert_failed ($file, $line, $expr) {
Print "Assertion failed in $file $line: $expr \ n";
}
Assert_options (Assert_callback, ' assert_failed ');
Assert_options (assert_warning, 0);
$foo = 10;
$bar = 11;
ASSERT ($foo > $bar);

That example shows a callback function defined which takes $file, $line, and $expr for the three variables in, and O Utputs them whenever an assertion fails. To make this result actually happen, assert_options () are called to let PHP know that assert_failed () is the correct func tion to use as a callbacknote which there are no brackets after the string being passed into assert_options ().
Assert_warning is also disabled, which stops PHP from outputting a WARNING as as the running function. Finally, two variables are set, and are used as part of the A call to assert () As for you can, $foo are quite clearly not great Er than $bar, which means the assertion'll fail and call our callback. So, the output from the script is:assertion failed in/home/paul/tmp/blerg.php in line 9: $foo > $bar.
You can assert () "Any" statement you, as long as it'll return either TRue or false. This makes the ASSERT () function incredibly powerfuleven, you can be just turn off assertion exec Ution to make the code run in full speed.
Here are some the more examples of assert () able things:
ASSERT ($savings >= $salary/10);
ASSERT ($myarray = = Array ("Apone", "Burke", "Hicks"));
ASSERT (Preg_match ("/wild sheep chase/", $book));



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.